blob: 45040f99031d9c8e3cde76aa888cf120189e5c0c [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>
28#include <linux/mutex.h>
29#include <linux/clk.h>
30
31#include <media/v4l2-common.h>
32#include <media/v4l2-dev.h>
Paulius Zaleckas092d3922008-07-11 20:50:31 -030033#include <media/videobuf-dma-sg.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030034#include <media/soc_camera.h>
35
36#include <linux/videodev2.h>
37
38#include <asm/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/pxa-regs.h>
40#include <mach/camera.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030041
42#define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
43#define PXA_CAM_DRV_NAME "pxa27x-camera"
44
Eric Miao5ca11fa2008-12-18 11:15:50 -030045/* Camera Interface */
46#define CICR0 0x0000
47#define CICR1 0x0004
48#define CICR2 0x0008
49#define CICR3 0x000C
50#define CICR4 0x0010
51#define CISR 0x0014
52#define CIFR 0x0018
53#define CITOR 0x001C
54#define CIBR0 0x0028
55#define CIBR1 0x0030
56#define CIBR2 0x0038
57
58#define CICR0_DMAEN (1 << 31) /* DMA request enable */
59#define CICR0_PAR_EN (1 << 30) /* Parity enable */
60#define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
61#define CICR0_ENB (1 << 28) /* Camera interface enable */
62#define CICR0_DIS (1 << 27) /* Camera interface disable */
63#define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
64#define CICR0_TOM (1 << 9) /* Time-out mask */
65#define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
66#define CICR0_FEM (1 << 7) /* FIFO-empty mask */
67#define CICR0_EOLM (1 << 6) /* End-of-line mask */
68#define CICR0_PERRM (1 << 5) /* Parity-error mask */
69#define CICR0_QDM (1 << 4) /* Quick-disable mask */
70#define CICR0_CDM (1 << 3) /* Disable-done mask */
71#define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
72#define CICR0_EOFM (1 << 1) /* End-of-frame mask */
73#define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
74
75#define CICR1_TBIT (1 << 31) /* Transparency bit */
76#define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
77#define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
78#define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
79#define CICR1_RGB_F (1 << 11) /* RGB format */
80#define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
81#define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
82#define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
83#define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
84#define CICR1_DW (0x7 << 0) /* Data width mask */
85
86#define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
87 wait count mask */
88#define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
89 wait count mask */
90#define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
91#define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
92 wait count mask */
93#define CICR2_FSW (0x7 << 0) /* Frame stabilization
94 wait count mask */
95
96#define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
97 wait count mask */
98#define CICR3_EFW (0xff << 16) /* End-of-frame line clock
99 wait count mask */
100#define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
101#define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
102 wait count mask */
103#define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
104
105#define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
106#define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
107#define CICR4_PCP (1 << 22) /* Pixel clock polarity */
108#define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
109#define CICR4_VSP (1 << 20) /* Vertical sync polarity */
110#define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
111#define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
112#define CICR4_DIV (0xff << 0) /* Clock divisor mask */
113
114#define CISR_FTO (1 << 15) /* FIFO time-out */
115#define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
116#define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
117#define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
118#define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
119#define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
120#define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
121#define CISR_EOL (1 << 8) /* End of line */
122#define CISR_PAR_ERR (1 << 7) /* Parity error */
123#define CISR_CQD (1 << 6) /* Camera interface quick disable */
124#define CISR_CDD (1 << 5) /* Camera interface disable done */
125#define CISR_SOF (1 << 4) /* Start of frame */
126#define CISR_EOF (1 << 3) /* End of frame */
127#define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
128#define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
129#define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
130
131#define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
132#define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
133#define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
134#define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
135#define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
136#define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
137#define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
138#define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
139
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300140#define CICR0_SIM_MP (0 << 24)
141#define CICR0_SIM_SP (1 << 24)
142#define CICR0_SIM_MS (2 << 24)
143#define CICR0_SIM_EP (3 << 24)
144#define CICR0_SIM_ES (4 << 24)
145
146#define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
147#define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300148#define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
149#define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
150#define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300151
152#define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
153#define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
154#define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
155#define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
156#define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
157
158#define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
159#define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
160#define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
161#define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
162
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300163#define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
164 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
165 CICR0_EOFM | CICR0_FOM)
166
167static DEFINE_MUTEX(camera_lock);
168
169/*
170 * Structures
171 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300172enum pxa_camera_active_dma {
173 DMA_Y = 0x1,
174 DMA_U = 0x2,
175 DMA_V = 0x4,
176};
177
178/* descriptor needed for the PXA DMA engine */
179struct pxa_cam_dma {
180 dma_addr_t sg_dma;
181 struct pxa_dma_desc *sg_cpu;
182 size_t sg_size;
183 int sglen;
184};
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300185
186/* buffer for one video frame */
187struct pxa_buffer {
188 /* common v4l buffer stuff -- must be first */
189 struct videobuf_buffer vb;
190
191 const struct soc_camera_data_format *fmt;
192
Mike Rapoporta5462e52008-04-22 10:36:32 -0300193 /* our descriptor lists for Y, U and V channels */
194 struct pxa_cam_dma dmas[3];
195
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300196 int inwork;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300197
198 enum pxa_camera_active_dma active_dma;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300199};
200
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300201struct pxa_camera_dev {
202 struct device *dev;
203 /* PXA27x is only supposed to handle one camera on its Quick Capture
204 * interface. If anyone ever builds hardware to enable more than
205 * one camera, they will have to modify this driver too */
206 struct soc_camera_device *icd;
207 struct clk *clk;
208
209 unsigned int irq;
210 void __iomem *base;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300211
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300212 int channels;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300213 unsigned int dma_chans[3];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300214
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300215 struct pxacamera_platform_data *pdata;
216 struct resource *res;
217 unsigned long platform_flags;
218 unsigned long platform_mclk_10khz;
219
220 struct list_head capture;
221
222 spinlock_t lock;
223
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300224 struct pxa_buffer *active;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300225 struct pxa_dma_desc *sg_tail[3];
Robert Jarzmik3f6ac492008-08-02 07:10:04 -0300226
227 u32 save_cicr[5];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300228};
229
230static const char *pxa_cam_driver_description = "PXA_Camera";
231
232static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
233
234/*
235 * Videobuf operations
236 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300237static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
238 unsigned int *size)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300239{
240 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300241 struct soc_camera_host *ici =
242 to_soc_camera_host(icd->dev.parent);
243 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300244
245 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
246
Mike Rapoporta5462e52008-04-22 10:36:32 -0300247 /* planar capture requires Y, U and V buffers to be page aligned */
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300248 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300249 *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
250 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
251 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
252 } else {
253 *size = icd->width * icd->height *
254 ((icd->current_fmt->depth + 7) >> 3);
255 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300256
257 if (0 == *count)
258 *count = 32;
259 while (*size * *count > vid_limit * 1024 * 1024)
260 (*count)--;
261
262 return 0;
263}
264
265static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
266{
267 struct soc_camera_device *icd = vq->priv_data;
268 struct soc_camera_host *ici =
269 to_soc_camera_host(icd->dev.parent);
270 struct pxa_camera_dev *pcdev = ici->priv;
271 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300272 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300273
274 BUG_ON(in_interrupt());
275
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300276 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300277 &buf->vb, buf->vb.baddr, buf->vb.bsize);
278
279 /* This waits until this buffer is out of danger, i.e., until it is no
280 * longer in STATE_QUEUED or STATE_ACTIVE */
281 videobuf_waiton(&buf->vb, 0, 0);
282 videobuf_dma_unmap(vq, dma);
283 videobuf_dma_free(dma);
284
Mike Rapoporta5462e52008-04-22 10:36:32 -0300285 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
286 if (buf->dmas[i].sg_cpu)
287 dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
288 buf->dmas[i].sg_cpu,
289 buf->dmas[i].sg_dma);
290 buf->dmas[i].sg_cpu = NULL;
291 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300292
293 buf->vb.state = VIDEOBUF_NEEDS_INIT;
294}
295
Mike Rapoporta5462e52008-04-22 10:36:32 -0300296static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
297 struct pxa_buffer *buf,
298 struct videobuf_dmabuf *dma, int channel,
299 int sglen, int sg_start, int cibr,
300 unsigned int size)
301{
302 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
303 int i;
304
305 if (pxa_dma->sg_cpu)
306 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
307 pxa_dma->sg_cpu, pxa_dma->sg_dma);
308
309 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
310 pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
311 &pxa_dma->sg_dma, GFP_KERNEL);
312 if (!pxa_dma->sg_cpu)
313 return -ENOMEM;
314
315 pxa_dma->sglen = sglen;
316
317 for (i = 0; i < sglen; i++) {
318 int sg_i = sg_start + i;
319 struct scatterlist *sg = dma->sglist;
320 unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
321
322 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
323 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
324
325 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
326 xfer_len = (min(dma_len, size) + 7) & ~7;
327
328 pxa_dma->sg_cpu[i].dcmd =
329 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
330 size -= dma_len;
331 pxa_dma->sg_cpu[i].ddadr =
332 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
333 }
334
335 pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
336 pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
337
338 return 0;
339}
340
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300341static int pxa_videobuf_prepare(struct videobuf_queue *vq,
342 struct videobuf_buffer *vb, enum v4l2_field field)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300343{
344 struct soc_camera_device *icd = vq->priv_data;
345 struct soc_camera_host *ici =
346 to_soc_camera_host(icd->dev.parent);
347 struct pxa_camera_dev *pcdev = ici->priv;
348 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300349 int ret;
350 int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
351 int size_y, size_u = 0, size_v = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300352
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300353 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300354 vb, vb->baddr, vb->bsize);
355
356 /* Added list head initialization on alloc */
357 WARN_ON(!list_empty(&vb->queue));
358
359#ifdef DEBUG
360 /* This can be useful if you want to see if we actually fill
361 * the buffer with something */
362 memset((void *)vb->baddr, 0xaa, vb->bsize);
363#endif
364
365 BUG_ON(NULL == icd->current_fmt);
366
367 /* I think, in buf_prepare you only have to protect global data,
368 * the actual buffer is yours */
369 buf->inwork = 1;
370
371 if (buf->fmt != icd->current_fmt ||
372 vb->width != icd->width ||
373 vb->height != icd->height ||
374 vb->field != field) {
375 buf->fmt = icd->current_fmt;
376 vb->width = icd->width;
377 vb->height = icd->height;
378 vb->field = field;
379 vb->state = VIDEOBUF_NEEDS_INIT;
380 }
381
382 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
383 if (0 != vb->baddr && vb->bsize < vb->size) {
384 ret = -EINVAL;
385 goto out;
386 }
387
388 if (vb->state == VIDEOBUF_NEEDS_INIT) {
389 unsigned int size = vb->size;
390 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
391
392 ret = videobuf_iolock(vq, vb, NULL);
393 if (ret)
394 goto fail;
395
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300396 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300397 /* FIXME the calculations should be more precise */
398 sglen_y = dma->sglen / 2;
399 sglen_u = sglen_v = dma->sglen / 4 + 1;
400 sglen_yu = sglen_y + sglen_u;
401 size_y = size / 2;
402 size_u = size_v = size / 4;
403 } else {
404 sglen_y = dma->sglen;
405 size_y = size;
406 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300407
Mike Rapoporta5462e52008-04-22 10:36:32 -0300408 /* init DMA for Y channel */
409 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
410 0, 0x28, size_y);
411
412 if (ret) {
413 dev_err(pcdev->dev,
414 "DMA initialization for Y/RGB failed\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300415 goto fail;
416 }
417
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300418 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300419 /* init DMA for U channel */
420 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
421 sglen_y, 0x30, size_u);
422 if (ret) {
423 dev_err(pcdev->dev,
424 "DMA initialization for U failed\n");
425 goto fail_u;
426 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300427
Mike Rapoporta5462e52008-04-22 10:36:32 -0300428 /* init DMA for V channel */
429 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
430 sglen_yu, 0x38, size_v);
431 if (ret) {
432 dev_err(pcdev->dev,
433 "DMA initialization for V failed\n");
434 goto fail_v;
435 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300436 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300437
438 vb->state = VIDEOBUF_PREPARED;
439 }
440
441 buf->inwork = 0;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300442 buf->active_dma = DMA_Y;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300443 if (pcdev->channels == 3)
Mike Rapoporta5462e52008-04-22 10:36:32 -0300444 buf->active_dma |= DMA_U | DMA_V;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300445
446 return 0;
447
Mike Rapoporta5462e52008-04-22 10:36:32 -0300448fail_v:
449 dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
450 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
451fail_u:
452 dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
453 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300454fail:
455 free_buffer(vq, buf);
456out:
457 buf->inwork = 0;
458 return ret;
459}
460
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300461static void pxa_videobuf_queue(struct videobuf_queue *vq,
462 struct videobuf_buffer *vb)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300463{
464 struct soc_camera_device *icd = vq->priv_data;
465 struct soc_camera_host *ici =
466 to_soc_camera_host(icd->dev.parent);
467 struct pxa_camera_dev *pcdev = ici->priv;
468 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300469 struct pxa_buffer *active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300470 unsigned long flags;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300471 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300472
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300473 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300474 vb, vb->baddr, vb->bsize);
475 spin_lock_irqsave(&pcdev->lock, flags);
476
477 list_add_tail(&vb->queue, &pcdev->capture);
478
479 vb->state = VIDEOBUF_ACTIVE;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300480 active = pcdev->active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300481
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300482 if (!active) {
Eric Miao5ca11fa2008-12-18 11:15:50 -0300483 unsigned long cifr, cicr0;
484
485 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
486 __raw_writel(cifr, pcdev->base + CIFR);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300487
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300488 for (i = 0; i < pcdev->channels; i++) {
489 DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
490 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
491 pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300492 }
493
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300494 pcdev->active = buf;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300495
496 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
497 __raw_writel(cicr0, pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300498 } else {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300499 struct pxa_cam_dma *buf_dma;
500 struct pxa_cam_dma *act_dma;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300501 int nents;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300502
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300503 for (i = 0; i < pcdev->channels; i++) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300504 buf_dma = &buf->dmas[i];
505 act_dma = &active->dmas[i];
506 nents = buf_dma->sglen;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300507
Mike Rapoporta5462e52008-04-22 10:36:32 -0300508 /* Stop DMA engine */
509 DCSR(pcdev->dma_chans[i]) = 0;
510
511 /* Add the descriptors we just initialized to
512 the currently running chain */
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300513 pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
514 pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300515
516 /* Setup a dummy descriptor with the DMA engines current
517 * state
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300518 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300519 buf_dma->sg_cpu[nents].dsadr =
520 pcdev->res->start + 0x28 + i*8; /* CIBRx */
521 buf_dma->sg_cpu[nents].dtadr =
522 DTADR(pcdev->dma_chans[i]);
523 buf_dma->sg_cpu[nents].dcmd =
524 DCMD(pcdev->dma_chans[i]);
525
526 if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
527 /* The DMA engine is on the last
528 descriptor, set the next descriptors
529 address to the descriptors we just
530 initialized */
531 buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
532 } else {
533 buf_dma->sg_cpu[nents].ddadr =
534 DDADR(pcdev->dma_chans[i]);
535 }
536
537 /* The next descriptor is the dummy descriptor */
538 DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
539 sizeof(struct pxa_dma_desc);
540
541 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300542 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300543 }
544
545 spin_unlock_irqrestore(&pcdev->lock, flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300546}
547
548static void pxa_videobuf_release(struct videobuf_queue *vq,
549 struct videobuf_buffer *vb)
550{
551 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
552#ifdef DEBUG
553 struct soc_camera_device *icd = vq->priv_data;
554
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300555 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300556 vb, vb->baddr, vb->bsize);
557
558 switch (vb->state) {
559 case VIDEOBUF_ACTIVE:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300560 dev_dbg(&icd->dev, "%s (active)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300561 break;
562 case VIDEOBUF_QUEUED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300563 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300564 break;
565 case VIDEOBUF_PREPARED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300566 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300567 break;
568 default:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300569 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300570 break;
571 }
572#endif
573
574 free_buffer(vq, buf);
575}
576
Mike Rapoporta5462e52008-04-22 10:36:32 -0300577static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
578 struct videobuf_buffer *vb,
579 struct pxa_buffer *buf)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300580{
Eric Miao5ca11fa2008-12-18 11:15:50 -0300581 unsigned long cicr0;
582
Mike Rapoporta5462e52008-04-22 10:36:32 -0300583 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
584 list_del_init(&vb->queue);
585 vb->state = VIDEOBUF_DONE;
586 do_gettimeofday(&vb->ts);
587 vb->field_count++;
588 wake_up(&vb->done);
589
590 if (list_empty(&pcdev->capture)) {
591 pcdev->active = NULL;
592 DCSR(pcdev->dma_chans[0]) = 0;
593 DCSR(pcdev->dma_chans[1]) = 0;
594 DCSR(pcdev->dma_chans[2]) = 0;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300595
596 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
597 __raw_writel(cicr0, pcdev->base + CICR0);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300598 return;
599 }
600
601 pcdev->active = list_entry(pcdev->capture.next,
602 struct pxa_buffer, vb.queue);
603}
604
605static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
606 enum pxa_camera_active_dma act_dma)
607{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300608 struct pxa_buffer *buf;
609 unsigned long flags;
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300610 u32 status, camera_status, overrun;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300611 struct videobuf_buffer *vb;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300612 unsigned long cifr, cicr0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300613
614 spin_lock_irqsave(&pcdev->lock, flags);
615
Mike Rapoporta5462e52008-04-22 10:36:32 -0300616 status = DCSR(channel);
617 DCSR(channel) = status | DCSR_ENDINTR;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300618
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300619 if (status & DCSR_BUSERR) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300620 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300621 goto out;
622 }
623
624 if (!(status & DCSR_ENDINTR)) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300625 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
626 "status: 0x%08x\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300627 goto out;
628 }
629
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300630 if (!pcdev->active) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300631 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300632 goto out;
633 }
634
Eric Miao5ca11fa2008-12-18 11:15:50 -0300635 camera_status = __raw_readl(pcdev->base + CISR);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300636 overrun = CISR_IFO_0;
637 if (pcdev->channels == 3)
638 overrun |= CISR_IFO_1 | CISR_IFO_2;
639 if (camera_status & overrun) {
640 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
641 /* Stop the Capture Interface */
Eric Miao5ca11fa2008-12-18 11:15:50 -0300642 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
643 __raw_writel(cicr0, pcdev->base + CICR0);
644
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300645 /* Stop DMA */
646 DCSR(channel) = 0;
647 /* Reset the FIFOs */
Eric Miao5ca11fa2008-12-18 11:15:50 -0300648 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
649 __raw_writel(cifr, pcdev->base + CIFR);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300650 /* Enable End-Of-Frame Interrupt */
Eric Miao5ca11fa2008-12-18 11:15:50 -0300651 cicr0 &= ~CICR0_EOFM;
652 __raw_writel(cicr0, pcdev->base + CICR0);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300653 /* Restart the Capture Interface */
Eric Miao5ca11fa2008-12-18 11:15:50 -0300654 __raw_writel(cicr0 | CICR0_ENB, pcdev->base + CICR0);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300655 goto out;
656 }
657
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300658 vb = &pcdev->active->vb;
659 buf = container_of(vb, struct pxa_buffer, vb);
660 WARN_ON(buf->inwork || list_empty(&vb->queue));
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300661 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300662 vb, vb->baddr, vb->bsize);
663
Mike Rapoporta5462e52008-04-22 10:36:32 -0300664 buf->active_dma &= ~act_dma;
665 if (!buf->active_dma)
666 pxa_camera_wakeup(pcdev, vb, buf);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300667
668out:
669 spin_unlock_irqrestore(&pcdev->lock, flags);
670}
671
Mike Rapoporta5462e52008-04-22 10:36:32 -0300672static void pxa_camera_dma_irq_y(int channel, void *data)
673{
674 struct pxa_camera_dev *pcdev = data;
675 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
676}
677
678static void pxa_camera_dma_irq_u(int channel, void *data)
679{
680 struct pxa_camera_dev *pcdev = data;
681 pxa_camera_dma_irq(channel, pcdev, DMA_U);
682}
683
684static void pxa_camera_dma_irq_v(int channel, void *data)
685{
686 struct pxa_camera_dev *pcdev = data;
687 pxa_camera_dma_irq(channel, pcdev, DMA_V);
688}
689
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300690static struct videobuf_queue_ops pxa_videobuf_ops = {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300691 .buf_setup = pxa_videobuf_setup,
692 .buf_prepare = pxa_videobuf_prepare,
693 .buf_queue = pxa_videobuf_queue,
694 .buf_release = pxa_videobuf_release,
695};
696
Magnus Damma034d1b2008-07-11 20:59:34 -0300697static void pxa_camera_init_videobuf(struct videobuf_queue *q,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300698 struct soc_camera_device *icd)
699{
Magnus Damma034d1b2008-07-11 20:59:34 -0300700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
701 struct pxa_camera_dev *pcdev = ici->priv;
702
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300703 /* We must pass NULL as dev pointer, then all pci_* dma operations
704 * transform to normal dma_* ones. */
Magnus Damma034d1b2008-07-11 20:59:34 -0300705 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300706 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
707 sizeof(struct pxa_buffer), icd);
708}
709
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300710static int mclk_get_divisor(struct pxa_camera_dev *pcdev)
711{
712 unsigned int mclk_10khz = pcdev->platform_mclk_10khz;
713 unsigned long div;
714 unsigned long lcdclk;
715
716 lcdclk = clk_get_rate(pcdev->clk) / 10000;
717
718 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
719 * they get a nice Oops */
720 div = (lcdclk + 2 * mclk_10khz - 1) / (2 * mclk_10khz) - 1;
721
722 dev_dbg(pcdev->dev, "LCD clock %lukHz, target freq %dkHz, "
723 "divisor %lu\n", lcdclk * 10, mclk_10khz * 10, div);
724
725 return div;
726}
727
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300728static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300729{
730 struct pxacamera_platform_data *pdata = pcdev->pdata;
731 u32 cicr4 = 0;
732
733 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
734 pcdev, pdata);
735
736 if (pdata && pdata->init) {
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300737 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300738 pdata->init(pcdev->dev);
739 }
740
Eric Miao5ca11fa2008-12-18 11:15:50 -0300741 /* disable all interrupts */
742 __raw_writel(0x3ff, pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300743
744 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
745 cicr4 |= CICR4_PCLK_EN;
746 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
747 cicr4 |= CICR4_MCLK_EN;
748 if (pcdev->platform_flags & PXA_CAMERA_PCP)
749 cicr4 |= CICR4_PCP;
750 if (pcdev->platform_flags & PXA_CAMERA_HSP)
751 cicr4 |= CICR4_HSP;
752 if (pcdev->platform_flags & PXA_CAMERA_VSP)
753 cicr4 |= CICR4_VSP;
754
Eric Miao5ca11fa2008-12-18 11:15:50 -0300755 cicr4 |= mclk_get_divisor(pcdev);
756 __raw_writel(cicr4, pcdev->base + CICR4);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300757
758 clk_enable(pcdev->clk);
759}
760
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300761static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300762{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300763 clk_disable(pcdev->clk);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300764}
765
766static irqreturn_t pxa_camera_irq(int irq, void *data)
767{
768 struct pxa_camera_dev *pcdev = data;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300769 unsigned long status, cicr0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300770
Eric Miao5ca11fa2008-12-18 11:15:50 -0300771 status = __raw_readl(pcdev->base + CISR);
772 dev_dbg(pcdev->dev, "Camera interrupt status 0x%lx\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300773
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300774 if (!status)
775 return IRQ_NONE;
776
Eric Miao5ca11fa2008-12-18 11:15:50 -0300777 __raw_writel(status, pcdev->base + CISR);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300778
779 if (status & CISR_EOF) {
780 int i;
781 for (i = 0; i < pcdev->channels; i++) {
782 DDADR(pcdev->dma_chans[i]) =
783 pcdev->active->dmas[i].sg_dma;
784 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
785 }
Eric Miao5ca11fa2008-12-18 11:15:50 -0300786 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
787 __raw_writel(cicr0, pcdev->base + CICR0);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300788 }
789
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300790 return IRQ_HANDLED;
791}
792
793/* The following two functions absolutely depend on the fact, that
794 * there can be only one camera on PXA quick capture interface */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300795static int pxa_camera_add_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300796{
797 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
798 struct pxa_camera_dev *pcdev = ici->priv;
799 int ret;
800
801 mutex_lock(&camera_lock);
802
803 if (pcdev->icd) {
804 ret = -EBUSY;
805 goto ebusy;
806 }
807
808 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
809 icd->devnum);
810
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300811 pxa_camera_activate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300812 ret = icd->ops->init(icd);
813
814 if (!ret)
815 pcdev->icd = icd;
816
817ebusy:
818 mutex_unlock(&camera_lock);
819
820 return ret;
821}
822
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300823static void pxa_camera_remove_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300824{
825 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
826 struct pxa_camera_dev *pcdev = ici->priv;
827
828 BUG_ON(icd != pcdev->icd);
829
830 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
831 icd->devnum);
832
833 /* disable capture, disable interrupts */
Eric Miao5ca11fa2008-12-18 11:15:50 -0300834 __raw_writel(0x3ff, pcdev->base + CICR0);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300835
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300836 /* Stop DMA engine */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300837 DCSR(pcdev->dma_chans[0]) = 0;
838 DCSR(pcdev->dma_chans[1]) = 0;
839 DCSR(pcdev->dma_chans[2]) = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300840
841 icd->ops->release(icd);
842
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300843 pxa_camera_deactivate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300844
845 pcdev->icd = NULL;
846}
847
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300848static int test_platform_param(struct pxa_camera_dev *pcdev,
849 unsigned char buswidth, unsigned long *flags)
850{
851 /*
852 * Platform specified synchronization and pixel clock polarities are
853 * only a recommendation and are only used during probing. The PXA270
854 * quick capture interface supports both.
855 */
856 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
857 SOCAM_MASTER : SOCAM_SLAVE) |
858 SOCAM_HSYNC_ACTIVE_HIGH |
859 SOCAM_HSYNC_ACTIVE_LOW |
860 SOCAM_VSYNC_ACTIVE_HIGH |
861 SOCAM_VSYNC_ACTIVE_LOW |
862 SOCAM_PCLK_SAMPLE_RISING |
863 SOCAM_PCLK_SAMPLE_FALLING;
864
865 /* If requested data width is supported by the platform, use it */
866 switch (buswidth) {
867 case 10:
868 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
869 return -EINVAL;
870 *flags |= SOCAM_DATAWIDTH_10;
871 break;
872 case 9:
873 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
874 return -EINVAL;
875 *flags |= SOCAM_DATAWIDTH_9;
876 break;
877 case 8:
878 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
879 return -EINVAL;
880 *flags |= SOCAM_DATAWIDTH_8;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300881 break;
882 default:
883 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300884 }
885
886 return 0;
887}
888
889static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300890{
891 struct soc_camera_host *ici =
892 to_soc_camera_host(icd->dev.parent);
893 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300894 unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300895 u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300896 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300897
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300898 if (ret < 0)
899 return ret;
900
901 camera_flags = icd->ops->query_bus_param(icd);
902
903 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
904 if (!common_flags)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300905 return -EINVAL;
906
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300907 pcdev->channels = 1;
908
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300909 /* Make choises, based on platform preferences */
910 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
911 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
912 if (pcdev->platform_flags & PXA_CAMERA_HSP)
913 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
914 else
915 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
916 }
917
918 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
919 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
920 if (pcdev->platform_flags & PXA_CAMERA_VSP)
921 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
922 else
923 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
924 }
925
926 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
927 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
928 if (pcdev->platform_flags & PXA_CAMERA_PCP)
929 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
930 else
931 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
932 }
933
934 ret = icd->ops->set_bus_param(icd, common_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300935 if (ret < 0)
936 return ret;
937
938 /* Datawidth is now guaranteed to be equal to one of the three values.
939 * We fix bit-per-pixel equal to data-width... */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300940 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
941 case SOCAM_DATAWIDTH_10:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300942 dw = 4;
943 bpp = 0x40;
944 break;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300945 case SOCAM_DATAWIDTH_9:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300946 dw = 3;
947 bpp = 0x20;
948 break;
949 default:
950 /* Actually it can only be 8 now,
951 * default is just to silence compiler warnings */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300952 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300953 dw = 2;
954 bpp = 0;
955 }
956
957 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
958 cicr4 |= CICR4_PCLK_EN;
959 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
960 cicr4 |= CICR4_MCLK_EN;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300961 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300962 cicr4 |= CICR4_PCP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300963 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300964 cicr4 |= CICR4_HSP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300965 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300966 cicr4 |= CICR4_VSP;
967
Eric Miao5ca11fa2008-12-18 11:15:50 -0300968 cicr0 = __raw_readl(pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300969 if (cicr0 & CICR0_ENB)
Eric Miao5ca11fa2008-12-18 11:15:50 -0300970 __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300971
972 cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
973
974 switch (pixfmt) {
975 case V4L2_PIX_FMT_YUV422P:
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300976 pcdev->channels = 3;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300977 cicr1 |= CICR1_YCBCR_F;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300978 /*
979 * Normally, pxa bus wants as input UYVY format. We allow all
980 * reorderings of the YUV422 format, as no processing is done,
981 * and the YUV stream is just passed through without any
982 * transformation. Note that UYVY is the only format that
983 * should be used if pxa framebuffer Overlay2 is used.
984 */
985 case V4L2_PIX_FMT_UYVY:
986 case V4L2_PIX_FMT_VYUY:
Mike Rapoporta5462e52008-04-22 10:36:32 -0300987 case V4L2_PIX_FMT_YUYV:
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300988 case V4L2_PIX_FMT_YVYU:
Mike Rapoporta5462e52008-04-22 10:36:32 -0300989 cicr1 |= CICR1_COLOR_SP_VAL(2);
990 break;
991 case V4L2_PIX_FMT_RGB555:
992 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
993 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
994 break;
995 case V4L2_PIX_FMT_RGB565:
996 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
997 break;
998 }
999
Eric Miao5ca11fa2008-12-18 11:15:50 -03001000 cicr2 = 0;
1001 cicr3 = CICR3_LPF_VAL(icd->height - 1) |
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001002 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
Eric Miao5ca11fa2008-12-18 11:15:50 -03001003 cicr4 |= mclk_get_divisor(pcdev);
1004
1005 __raw_writel(cicr1, pcdev->base + CICR1);
1006 __raw_writel(cicr2, pcdev->base + CICR2);
1007 __raw_writel(cicr3, pcdev->base + CICR3);
1008 __raw_writel(cicr4, pcdev->base + CICR4);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001009
1010 /* CIF interrupts are not used, only DMA */
Eric Miao5ca11fa2008-12-18 11:15:50 -03001011 cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1012 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
1013 cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
1014 __raw_writel(cicr0, pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001015
1016 return 0;
1017}
1018
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001019static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
1020 unsigned char buswidth)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001021{
1022 struct soc_camera_host *ici =
1023 to_soc_camera_host(icd->dev.parent);
1024 struct pxa_camera_dev *pcdev = ici->priv;
1025 unsigned long bus_flags, camera_flags;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001026 int ret = test_platform_param(pcdev, buswidth, &bus_flags);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001027
1028 if (ret < 0)
1029 return ret;
1030
1031 camera_flags = icd->ops->query_bus_param(icd);
1032
1033 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
1034}
1035
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001036static const struct soc_camera_data_format pxa_camera_formats[] = {
1037 {
1038 .name = "Planar YUV422 16 bit",
1039 .depth = 16,
1040 .fourcc = V4L2_PIX_FMT_YUV422P,
1041 .colorspace = V4L2_COLORSPACE_JPEG,
1042 },
1043};
1044
1045static bool buswidth_supported(struct soc_camera_device *icd, int depth)
1046{
1047 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1048 struct pxa_camera_dev *pcdev = ici->priv;
1049
1050 switch (depth) {
1051 case 8:
1052 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
1053 case 9:
1054 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
1055 case 10:
1056 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
1057 }
1058 return false;
1059}
1060
1061static int required_buswidth(const struct soc_camera_data_format *fmt)
1062{
1063 switch (fmt->fourcc) {
1064 case V4L2_PIX_FMT_UYVY:
1065 case V4L2_PIX_FMT_VYUY:
1066 case V4L2_PIX_FMT_YUYV:
1067 case V4L2_PIX_FMT_YVYU:
1068 case V4L2_PIX_FMT_RGB565:
1069 case V4L2_PIX_FMT_RGB555:
1070 return 8;
1071 default:
1072 return fmt->depth;
1073 }
1074}
1075
1076static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
1077 struct soc_camera_format_xlate *xlate)
1078{
1079 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1080 int formats = 0, buswidth, ret;
1081
1082 buswidth = required_buswidth(icd->formats + idx);
1083
1084 if (!buswidth_supported(icd, buswidth))
1085 return 0;
1086
1087 ret = pxa_camera_try_bus_param(icd, buswidth);
1088 if (ret < 0)
1089 return 0;
1090
1091 switch (icd->formats[idx].fourcc) {
1092 case V4L2_PIX_FMT_UYVY:
1093 formats++;
1094 if (xlate) {
1095 xlate->host_fmt = &pxa_camera_formats[0];
1096 xlate->cam_fmt = icd->formats + idx;
1097 xlate->buswidth = buswidth;
1098 xlate++;
1099 dev_dbg(&ici->dev, "Providing format %s using %s\n",
1100 pxa_camera_formats[0].name,
1101 icd->formats[idx].name);
1102 }
1103 case V4L2_PIX_FMT_VYUY:
1104 case V4L2_PIX_FMT_YUYV:
1105 case V4L2_PIX_FMT_YVYU:
1106 case V4L2_PIX_FMT_RGB565:
1107 case V4L2_PIX_FMT_RGB555:
1108 formats++;
1109 if (xlate) {
1110 xlate->host_fmt = icd->formats + idx;
1111 xlate->cam_fmt = icd->formats + idx;
1112 xlate->buswidth = buswidth;
1113 xlate++;
1114 dev_dbg(&ici->dev, "Providing format %s packed\n",
1115 icd->formats[idx].name);
1116 }
1117 break;
1118 default:
1119 /* Generic pass-through */
1120 formats++;
1121 if (xlate) {
1122 xlate->host_fmt = icd->formats + idx;
1123 xlate->cam_fmt = icd->formats + idx;
1124 xlate->buswidth = icd->formats[idx].depth;
1125 xlate++;
1126 dev_dbg(&ici->dev,
1127 "Providing format %s in pass-through mode\n",
1128 icd->formats[idx].name);
1129 }
1130 }
1131
1132 return formats;
1133}
1134
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001135static int pxa_camera_set_fmt(struct soc_camera_device *icd,
1136 __u32 pixfmt, struct v4l2_rect *rect)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001137{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001138 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1139 const struct soc_camera_data_format *host_fmt, *cam_fmt = NULL;
1140 const struct soc_camera_format_xlate *xlate;
1141 int ret, buswidth;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001142
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001143 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1144 if (!xlate) {
1145 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
1146 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001147 }
1148
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001149 buswidth = xlate->buswidth;
1150 host_fmt = xlate->host_fmt;
1151 cam_fmt = xlate->cam_fmt;
1152
1153 switch (pixfmt) {
1154 case 0: /* Only geometry change */
1155 ret = icd->ops->set_fmt(icd, pixfmt, rect);
1156 break;
1157 default:
1158 ret = icd->ops->set_fmt(icd, cam_fmt->fourcc, rect);
1159 }
1160
1161 if (ret < 0)
1162 dev_warn(&ici->dev, "Failed to configure for format %x\n",
1163 pixfmt);
1164
1165 if (pixfmt && !ret) {
1166 icd->buswidth = buswidth;
1167 icd->current_fmt = host_fmt;
1168 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001169
1170 return ret;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001171}
1172
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001173static int pxa_camera_try_fmt(struct soc_camera_device *icd,
1174 struct v4l2_format *f)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001175{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001176 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1177 const struct soc_camera_format_xlate *xlate;
1178 struct v4l2_pix_format *pix = &f->fmt.pix;
1179 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001180
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001181 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1182 if (!xlate) {
1183 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001184 return -EINVAL;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001185 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001186
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001187 /* limit to pxa hardware capabilities */
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001188 if (pix->height < 32)
1189 pix->height = 32;
1190 if (pix->height > 2048)
1191 pix->height = 2048;
1192 if (pix->width < 48)
1193 pix->width = 48;
1194 if (pix->width > 2048)
1195 pix->width = 2048;
1196 pix->width &= ~0x01;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001197
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001198 pix->bytesperline = pix->width *
1199 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1200 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001201
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001202 /* limit to sensor capabilities */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001203 return icd->ops->try_fmt(icd, f);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001204}
1205
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001206static int pxa_camera_reqbufs(struct soc_camera_file *icf,
1207 struct v4l2_requestbuffers *p)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001208{
1209 int i;
1210
1211 /* This is for locking debugging only. I removed spinlocks and now I
1212 * check whether .prepare is ever called on a linked buffer, or whether
1213 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1214 * it hadn't triggered */
1215 for (i = 0; i < p->count; i++) {
1216 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1217 struct pxa_buffer, vb);
1218 buf->inwork = 0;
1219 INIT_LIST_HEAD(&buf->vb.queue);
1220 }
1221
1222 return 0;
1223}
1224
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001225static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001226{
1227 struct soc_camera_file *icf = file->private_data;
1228 struct pxa_buffer *buf;
1229
1230 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
1231 vb.stream);
1232
1233 poll_wait(file, &buf->vb.done, pt);
1234
1235 if (buf->vb.state == VIDEOBUF_DONE ||
1236 buf->vb.state == VIDEOBUF_ERROR)
1237 return POLLIN|POLLRDNORM;
1238
1239 return 0;
1240}
1241
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001242static int pxa_camera_querycap(struct soc_camera_host *ici,
1243 struct v4l2_capability *cap)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001244{
1245 /* cap->name is set by the firendly caller:-> */
1246 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1247 cap->version = PXA_CAM_VERSION_CODE;
1248 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1249
1250 return 0;
1251}
1252
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001253static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1254{
1255 struct soc_camera_host *ici =
1256 to_soc_camera_host(icd->dev.parent);
1257 struct pxa_camera_dev *pcdev = ici->priv;
1258 int i = 0, ret = 0;
1259
Eric Miao5ca11fa2008-12-18 11:15:50 -03001260 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
1261 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
1262 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
1263 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
1264 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001265
1266 if ((pcdev->icd) && (pcdev->icd->ops->suspend))
1267 ret = pcdev->icd->ops->suspend(pcdev->icd, state);
1268
1269 return ret;
1270}
1271
1272static int pxa_camera_resume(struct soc_camera_device *icd)
1273{
1274 struct soc_camera_host *ici =
1275 to_soc_camera_host(icd->dev.parent);
1276 struct pxa_camera_dev *pcdev = ici->priv;
1277 int i = 0, ret = 0;
1278
Eric Miao87f3dd72008-09-08 15:26:43 +08001279 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1280 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1281 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001282
Eric Miao5ca11fa2008-12-18 11:15:50 -03001283 __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
1284 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
1285 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
1286 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
1287 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001288
1289 if ((pcdev->icd) && (pcdev->icd->ops->resume))
1290 ret = pcdev->icd->ops->resume(pcdev->icd);
1291
1292 /* Restart frame capture if active buffer exists */
1293 if (!ret && pcdev->active) {
Eric Miao5ca11fa2008-12-18 11:15:50 -03001294 unsigned long cifr, cicr0;
1295
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001296 /* Reset the FIFOs */
Eric Miao5ca11fa2008-12-18 11:15:50 -03001297 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
1298 __raw_writel(cifr, pcdev->base + CIFR);
1299
1300 cicr0 = __raw_readl(pcdev->base + CICR0);
1301 cicr0 &= ~CICR0_EOFM; /* Enable End-Of-Frame Interrupt */
1302 cicr0 |= CICR0_ENB; /* Restart the Capture Interface */
1303 __raw_writel(cicr0, pcdev->base + CICR0);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001304 }
1305
1306 return ret;
1307}
1308
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001309static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1310 .owner = THIS_MODULE,
1311 .add = pxa_camera_add_device,
1312 .remove = pxa_camera_remove_device,
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001313 .suspend = pxa_camera_suspend,
1314 .resume = pxa_camera_resume,
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001315 .get_formats = pxa_camera_get_formats,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001316 .set_fmt = pxa_camera_set_fmt,
1317 .try_fmt = pxa_camera_try_fmt,
Paulius Zaleckas092d3922008-07-11 20:50:31 -03001318 .init_videobuf = pxa_camera_init_videobuf,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001319 .reqbufs = pxa_camera_reqbufs,
1320 .poll = pxa_camera_poll,
1321 .querycap = pxa_camera_querycap,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001322 .set_bus_param = pxa_camera_set_bus_param,
1323};
1324
1325/* Should be allocated dynamically too, but we have only one. */
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001326static struct soc_camera_host pxa_soc_camera_host = {
1327 .drv_name = PXA_CAM_DRV_NAME,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001328 .ops = &pxa_soc_camera_host_ops,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001329};
1330
1331static int pxa_camera_probe(struct platform_device *pdev)
1332{
1333 struct pxa_camera_dev *pcdev;
1334 struct resource *res;
1335 void __iomem *base;
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001336 int irq;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001337 int err = 0;
1338
1339 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1340 irq = platform_get_irq(pdev, 0);
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001341 if (!res || irq < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001342 err = -ENODEV;
1343 goto exit;
1344 }
1345
1346 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1347 if (!pcdev) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001348 dev_err(&pdev->dev, "Could not allocate pcdev\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001349 err = -ENOMEM;
1350 goto exit;
1351 }
1352
1353 pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
1354 if (IS_ERR(pcdev->clk)) {
1355 err = PTR_ERR(pcdev->clk);
1356 goto exit_kfree;
1357 }
1358
1359 dev_set_drvdata(&pdev->dev, pcdev);
1360 pcdev->res = res;
1361
1362 pcdev->pdata = pdev->dev.platform_data;
1363 pcdev->platform_flags = pcdev->pdata->flags;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001364 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1365 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001366 /* Platform hasn't set available data widths. This is bad.
1367 * Warn and use a default. */
1368 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1369 "data widths, using default 10 bit\n");
1370 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1371 }
1372 pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
1373 if (!pcdev->platform_mclk_10khz) {
1374 dev_warn(&pdev->dev,
1375 "mclk_10khz == 0! Please, fix your platform data. "
1376 "Using default 20MHz\n");
1377 pcdev->platform_mclk_10khz = 2000;
1378 }
1379
1380 INIT_LIST_HEAD(&pcdev->capture);
1381 spin_lock_init(&pcdev->lock);
1382
1383 /*
1384 * Request the regions.
1385 */
1386 if (!request_mem_region(res->start, res->end - res->start + 1,
1387 PXA_CAM_DRV_NAME)) {
1388 err = -EBUSY;
1389 goto exit_clk;
1390 }
1391
1392 base = ioremap(res->start, res->end - res->start + 1);
1393 if (!base) {
1394 err = -ENOMEM;
1395 goto exit_release;
1396 }
1397 pcdev->irq = irq;
1398 pcdev->base = base;
1399 pcdev->dev = &pdev->dev;
1400
1401 /* request dma */
roel kluinde3e3b82008-09-18 17:50:15 -03001402 err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1403 pxa_camera_dma_irq_y, pcdev);
1404 if (err < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001405 dev_err(pcdev->dev, "Can't request DMA for Y\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001406 goto exit_iounmap;
1407 }
roel kluinde3e3b82008-09-18 17:50:15 -03001408 pcdev->dma_chans[0] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001409 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001410
roel kluinde3e3b82008-09-18 17:50:15 -03001411 err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1412 pxa_camera_dma_irq_u, pcdev);
1413 if (err < 0) {
Mike Rapoporta5462e52008-04-22 10:36:32 -03001414 dev_err(pcdev->dev, "Can't request DMA for U\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001415 goto exit_free_dma_y;
1416 }
roel kluinde3e3b82008-09-18 17:50:15 -03001417 pcdev->dma_chans[1] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001418 dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1419
roel kluinde3e3b82008-09-18 17:50:15 -03001420 err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1421 pxa_camera_dma_irq_v, pcdev);
1422 if (err < 0) {
Mike Rapoporta5462e52008-04-22 10:36:32 -03001423 dev_err(pcdev->dev, "Can't request DMA for V\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001424 goto exit_free_dma_u;
1425 }
roel kluinde3e3b82008-09-18 17:50:15 -03001426 pcdev->dma_chans[2] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001427 dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
1428
Eric Miao87f3dd72008-09-08 15:26:43 +08001429 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1430 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1431 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001432
1433 /* request irq */
1434 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1435 pcdev);
1436 if (err) {
1437 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1438 goto exit_free_dma;
1439 }
1440
1441 pxa_soc_camera_host.priv = pcdev;
1442 pxa_soc_camera_host.dev.parent = &pdev->dev;
1443 pxa_soc_camera_host.nr = pdev->id;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001444 err = soc_camera_host_register(&pxa_soc_camera_host);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001445 if (err)
1446 goto exit_free_irq;
1447
1448 return 0;
1449
1450exit_free_irq:
1451 free_irq(pcdev->irq, pcdev);
1452exit_free_dma:
Mike Rapoporta5462e52008-04-22 10:36:32 -03001453 pxa_free_dma(pcdev->dma_chans[2]);
1454exit_free_dma_u:
1455 pxa_free_dma(pcdev->dma_chans[1]);
1456exit_free_dma_y:
1457 pxa_free_dma(pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001458exit_iounmap:
1459 iounmap(base);
1460exit_release:
1461 release_mem_region(res->start, res->end - res->start + 1);
1462exit_clk:
1463 clk_put(pcdev->clk);
1464exit_kfree:
1465 kfree(pcdev);
1466exit:
1467 return err;
1468}
1469
1470static int __devexit pxa_camera_remove(struct platform_device *pdev)
1471{
1472 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1473 struct resource *res;
1474
1475 clk_put(pcdev->clk);
1476
Mike Rapoporta5462e52008-04-22 10:36:32 -03001477 pxa_free_dma(pcdev->dma_chans[0]);
1478 pxa_free_dma(pcdev->dma_chans[1]);
1479 pxa_free_dma(pcdev->dma_chans[2]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001480 free_irq(pcdev->irq, pcdev);
1481
1482 soc_camera_host_unregister(&pxa_soc_camera_host);
1483
1484 iounmap(pcdev->base);
1485
1486 res = pcdev->res;
1487 release_mem_region(res->start, res->end - res->start + 1);
1488
1489 kfree(pcdev);
1490
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001491 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001492
1493 return 0;
1494}
1495
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001496static struct platform_driver pxa_camera_driver = {
1497 .driver = {
1498 .name = PXA_CAM_DRV_NAME,
1499 },
1500 .probe = pxa_camera_probe,
1501 .remove = __exit_p(pxa_camera_remove),
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001502};
1503
1504
1505static int __devinit pxa_camera_init(void)
1506{
1507 return platform_driver_register(&pxa_camera_driver);
1508}
1509
1510static void __exit pxa_camera_exit(void)
1511{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001512 platform_driver_unregister(&pxa_camera_driver);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001513}
1514
1515module_init(pxa_camera_init);
1516module_exit(pxa_camera_exit);
1517
1518MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1519MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1520MODULE_LICENSE("GPL");