blob: c3a7d476dc4bc2a663cd9f5f8ab898c4897c7536 [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>
39#include <asm/arch/pxa-regs.h>
40#include <asm/arch/camera.h>
41
42#define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
43#define PXA_CAM_DRV_NAME "pxa27x-camera"
44
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -030045#define CICR0_SIM_MP (0 << 24)
46#define CICR0_SIM_SP (1 << 24)
47#define CICR0_SIM_MS (2 << 24)
48#define CICR0_SIM_EP (3 << 24)
49#define CICR0_SIM_ES (4 << 24)
50
51#define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
52#define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
Mike Rapoporta5462e52008-04-22 10:36:32 -030053#define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
54#define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
55#define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -030056
57#define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
58#define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
59#define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
60#define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
61#define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
62
63#define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
64#define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
65#define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
66#define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
67
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030068#define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
69 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
70 CICR0_EOFM | CICR0_FOM)
71
72static DEFINE_MUTEX(camera_lock);
73
74/*
75 * Structures
76 */
Mike Rapoporta5462e52008-04-22 10:36:32 -030077enum pxa_camera_active_dma {
78 DMA_Y = 0x1,
79 DMA_U = 0x2,
80 DMA_V = 0x4,
81};
82
83/* descriptor needed for the PXA DMA engine */
84struct pxa_cam_dma {
85 dma_addr_t sg_dma;
86 struct pxa_dma_desc *sg_cpu;
87 size_t sg_size;
88 int sglen;
89};
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030090
91/* buffer for one video frame */
92struct pxa_buffer {
93 /* common v4l buffer stuff -- must be first */
94 struct videobuf_buffer vb;
95
96 const struct soc_camera_data_format *fmt;
97
Mike Rapoporta5462e52008-04-22 10:36:32 -030098 /* our descriptor lists for Y, U and V channels */
99 struct pxa_cam_dma dmas[3];
100
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300101 int inwork;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300102
103 enum pxa_camera_active_dma active_dma;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300104};
105
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300106struct pxa_camera_dev {
107 struct device *dev;
108 /* PXA27x is only supposed to handle one camera on its Quick Capture
109 * interface. If anyone ever builds hardware to enable more than
110 * one camera, they will have to modify this driver too */
111 struct soc_camera_device *icd;
112 struct clk *clk;
113
114 unsigned int irq;
115 void __iomem *base;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300116
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300117 int channels;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300118 unsigned int dma_chans[3];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300119
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300120 struct pxacamera_platform_data *pdata;
121 struct resource *res;
122 unsigned long platform_flags;
123 unsigned long platform_mclk_10khz;
124
125 struct list_head capture;
126
127 spinlock_t lock;
128
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300129 struct pxa_buffer *active;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300130 struct pxa_dma_desc *sg_tail[3];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300131};
132
133static const char *pxa_cam_driver_description = "PXA_Camera";
134
135static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
136
137/*
138 * Videobuf operations
139 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300140static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
141 unsigned int *size)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300142{
143 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300144 struct soc_camera_host *ici =
145 to_soc_camera_host(icd->dev.parent);
146 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300147
148 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
149
Mike Rapoporta5462e52008-04-22 10:36:32 -0300150 /* planar capture requires Y, U and V buffers to be page aligned */
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300151 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300152 *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
153 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
154 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
155 } else {
156 *size = icd->width * icd->height *
157 ((icd->current_fmt->depth + 7) >> 3);
158 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300159
160 if (0 == *count)
161 *count = 32;
162 while (*size * *count > vid_limit * 1024 * 1024)
163 (*count)--;
164
165 return 0;
166}
167
168static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
169{
170 struct soc_camera_device *icd = vq->priv_data;
171 struct soc_camera_host *ici =
172 to_soc_camera_host(icd->dev.parent);
173 struct pxa_camera_dev *pcdev = ici->priv;
174 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300175 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300176
177 BUG_ON(in_interrupt());
178
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300179 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300180 &buf->vb, buf->vb.baddr, buf->vb.bsize);
181
182 /* This waits until this buffer is out of danger, i.e., until it is no
183 * longer in STATE_QUEUED or STATE_ACTIVE */
184 videobuf_waiton(&buf->vb, 0, 0);
185 videobuf_dma_unmap(vq, dma);
186 videobuf_dma_free(dma);
187
Mike Rapoporta5462e52008-04-22 10:36:32 -0300188 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
189 if (buf->dmas[i].sg_cpu)
190 dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
191 buf->dmas[i].sg_cpu,
192 buf->dmas[i].sg_dma);
193 buf->dmas[i].sg_cpu = NULL;
194 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300195
196 buf->vb.state = VIDEOBUF_NEEDS_INIT;
197}
198
Mike Rapoporta5462e52008-04-22 10:36:32 -0300199static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
200 struct pxa_buffer *buf,
201 struct videobuf_dmabuf *dma, int channel,
202 int sglen, int sg_start, int cibr,
203 unsigned int size)
204{
205 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
206 int i;
207
208 if (pxa_dma->sg_cpu)
209 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
210 pxa_dma->sg_cpu, pxa_dma->sg_dma);
211
212 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
213 pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
214 &pxa_dma->sg_dma, GFP_KERNEL);
215 if (!pxa_dma->sg_cpu)
216 return -ENOMEM;
217
218 pxa_dma->sglen = sglen;
219
220 for (i = 0; i < sglen; i++) {
221 int sg_i = sg_start + i;
222 struct scatterlist *sg = dma->sglist;
223 unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
224
225 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
226 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
227
228 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
229 xfer_len = (min(dma_len, size) + 7) & ~7;
230
231 pxa_dma->sg_cpu[i].dcmd =
232 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
233 size -= dma_len;
234 pxa_dma->sg_cpu[i].ddadr =
235 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
236 }
237
238 pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
239 pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
240
241 return 0;
242}
243
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300244static int pxa_videobuf_prepare(struct videobuf_queue *vq,
245 struct videobuf_buffer *vb, enum v4l2_field field)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300246{
247 struct soc_camera_device *icd = vq->priv_data;
248 struct soc_camera_host *ici =
249 to_soc_camera_host(icd->dev.parent);
250 struct pxa_camera_dev *pcdev = ici->priv;
251 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300252 int ret;
253 int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
254 int size_y, size_u = 0, size_v = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300255
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300256 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300257 vb, vb->baddr, vb->bsize);
258
259 /* Added list head initialization on alloc */
260 WARN_ON(!list_empty(&vb->queue));
261
262#ifdef DEBUG
263 /* This can be useful if you want to see if we actually fill
264 * the buffer with something */
265 memset((void *)vb->baddr, 0xaa, vb->bsize);
266#endif
267
268 BUG_ON(NULL == icd->current_fmt);
269
270 /* I think, in buf_prepare you only have to protect global data,
271 * the actual buffer is yours */
272 buf->inwork = 1;
273
274 if (buf->fmt != icd->current_fmt ||
275 vb->width != icd->width ||
276 vb->height != icd->height ||
277 vb->field != field) {
278 buf->fmt = icd->current_fmt;
279 vb->width = icd->width;
280 vb->height = icd->height;
281 vb->field = field;
282 vb->state = VIDEOBUF_NEEDS_INIT;
283 }
284
285 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
286 if (0 != vb->baddr && vb->bsize < vb->size) {
287 ret = -EINVAL;
288 goto out;
289 }
290
291 if (vb->state == VIDEOBUF_NEEDS_INIT) {
292 unsigned int size = vb->size;
293 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
294
295 ret = videobuf_iolock(vq, vb, NULL);
296 if (ret)
297 goto fail;
298
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300299 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300300 /* FIXME the calculations should be more precise */
301 sglen_y = dma->sglen / 2;
302 sglen_u = sglen_v = dma->sglen / 4 + 1;
303 sglen_yu = sglen_y + sglen_u;
304 size_y = size / 2;
305 size_u = size_v = size / 4;
306 } else {
307 sglen_y = dma->sglen;
308 size_y = size;
309 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300310
Mike Rapoporta5462e52008-04-22 10:36:32 -0300311 /* init DMA for Y channel */
312 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
313 0, 0x28, size_y);
314
315 if (ret) {
316 dev_err(pcdev->dev,
317 "DMA initialization for Y/RGB failed\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300318 goto fail;
319 }
320
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300321 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300322 /* init DMA for U channel */
323 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
324 sglen_y, 0x30, size_u);
325 if (ret) {
326 dev_err(pcdev->dev,
327 "DMA initialization for U failed\n");
328 goto fail_u;
329 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300330
Mike Rapoporta5462e52008-04-22 10:36:32 -0300331 /* init DMA for V channel */
332 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
333 sglen_yu, 0x38, size_v);
334 if (ret) {
335 dev_err(pcdev->dev,
336 "DMA initialization for V failed\n");
337 goto fail_v;
338 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300339 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300340
341 vb->state = VIDEOBUF_PREPARED;
342 }
343
344 buf->inwork = 0;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300345 buf->active_dma = DMA_Y;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300346 if (pcdev->channels == 3)
Mike Rapoporta5462e52008-04-22 10:36:32 -0300347 buf->active_dma |= DMA_U | DMA_V;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300348
349 return 0;
350
Mike Rapoporta5462e52008-04-22 10:36:32 -0300351fail_v:
352 dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
353 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
354fail_u:
355 dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
356 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300357fail:
358 free_buffer(vq, buf);
359out:
360 buf->inwork = 0;
361 return ret;
362}
363
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300364static void pxa_videobuf_queue(struct videobuf_queue *vq,
365 struct videobuf_buffer *vb)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300366{
367 struct soc_camera_device *icd = vq->priv_data;
368 struct soc_camera_host *ici =
369 to_soc_camera_host(icd->dev.parent);
370 struct pxa_camera_dev *pcdev = ici->priv;
371 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300372 struct pxa_buffer *active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300373 unsigned long flags;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300374 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300375
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300376 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300377 vb, vb->baddr, vb->bsize);
378 spin_lock_irqsave(&pcdev->lock, flags);
379
380 list_add_tail(&vb->queue, &pcdev->capture);
381
382 vb->state = VIDEOBUF_ACTIVE;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300383 active = pcdev->active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300384
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300385 if (!active) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300386 CIFR |= CIFR_RESET_F;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300387
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300388 for (i = 0; i < pcdev->channels; i++) {
389 DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
390 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
391 pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300392 }
393
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300394 pcdev->active = buf;
395 CICR0 |= CICR0_ENB;
396 } else {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300397 struct pxa_cam_dma *buf_dma;
398 struct pxa_cam_dma *act_dma;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300399 int nents;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300400
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300401 for (i = 0; i < pcdev->channels; i++) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300402 buf_dma = &buf->dmas[i];
403 act_dma = &active->dmas[i];
404 nents = buf_dma->sglen;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300405
Mike Rapoporta5462e52008-04-22 10:36:32 -0300406 /* Stop DMA engine */
407 DCSR(pcdev->dma_chans[i]) = 0;
408
409 /* Add the descriptors we just initialized to
410 the currently running chain */
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300411 pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
412 pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300413
414 /* Setup a dummy descriptor with the DMA engines current
415 * state
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300416 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300417 buf_dma->sg_cpu[nents].dsadr =
418 pcdev->res->start + 0x28 + i*8; /* CIBRx */
419 buf_dma->sg_cpu[nents].dtadr =
420 DTADR(pcdev->dma_chans[i]);
421 buf_dma->sg_cpu[nents].dcmd =
422 DCMD(pcdev->dma_chans[i]);
423
424 if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
425 /* The DMA engine is on the last
426 descriptor, set the next descriptors
427 address to the descriptors we just
428 initialized */
429 buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
430 } else {
431 buf_dma->sg_cpu[nents].ddadr =
432 DDADR(pcdev->dma_chans[i]);
433 }
434
435 /* The next descriptor is the dummy descriptor */
436 DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
437 sizeof(struct pxa_dma_desc);
438
439 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300440 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300441 }
442
443 spin_unlock_irqrestore(&pcdev->lock, flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300444}
445
446static void pxa_videobuf_release(struct videobuf_queue *vq,
447 struct videobuf_buffer *vb)
448{
449 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
450#ifdef DEBUG
451 struct soc_camera_device *icd = vq->priv_data;
452
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300453 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300454 vb, vb->baddr, vb->bsize);
455
456 switch (vb->state) {
457 case VIDEOBUF_ACTIVE:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300458 dev_dbg(&icd->dev, "%s (active)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300459 break;
460 case VIDEOBUF_QUEUED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300461 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300462 break;
463 case VIDEOBUF_PREPARED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300464 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300465 break;
466 default:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300467 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300468 break;
469 }
470#endif
471
472 free_buffer(vq, buf);
473}
474
Mike Rapoporta5462e52008-04-22 10:36:32 -0300475static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
476 struct videobuf_buffer *vb,
477 struct pxa_buffer *buf)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300478{
Mike Rapoporta5462e52008-04-22 10:36:32 -0300479 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
480 list_del_init(&vb->queue);
481 vb->state = VIDEOBUF_DONE;
482 do_gettimeofday(&vb->ts);
483 vb->field_count++;
484 wake_up(&vb->done);
485
486 if (list_empty(&pcdev->capture)) {
487 pcdev->active = NULL;
488 DCSR(pcdev->dma_chans[0]) = 0;
489 DCSR(pcdev->dma_chans[1]) = 0;
490 DCSR(pcdev->dma_chans[2]) = 0;
491 CICR0 &= ~CICR0_ENB;
492 return;
493 }
494
495 pcdev->active = list_entry(pcdev->capture.next,
496 struct pxa_buffer, vb.queue);
497}
498
499static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
500 enum pxa_camera_active_dma act_dma)
501{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300502 struct pxa_buffer *buf;
503 unsigned long flags;
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300504 u32 status, camera_status, overrun;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300505 struct videobuf_buffer *vb;
506
507 spin_lock_irqsave(&pcdev->lock, flags);
508
Mike Rapoporta5462e52008-04-22 10:36:32 -0300509 status = DCSR(channel);
510 DCSR(channel) = status | DCSR_ENDINTR;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300511
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300512 if (status & DCSR_BUSERR) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300513 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300514 goto out;
515 }
516
517 if (!(status & DCSR_ENDINTR)) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300518 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
519 "status: 0x%08x\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300520 goto out;
521 }
522
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300523 if (!pcdev->active) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300524 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300525 goto out;
526 }
527
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300528 camera_status = CISR;
529 overrun = CISR_IFO_0;
530 if (pcdev->channels == 3)
531 overrun |= CISR_IFO_1 | CISR_IFO_2;
532 if (camera_status & overrun) {
533 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
534 /* Stop the Capture Interface */
535 CICR0 &= ~CICR0_ENB;
536 /* Stop DMA */
537 DCSR(channel) = 0;
538 /* Reset the FIFOs */
539 CIFR |= CIFR_RESET_F;
540 /* Enable End-Of-Frame Interrupt */
541 CICR0 &= ~CICR0_EOFM;
542 /* Restart the Capture Interface */
543 CICR0 |= CICR0_ENB;
544 goto out;
545 }
546
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300547 vb = &pcdev->active->vb;
548 buf = container_of(vb, struct pxa_buffer, vb);
549 WARN_ON(buf->inwork || list_empty(&vb->queue));
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300550 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300551 vb, vb->baddr, vb->bsize);
552
Mike Rapoporta5462e52008-04-22 10:36:32 -0300553 buf->active_dma &= ~act_dma;
554 if (!buf->active_dma)
555 pxa_camera_wakeup(pcdev, vb, buf);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300556
557out:
558 spin_unlock_irqrestore(&pcdev->lock, flags);
559}
560
Mike Rapoporta5462e52008-04-22 10:36:32 -0300561static void pxa_camera_dma_irq_y(int channel, void *data)
562{
563 struct pxa_camera_dev *pcdev = data;
564 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
565}
566
567static void pxa_camera_dma_irq_u(int channel, void *data)
568{
569 struct pxa_camera_dev *pcdev = data;
570 pxa_camera_dma_irq(channel, pcdev, DMA_U);
571}
572
573static void pxa_camera_dma_irq_v(int channel, void *data)
574{
575 struct pxa_camera_dev *pcdev = data;
576 pxa_camera_dma_irq(channel, pcdev, DMA_V);
577}
578
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300579static struct videobuf_queue_ops pxa_videobuf_ops = {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300580 .buf_setup = pxa_videobuf_setup,
581 .buf_prepare = pxa_videobuf_prepare,
582 .buf_queue = pxa_videobuf_queue,
583 .buf_release = pxa_videobuf_release,
584};
585
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300586static void pxa_camera_init_videobuf(struct videobuf_queue *q, spinlock_t *lock,
587 struct soc_camera_device *icd)
588{
589 /* We must pass NULL as dev pointer, then all pci_* dma operations
590 * transform to normal dma_* ones. */
591 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, lock,
592 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
593 sizeof(struct pxa_buffer), icd);
594}
595
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300596static int mclk_get_divisor(struct pxa_camera_dev *pcdev)
597{
598 unsigned int mclk_10khz = pcdev->platform_mclk_10khz;
599 unsigned long div;
600 unsigned long lcdclk;
601
602 lcdclk = clk_get_rate(pcdev->clk) / 10000;
603
604 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
605 * they get a nice Oops */
606 div = (lcdclk + 2 * mclk_10khz - 1) / (2 * mclk_10khz) - 1;
607
608 dev_dbg(pcdev->dev, "LCD clock %lukHz, target freq %dkHz, "
609 "divisor %lu\n", lcdclk * 10, mclk_10khz * 10, div);
610
611 return div;
612}
613
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300614static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300615{
616 struct pxacamera_platform_data *pdata = pcdev->pdata;
617 u32 cicr4 = 0;
618
619 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
620 pcdev, pdata);
621
622 if (pdata && pdata->init) {
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300623 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300624 pdata->init(pcdev->dev);
625 }
626
627 if (pdata && pdata->power) {
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300628 dev_dbg(pcdev->dev, "%s: Power on camera\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300629 pdata->power(pcdev->dev, 1);
630 }
631
632 if (pdata && pdata->reset) {
633 dev_dbg(pcdev->dev, "%s: Releasing camera reset\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300634 __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300635 pdata->reset(pcdev->dev, 1);
636 }
637
638 CICR0 = 0x3FF; /* disable all interrupts */
639
640 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
641 cicr4 |= CICR4_PCLK_EN;
642 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
643 cicr4 |= CICR4_MCLK_EN;
644 if (pcdev->platform_flags & PXA_CAMERA_PCP)
645 cicr4 |= CICR4_PCP;
646 if (pcdev->platform_flags & PXA_CAMERA_HSP)
647 cicr4 |= CICR4_HSP;
648 if (pcdev->platform_flags & PXA_CAMERA_VSP)
649 cicr4 |= CICR4_VSP;
650
651 CICR4 = mclk_get_divisor(pcdev) | cicr4;
652
653 clk_enable(pcdev->clk);
654}
655
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300656static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300657{
658 struct pxacamera_platform_data *board = pcdev->pdata;
659
660 clk_disable(pcdev->clk);
661
662 if (board && board->reset) {
663 dev_dbg(pcdev->dev, "%s: Asserting camera reset\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300664 __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300665 board->reset(pcdev->dev, 0);
666 }
667
668 if (board && board->power) {
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300669 dev_dbg(pcdev->dev, "%s: Power off camera\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300670 board->power(pcdev->dev, 0);
671 }
672}
673
674static irqreturn_t pxa_camera_irq(int irq, void *data)
675{
676 struct pxa_camera_dev *pcdev = data;
677 unsigned int status = CISR;
678
679 dev_dbg(pcdev->dev, "Camera interrupt status 0x%x\n", status);
680
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300681 if (!status)
682 return IRQ_NONE;
683
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300684 CISR = status;
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300685
686 if (status & CISR_EOF) {
687 int i;
688 for (i = 0; i < pcdev->channels; i++) {
689 DDADR(pcdev->dma_chans[i]) =
690 pcdev->active->dmas[i].sg_dma;
691 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
692 }
693 CICR0 |= CICR0_EOFM;
694 }
695
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300696 return IRQ_HANDLED;
697}
698
699/* The following two functions absolutely depend on the fact, that
700 * there can be only one camera on PXA quick capture interface */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300701static int pxa_camera_add_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300702{
703 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
704 struct pxa_camera_dev *pcdev = ici->priv;
705 int ret;
706
707 mutex_lock(&camera_lock);
708
709 if (pcdev->icd) {
710 ret = -EBUSY;
711 goto ebusy;
712 }
713
714 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
715 icd->devnum);
716
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300717 pxa_camera_activate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300718 ret = icd->ops->init(icd);
719
720 if (!ret)
721 pcdev->icd = icd;
722
723ebusy:
724 mutex_unlock(&camera_lock);
725
726 return ret;
727}
728
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300729static void pxa_camera_remove_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300730{
731 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
732 struct pxa_camera_dev *pcdev = ici->priv;
733
734 BUG_ON(icd != pcdev->icd);
735
736 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
737 icd->devnum);
738
739 /* disable capture, disable interrupts */
740 CICR0 = 0x3ff;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300741
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300742 /* Stop DMA engine */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300743 DCSR(pcdev->dma_chans[0]) = 0;
744 DCSR(pcdev->dma_chans[1]) = 0;
745 DCSR(pcdev->dma_chans[2]) = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300746
747 icd->ops->release(icd);
748
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300749 pxa_camera_deactivate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300750
751 pcdev->icd = NULL;
752}
753
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300754static int test_platform_param(struct pxa_camera_dev *pcdev,
755 unsigned char buswidth, unsigned long *flags)
756{
757 /*
758 * Platform specified synchronization and pixel clock polarities are
759 * only a recommendation and are only used during probing. The PXA270
760 * quick capture interface supports both.
761 */
762 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
763 SOCAM_MASTER : SOCAM_SLAVE) |
764 SOCAM_HSYNC_ACTIVE_HIGH |
765 SOCAM_HSYNC_ACTIVE_LOW |
766 SOCAM_VSYNC_ACTIVE_HIGH |
767 SOCAM_VSYNC_ACTIVE_LOW |
768 SOCAM_PCLK_SAMPLE_RISING |
769 SOCAM_PCLK_SAMPLE_FALLING;
770
771 /* If requested data width is supported by the platform, use it */
772 switch (buswidth) {
773 case 10:
774 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
775 return -EINVAL;
776 *flags |= SOCAM_DATAWIDTH_10;
777 break;
778 case 9:
779 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
780 return -EINVAL;
781 *flags |= SOCAM_DATAWIDTH_9;
782 break;
783 case 8:
784 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
785 return -EINVAL;
786 *flags |= SOCAM_DATAWIDTH_8;
787 }
788
789 return 0;
790}
791
792static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300793{
794 struct soc_camera_host *ici =
795 to_soc_camera_host(icd->dev.parent);
796 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300797 unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300798 u32 cicr0, cicr1, cicr4 = 0;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300799 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300800
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300801 if (ret < 0)
802 return ret;
803
804 camera_flags = icd->ops->query_bus_param(icd);
805
806 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
807 if (!common_flags)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300808 return -EINVAL;
809
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300810 pcdev->channels = 1;
811
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300812 /* Make choises, based on platform preferences */
813 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
814 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
815 if (pcdev->platform_flags & PXA_CAMERA_HSP)
816 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
817 else
818 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
819 }
820
821 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
822 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
823 if (pcdev->platform_flags & PXA_CAMERA_VSP)
824 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
825 else
826 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
827 }
828
829 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
830 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
831 if (pcdev->platform_flags & PXA_CAMERA_PCP)
832 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
833 else
834 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
835 }
836
837 ret = icd->ops->set_bus_param(icd, common_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300838 if (ret < 0)
839 return ret;
840
841 /* Datawidth is now guaranteed to be equal to one of the three values.
842 * We fix bit-per-pixel equal to data-width... */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300843 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
844 case SOCAM_DATAWIDTH_10:
845 icd->buswidth = 10;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300846 dw = 4;
847 bpp = 0x40;
848 break;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300849 case SOCAM_DATAWIDTH_9:
850 icd->buswidth = 9;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300851 dw = 3;
852 bpp = 0x20;
853 break;
854 default:
855 /* Actually it can only be 8 now,
856 * default is just to silence compiler warnings */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300857 case SOCAM_DATAWIDTH_8:
858 icd->buswidth = 8;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300859 dw = 2;
860 bpp = 0;
861 }
862
863 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
864 cicr4 |= CICR4_PCLK_EN;
865 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
866 cicr4 |= CICR4_MCLK_EN;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300867 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300868 cicr4 |= CICR4_PCP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300869 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300870 cicr4 |= CICR4_HSP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300871 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300872 cicr4 |= CICR4_VSP;
873
874 cicr0 = CICR0;
875 if (cicr0 & CICR0_ENB)
876 CICR0 = cicr0 & ~CICR0_ENB;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300877
878 cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
879
880 switch (pixfmt) {
881 case V4L2_PIX_FMT_YUV422P:
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300882 pcdev->channels = 3;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300883 cicr1 |= CICR1_YCBCR_F;
884 case V4L2_PIX_FMT_YUYV:
885 cicr1 |= CICR1_COLOR_SP_VAL(2);
886 break;
887 case V4L2_PIX_FMT_RGB555:
888 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
889 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
890 break;
891 case V4L2_PIX_FMT_RGB565:
892 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
893 break;
894 }
895
896 CICR1 = cicr1;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300897 CICR2 = 0;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300898 CICR3 = CICR3_LPF_VAL(icd->height - 1) |
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300899 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
900 CICR4 = mclk_get_divisor(pcdev) | cicr4;
901
902 /* CIF interrupts are not used, only DMA */
903 CICR0 = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300904 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)) |
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300905 CICR0_DMAEN | CICR0_IRQ_MASK | (cicr0 & CICR0_ENB);
906
907 return 0;
908}
909
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300910static int pxa_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
911{
912 struct soc_camera_host *ici =
913 to_soc_camera_host(icd->dev.parent);
914 struct pxa_camera_dev *pcdev = ici->priv;
915 unsigned long bus_flags, camera_flags;
916 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
917
918 if (ret < 0)
919 return ret;
920
921 camera_flags = icd->ops->query_bus_param(icd);
922
923 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
924}
925
926static int pxa_camera_set_fmt_cap(struct soc_camera_device *icd,
927 __u32 pixfmt, struct v4l2_rect *rect)
928{
929 return icd->ops->set_fmt_cap(icd, pixfmt, rect);
930}
931
932static int pxa_camera_try_fmt_cap(struct soc_camera_device *icd,
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300933 struct v4l2_format *f)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300934{
935 /* limit to pxa hardware capabilities */
936 if (f->fmt.pix.height < 32)
937 f->fmt.pix.height = 32;
938 if (f->fmt.pix.height > 2048)
939 f->fmt.pix.height = 2048;
940 if (f->fmt.pix.width < 48)
941 f->fmt.pix.width = 48;
942 if (f->fmt.pix.width > 2048)
943 f->fmt.pix.width = 2048;
944 f->fmt.pix.width &= ~0x01;
945
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300946 /* limit to sensor capabilities */
947 return icd->ops->try_fmt_cap(icd, f);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300948}
949
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300950static int pxa_camera_reqbufs(struct soc_camera_file *icf,
951 struct v4l2_requestbuffers *p)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300952{
953 int i;
954
955 /* This is for locking debugging only. I removed spinlocks and now I
956 * check whether .prepare is ever called on a linked buffer, or whether
957 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
958 * it hadn't triggered */
959 for (i = 0; i < p->count; i++) {
960 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
961 struct pxa_buffer, vb);
962 buf->inwork = 0;
963 INIT_LIST_HEAD(&buf->vb.queue);
964 }
965
966 return 0;
967}
968
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300969static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300970{
971 struct soc_camera_file *icf = file->private_data;
972 struct pxa_buffer *buf;
973
974 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
975 vb.stream);
976
977 poll_wait(file, &buf->vb.done, pt);
978
979 if (buf->vb.state == VIDEOBUF_DONE ||
980 buf->vb.state == VIDEOBUF_ERROR)
981 return POLLIN|POLLRDNORM;
982
983 return 0;
984}
985
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300986static int pxa_camera_querycap(struct soc_camera_host *ici,
987 struct v4l2_capability *cap)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300988{
989 /* cap->name is set by the firendly caller:-> */
990 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
991 cap->version = PXA_CAM_VERSION_CODE;
992 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
993
994 return 0;
995}
996
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300997static spinlock_t *pxa_camera_spinlock_alloc(struct soc_camera_file *icf)
998{
999 struct soc_camera_host *ici =
1000 to_soc_camera_host(icf->icd->dev.parent);
1001 struct pxa_camera_dev *pcdev = ici->priv;
1002
1003 return &pcdev->lock;
1004}
1005
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001006static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1007 .owner = THIS_MODULE,
1008 .add = pxa_camera_add_device,
1009 .remove = pxa_camera_remove_device,
1010 .set_fmt_cap = pxa_camera_set_fmt_cap,
1011 .try_fmt_cap = pxa_camera_try_fmt_cap,
Paulius Zaleckas092d3922008-07-11 20:50:31 -03001012 .init_videobuf = pxa_camera_init_videobuf,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001013 .reqbufs = pxa_camera_reqbufs,
1014 .poll = pxa_camera_poll,
1015 .querycap = pxa_camera_querycap,
1016 .try_bus_param = pxa_camera_try_bus_param,
1017 .set_bus_param = pxa_camera_set_bus_param,
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -03001018 .spinlock_alloc = pxa_camera_spinlock_alloc,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001019};
1020
1021/* Should be allocated dynamically too, but we have only one. */
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001022static struct soc_camera_host pxa_soc_camera_host = {
1023 .drv_name = PXA_CAM_DRV_NAME,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001024 .ops = &pxa_soc_camera_host_ops,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001025};
1026
1027static int pxa_camera_probe(struct platform_device *pdev)
1028{
1029 struct pxa_camera_dev *pcdev;
1030 struct resource *res;
1031 void __iomem *base;
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001032 int irq;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001033 int err = 0;
1034
1035 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1036 irq = platform_get_irq(pdev, 0);
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001037 if (!res || irq < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001038 err = -ENODEV;
1039 goto exit;
1040 }
1041
1042 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1043 if (!pcdev) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001044 dev_err(&pdev->dev, "Could not allocate pcdev\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001045 err = -ENOMEM;
1046 goto exit;
1047 }
1048
1049 pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
1050 if (IS_ERR(pcdev->clk)) {
1051 err = PTR_ERR(pcdev->clk);
1052 goto exit_kfree;
1053 }
1054
1055 dev_set_drvdata(&pdev->dev, pcdev);
1056 pcdev->res = res;
1057
1058 pcdev->pdata = pdev->dev.platform_data;
1059 pcdev->platform_flags = pcdev->pdata->flags;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001060 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1061 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001062 /* Platform hasn't set available data widths. This is bad.
1063 * Warn and use a default. */
1064 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1065 "data widths, using default 10 bit\n");
1066 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1067 }
1068 pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
1069 if (!pcdev->platform_mclk_10khz) {
1070 dev_warn(&pdev->dev,
1071 "mclk_10khz == 0! Please, fix your platform data. "
1072 "Using default 20MHz\n");
1073 pcdev->platform_mclk_10khz = 2000;
1074 }
1075
1076 INIT_LIST_HEAD(&pcdev->capture);
1077 spin_lock_init(&pcdev->lock);
1078
1079 /*
1080 * Request the regions.
1081 */
1082 if (!request_mem_region(res->start, res->end - res->start + 1,
1083 PXA_CAM_DRV_NAME)) {
1084 err = -EBUSY;
1085 goto exit_clk;
1086 }
1087
1088 base = ioremap(res->start, res->end - res->start + 1);
1089 if (!base) {
1090 err = -ENOMEM;
1091 goto exit_release;
1092 }
1093 pcdev->irq = irq;
1094 pcdev->base = base;
1095 pcdev->dev = &pdev->dev;
1096
1097 /* request dma */
Mike Rapoporta5462e52008-04-22 10:36:32 -03001098 pcdev->dma_chans[0] = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1099 pxa_camera_dma_irq_y, pcdev);
1100 if (pcdev->dma_chans[0] < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001101 dev_err(pcdev->dev, "Can't request DMA for Y\n");
1102 err = -ENOMEM;
1103 goto exit_iounmap;
1104 }
Mike Rapoporta5462e52008-04-22 10:36:32 -03001105 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001106
Mike Rapoporta5462e52008-04-22 10:36:32 -03001107 pcdev->dma_chans[1] = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1108 pxa_camera_dma_irq_u, pcdev);
1109 if (pcdev->dma_chans[1] < 0) {
1110 dev_err(pcdev->dev, "Can't request DMA for U\n");
1111 err = -ENOMEM;
1112 goto exit_free_dma_y;
1113 }
1114 dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1115
1116 pcdev->dma_chans[2] = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1117 pxa_camera_dma_irq_v, pcdev);
1118 if (pcdev->dma_chans[0] < 0) {
1119 dev_err(pcdev->dev, "Can't request DMA for V\n");
1120 err = -ENOMEM;
1121 goto exit_free_dma_u;
1122 }
1123 dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
1124
1125 DRCMR68 = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1126 DRCMR69 = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1127 DRCMR70 = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001128
1129 /* request irq */
1130 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1131 pcdev);
1132 if (err) {
1133 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1134 goto exit_free_dma;
1135 }
1136
1137 pxa_soc_camera_host.priv = pcdev;
1138 pxa_soc_camera_host.dev.parent = &pdev->dev;
1139 pxa_soc_camera_host.nr = pdev->id;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001140 err = soc_camera_host_register(&pxa_soc_camera_host);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001141 if (err)
1142 goto exit_free_irq;
1143
1144 return 0;
1145
1146exit_free_irq:
1147 free_irq(pcdev->irq, pcdev);
1148exit_free_dma:
Mike Rapoporta5462e52008-04-22 10:36:32 -03001149 pxa_free_dma(pcdev->dma_chans[2]);
1150exit_free_dma_u:
1151 pxa_free_dma(pcdev->dma_chans[1]);
1152exit_free_dma_y:
1153 pxa_free_dma(pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001154exit_iounmap:
1155 iounmap(base);
1156exit_release:
1157 release_mem_region(res->start, res->end - res->start + 1);
1158exit_clk:
1159 clk_put(pcdev->clk);
1160exit_kfree:
1161 kfree(pcdev);
1162exit:
1163 return err;
1164}
1165
1166static int __devexit pxa_camera_remove(struct platform_device *pdev)
1167{
1168 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1169 struct resource *res;
1170
1171 clk_put(pcdev->clk);
1172
Mike Rapoporta5462e52008-04-22 10:36:32 -03001173 pxa_free_dma(pcdev->dma_chans[0]);
1174 pxa_free_dma(pcdev->dma_chans[1]);
1175 pxa_free_dma(pcdev->dma_chans[2]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001176 free_irq(pcdev->irq, pcdev);
1177
1178 soc_camera_host_unregister(&pxa_soc_camera_host);
1179
1180 iounmap(pcdev->base);
1181
1182 res = pcdev->res;
1183 release_mem_region(res->start, res->end - res->start + 1);
1184
1185 kfree(pcdev);
1186
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001187 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001188
1189 return 0;
1190}
1191
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001192static struct platform_driver pxa_camera_driver = {
1193 .driver = {
1194 .name = PXA_CAM_DRV_NAME,
1195 },
1196 .probe = pxa_camera_probe,
1197 .remove = __exit_p(pxa_camera_remove),
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001198};
1199
1200
1201static int __devinit pxa_camera_init(void)
1202{
1203 return platform_driver_register(&pxa_camera_driver);
1204}
1205
1206static void __exit pxa_camera_exit(void)
1207{
1208 return platform_driver_unregister(&pxa_camera_driver);
1209}
1210
1211module_init(pxa_camera_init);
1212module_exit(pxa_camera_exit);
1213
1214MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1215MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1216MODULE_LICENSE("GPL");