blob: 97923e1bd06d9af2b0a55e06c114f0f5faca691b [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
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];
Robert Jarzmik3f6ac492008-08-02 07:10:04 -0300131
132 u32 save_cicr[5];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300133};
134
135static const char *pxa_cam_driver_description = "PXA_Camera";
136
137static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
138
139/*
140 * Videobuf operations
141 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300142static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
143 unsigned int *size)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300144{
145 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300146 struct soc_camera_host *ici =
147 to_soc_camera_host(icd->dev.parent);
148 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300149
150 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
151
Mike Rapoporta5462e52008-04-22 10:36:32 -0300152 /* planar capture requires Y, U and V buffers to be page aligned */
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300153 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300154 *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
155 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
156 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
157 } else {
158 *size = icd->width * icd->height *
159 ((icd->current_fmt->depth + 7) >> 3);
160 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300161
162 if (0 == *count)
163 *count = 32;
164 while (*size * *count > vid_limit * 1024 * 1024)
165 (*count)--;
166
167 return 0;
168}
169
170static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
171{
172 struct soc_camera_device *icd = vq->priv_data;
173 struct soc_camera_host *ici =
174 to_soc_camera_host(icd->dev.parent);
175 struct pxa_camera_dev *pcdev = ici->priv;
176 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300177 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300178
179 BUG_ON(in_interrupt());
180
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300181 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300182 &buf->vb, buf->vb.baddr, buf->vb.bsize);
183
184 /* This waits until this buffer is out of danger, i.e., until it is no
185 * longer in STATE_QUEUED or STATE_ACTIVE */
186 videobuf_waiton(&buf->vb, 0, 0);
187 videobuf_dma_unmap(vq, dma);
188 videobuf_dma_free(dma);
189
Mike Rapoporta5462e52008-04-22 10:36:32 -0300190 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
191 if (buf->dmas[i].sg_cpu)
192 dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
193 buf->dmas[i].sg_cpu,
194 buf->dmas[i].sg_dma);
195 buf->dmas[i].sg_cpu = NULL;
196 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300197
198 buf->vb.state = VIDEOBUF_NEEDS_INIT;
199}
200
Mike Rapoporta5462e52008-04-22 10:36:32 -0300201static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
202 struct pxa_buffer *buf,
203 struct videobuf_dmabuf *dma, int channel,
204 int sglen, int sg_start, int cibr,
205 unsigned int size)
206{
207 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
208 int i;
209
210 if (pxa_dma->sg_cpu)
211 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
212 pxa_dma->sg_cpu, pxa_dma->sg_dma);
213
214 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
215 pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
216 &pxa_dma->sg_dma, GFP_KERNEL);
217 if (!pxa_dma->sg_cpu)
218 return -ENOMEM;
219
220 pxa_dma->sglen = sglen;
221
222 for (i = 0; i < sglen; i++) {
223 int sg_i = sg_start + i;
224 struct scatterlist *sg = dma->sglist;
225 unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
226
227 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
228 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
229
230 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
231 xfer_len = (min(dma_len, size) + 7) & ~7;
232
233 pxa_dma->sg_cpu[i].dcmd =
234 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
235 size -= dma_len;
236 pxa_dma->sg_cpu[i].ddadr =
237 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
238 }
239
240 pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
241 pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
242
243 return 0;
244}
245
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300246static int pxa_videobuf_prepare(struct videobuf_queue *vq,
247 struct videobuf_buffer *vb, enum v4l2_field field)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300248{
249 struct soc_camera_device *icd = vq->priv_data;
250 struct soc_camera_host *ici =
251 to_soc_camera_host(icd->dev.parent);
252 struct pxa_camera_dev *pcdev = ici->priv;
253 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300254 int ret;
255 int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
256 int size_y, size_u = 0, size_v = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300257
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300258 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300259 vb, vb->baddr, vb->bsize);
260
261 /* Added list head initialization on alloc */
262 WARN_ON(!list_empty(&vb->queue));
263
264#ifdef DEBUG
265 /* This can be useful if you want to see if we actually fill
266 * the buffer with something */
267 memset((void *)vb->baddr, 0xaa, vb->bsize);
268#endif
269
270 BUG_ON(NULL == icd->current_fmt);
271
272 /* I think, in buf_prepare you only have to protect global data,
273 * the actual buffer is yours */
274 buf->inwork = 1;
275
276 if (buf->fmt != icd->current_fmt ||
277 vb->width != icd->width ||
278 vb->height != icd->height ||
279 vb->field != field) {
280 buf->fmt = icd->current_fmt;
281 vb->width = icd->width;
282 vb->height = icd->height;
283 vb->field = field;
284 vb->state = VIDEOBUF_NEEDS_INIT;
285 }
286
287 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
288 if (0 != vb->baddr && vb->bsize < vb->size) {
289 ret = -EINVAL;
290 goto out;
291 }
292
293 if (vb->state == VIDEOBUF_NEEDS_INIT) {
294 unsigned int size = vb->size;
295 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
296
297 ret = videobuf_iolock(vq, vb, NULL);
298 if (ret)
299 goto fail;
300
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300301 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300302 /* FIXME the calculations should be more precise */
303 sglen_y = dma->sglen / 2;
304 sglen_u = sglen_v = dma->sglen / 4 + 1;
305 sglen_yu = sglen_y + sglen_u;
306 size_y = size / 2;
307 size_u = size_v = size / 4;
308 } else {
309 sglen_y = dma->sglen;
310 size_y = size;
311 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300312
Mike Rapoporta5462e52008-04-22 10:36:32 -0300313 /* init DMA for Y channel */
314 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
315 0, 0x28, size_y);
316
317 if (ret) {
318 dev_err(pcdev->dev,
319 "DMA initialization for Y/RGB failed\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300320 goto fail;
321 }
322
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300323 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300324 /* init DMA for U channel */
325 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
326 sglen_y, 0x30, size_u);
327 if (ret) {
328 dev_err(pcdev->dev,
329 "DMA initialization for U failed\n");
330 goto fail_u;
331 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300332
Mike Rapoporta5462e52008-04-22 10:36:32 -0300333 /* init DMA for V channel */
334 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
335 sglen_yu, 0x38, size_v);
336 if (ret) {
337 dev_err(pcdev->dev,
338 "DMA initialization for V failed\n");
339 goto fail_v;
340 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300341 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300342
343 vb->state = VIDEOBUF_PREPARED;
344 }
345
346 buf->inwork = 0;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300347 buf->active_dma = DMA_Y;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300348 if (pcdev->channels == 3)
Mike Rapoporta5462e52008-04-22 10:36:32 -0300349 buf->active_dma |= DMA_U | DMA_V;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300350
351 return 0;
352
Mike Rapoporta5462e52008-04-22 10:36:32 -0300353fail_v:
354 dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
355 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
356fail_u:
357 dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
358 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300359fail:
360 free_buffer(vq, buf);
361out:
362 buf->inwork = 0;
363 return ret;
364}
365
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300366static void pxa_videobuf_queue(struct videobuf_queue *vq,
367 struct videobuf_buffer *vb)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300368{
369 struct soc_camera_device *icd = vq->priv_data;
370 struct soc_camera_host *ici =
371 to_soc_camera_host(icd->dev.parent);
372 struct pxa_camera_dev *pcdev = ici->priv;
373 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300374 struct pxa_buffer *active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300375 unsigned long flags;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300376 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300377
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300378 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300379 vb, vb->baddr, vb->bsize);
380 spin_lock_irqsave(&pcdev->lock, flags);
381
382 list_add_tail(&vb->queue, &pcdev->capture);
383
384 vb->state = VIDEOBUF_ACTIVE;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300385 active = pcdev->active;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300386
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300387 if (!active) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300388 CIFR |= CIFR_RESET_F;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300389
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300390 for (i = 0; i < pcdev->channels; i++) {
391 DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
392 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
393 pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300394 }
395
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300396 pcdev->active = buf;
397 CICR0 |= CICR0_ENB;
398 } else {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300399 struct pxa_cam_dma *buf_dma;
400 struct pxa_cam_dma *act_dma;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300401 int nents;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300402
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300403 for (i = 0; i < pcdev->channels; i++) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300404 buf_dma = &buf->dmas[i];
405 act_dma = &active->dmas[i];
406 nents = buf_dma->sglen;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300407
Mike Rapoporta5462e52008-04-22 10:36:32 -0300408 /* Stop DMA engine */
409 DCSR(pcdev->dma_chans[i]) = 0;
410
411 /* Add the descriptors we just initialized to
412 the currently running chain */
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300413 pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
414 pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300415
416 /* Setup a dummy descriptor with the DMA engines current
417 * state
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300418 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300419 buf_dma->sg_cpu[nents].dsadr =
420 pcdev->res->start + 0x28 + i*8; /* CIBRx */
421 buf_dma->sg_cpu[nents].dtadr =
422 DTADR(pcdev->dma_chans[i]);
423 buf_dma->sg_cpu[nents].dcmd =
424 DCMD(pcdev->dma_chans[i]);
425
426 if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
427 /* The DMA engine is on the last
428 descriptor, set the next descriptors
429 address to the descriptors we just
430 initialized */
431 buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
432 } else {
433 buf_dma->sg_cpu[nents].ddadr =
434 DDADR(pcdev->dma_chans[i]);
435 }
436
437 /* The next descriptor is the dummy descriptor */
438 DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
439 sizeof(struct pxa_dma_desc);
440
441 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300442 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300443 }
444
445 spin_unlock_irqrestore(&pcdev->lock, flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300446}
447
448static void pxa_videobuf_release(struct videobuf_queue *vq,
449 struct videobuf_buffer *vb)
450{
451 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
452#ifdef DEBUG
453 struct soc_camera_device *icd = vq->priv_data;
454
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300455 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300456 vb, vb->baddr, vb->bsize);
457
458 switch (vb->state) {
459 case VIDEOBUF_ACTIVE:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300460 dev_dbg(&icd->dev, "%s (active)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300461 break;
462 case VIDEOBUF_QUEUED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300463 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300464 break;
465 case VIDEOBUF_PREPARED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300466 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300467 break;
468 default:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300469 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300470 break;
471 }
472#endif
473
474 free_buffer(vq, buf);
475}
476
Mike Rapoporta5462e52008-04-22 10:36:32 -0300477static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
478 struct videobuf_buffer *vb,
479 struct pxa_buffer *buf)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300480{
Mike Rapoporta5462e52008-04-22 10:36:32 -0300481 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
482 list_del_init(&vb->queue);
483 vb->state = VIDEOBUF_DONE;
484 do_gettimeofday(&vb->ts);
485 vb->field_count++;
486 wake_up(&vb->done);
487
488 if (list_empty(&pcdev->capture)) {
489 pcdev->active = NULL;
490 DCSR(pcdev->dma_chans[0]) = 0;
491 DCSR(pcdev->dma_chans[1]) = 0;
492 DCSR(pcdev->dma_chans[2]) = 0;
493 CICR0 &= ~CICR0_ENB;
494 return;
495 }
496
497 pcdev->active = list_entry(pcdev->capture.next,
498 struct pxa_buffer, vb.queue);
499}
500
501static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
502 enum pxa_camera_active_dma act_dma)
503{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300504 struct pxa_buffer *buf;
505 unsigned long flags;
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300506 u32 status, camera_status, overrun;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300507 struct videobuf_buffer *vb;
508
509 spin_lock_irqsave(&pcdev->lock, flags);
510
Mike Rapoporta5462e52008-04-22 10:36:32 -0300511 status = DCSR(channel);
512 DCSR(channel) = status | DCSR_ENDINTR;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300513
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300514 if (status & DCSR_BUSERR) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300515 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300516 goto out;
517 }
518
519 if (!(status & DCSR_ENDINTR)) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300520 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
521 "status: 0x%08x\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300522 goto out;
523 }
524
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300525 if (!pcdev->active) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300526 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300527 goto out;
528 }
529
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300530 camera_status = CISR;
531 overrun = CISR_IFO_0;
532 if (pcdev->channels == 3)
533 overrun |= CISR_IFO_1 | CISR_IFO_2;
534 if (camera_status & overrun) {
535 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
536 /* Stop the Capture Interface */
537 CICR0 &= ~CICR0_ENB;
538 /* Stop DMA */
539 DCSR(channel) = 0;
540 /* Reset the FIFOs */
541 CIFR |= CIFR_RESET_F;
542 /* Enable End-Of-Frame Interrupt */
543 CICR0 &= ~CICR0_EOFM;
544 /* Restart the Capture Interface */
545 CICR0 |= CICR0_ENB;
546 goto out;
547 }
548
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300549 vb = &pcdev->active->vb;
550 buf = container_of(vb, struct pxa_buffer, vb);
551 WARN_ON(buf->inwork || list_empty(&vb->queue));
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300552 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300553 vb, vb->baddr, vb->bsize);
554
Mike Rapoporta5462e52008-04-22 10:36:32 -0300555 buf->active_dma &= ~act_dma;
556 if (!buf->active_dma)
557 pxa_camera_wakeup(pcdev, vb, buf);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300558
559out:
560 spin_unlock_irqrestore(&pcdev->lock, flags);
561}
562
Mike Rapoporta5462e52008-04-22 10:36:32 -0300563static void pxa_camera_dma_irq_y(int channel, void *data)
564{
565 struct pxa_camera_dev *pcdev = data;
566 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
567}
568
569static void pxa_camera_dma_irq_u(int channel, void *data)
570{
571 struct pxa_camera_dev *pcdev = data;
572 pxa_camera_dma_irq(channel, pcdev, DMA_U);
573}
574
575static void pxa_camera_dma_irq_v(int channel, void *data)
576{
577 struct pxa_camera_dev *pcdev = data;
578 pxa_camera_dma_irq(channel, pcdev, DMA_V);
579}
580
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300581static struct videobuf_queue_ops pxa_videobuf_ops = {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300582 .buf_setup = pxa_videobuf_setup,
583 .buf_prepare = pxa_videobuf_prepare,
584 .buf_queue = pxa_videobuf_queue,
585 .buf_release = pxa_videobuf_release,
586};
587
Magnus Damma034d1b2008-07-11 20:59:34 -0300588static void pxa_camera_init_videobuf(struct videobuf_queue *q,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300589 struct soc_camera_device *icd)
590{
Magnus Damma034d1b2008-07-11 20:59:34 -0300591 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
592 struct pxa_camera_dev *pcdev = ici->priv;
593
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300594 /* We must pass NULL as dev pointer, then all pci_* dma operations
595 * transform to normal dma_* ones. */
Magnus Damma034d1b2008-07-11 20:59:34 -0300596 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300597 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
598 sizeof(struct pxa_buffer), icd);
599}
600
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300601static int mclk_get_divisor(struct pxa_camera_dev *pcdev)
602{
603 unsigned int mclk_10khz = pcdev->platform_mclk_10khz;
604 unsigned long div;
605 unsigned long lcdclk;
606
607 lcdclk = clk_get_rate(pcdev->clk) / 10000;
608
609 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
610 * they get a nice Oops */
611 div = (lcdclk + 2 * mclk_10khz - 1) / (2 * mclk_10khz) - 1;
612
613 dev_dbg(pcdev->dev, "LCD clock %lukHz, target freq %dkHz, "
614 "divisor %lu\n", lcdclk * 10, mclk_10khz * 10, div);
615
616 return div;
617}
618
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300619static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300620{
621 struct pxacamera_platform_data *pdata = pcdev->pdata;
622 u32 cicr4 = 0;
623
624 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
625 pcdev, pdata);
626
627 if (pdata && pdata->init) {
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300628 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300629 pdata->init(pcdev->dev);
630 }
631
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300632 CICR0 = 0x3FF; /* disable all interrupts */
633
634 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
635 cicr4 |= CICR4_PCLK_EN;
636 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
637 cicr4 |= CICR4_MCLK_EN;
638 if (pcdev->platform_flags & PXA_CAMERA_PCP)
639 cicr4 |= CICR4_PCP;
640 if (pcdev->platform_flags & PXA_CAMERA_HSP)
641 cicr4 |= CICR4_HSP;
642 if (pcdev->platform_flags & PXA_CAMERA_VSP)
643 cicr4 |= CICR4_VSP;
644
645 CICR4 = mclk_get_divisor(pcdev) | cicr4;
646
647 clk_enable(pcdev->clk);
648}
649
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300650static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300651{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300652 clk_disable(pcdev->clk);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300653}
654
655static irqreturn_t pxa_camera_irq(int irq, void *data)
656{
657 struct pxa_camera_dev *pcdev = data;
658 unsigned int status = CISR;
659
660 dev_dbg(pcdev->dev, "Camera interrupt status 0x%x\n", status);
661
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300662 if (!status)
663 return IRQ_NONE;
664
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300665 CISR = status;
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300666
667 if (status & CISR_EOF) {
668 int i;
669 for (i = 0; i < pcdev->channels; i++) {
670 DDADR(pcdev->dma_chans[i]) =
671 pcdev->active->dmas[i].sg_dma;
672 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
673 }
674 CICR0 |= CICR0_EOFM;
675 }
676
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300677 return IRQ_HANDLED;
678}
679
680/* The following two functions absolutely depend on the fact, that
681 * there can be only one camera on PXA quick capture interface */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300682static int pxa_camera_add_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300683{
684 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
685 struct pxa_camera_dev *pcdev = ici->priv;
686 int ret;
687
688 mutex_lock(&camera_lock);
689
690 if (pcdev->icd) {
691 ret = -EBUSY;
692 goto ebusy;
693 }
694
695 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
696 icd->devnum);
697
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300698 pxa_camera_activate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300699 ret = icd->ops->init(icd);
700
701 if (!ret)
702 pcdev->icd = icd;
703
704ebusy:
705 mutex_unlock(&camera_lock);
706
707 return ret;
708}
709
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300710static void pxa_camera_remove_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300711{
712 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
713 struct pxa_camera_dev *pcdev = ici->priv;
714
715 BUG_ON(icd != pcdev->icd);
716
717 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
718 icd->devnum);
719
720 /* disable capture, disable interrupts */
721 CICR0 = 0x3ff;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300722
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300723 /* Stop DMA engine */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300724 DCSR(pcdev->dma_chans[0]) = 0;
725 DCSR(pcdev->dma_chans[1]) = 0;
726 DCSR(pcdev->dma_chans[2]) = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300727
728 icd->ops->release(icd);
729
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300730 pxa_camera_deactivate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300731
732 pcdev->icd = NULL;
733}
734
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300735static int test_platform_param(struct pxa_camera_dev *pcdev,
736 unsigned char buswidth, unsigned long *flags)
737{
738 /*
739 * Platform specified synchronization and pixel clock polarities are
740 * only a recommendation and are only used during probing. The PXA270
741 * quick capture interface supports both.
742 */
743 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
744 SOCAM_MASTER : SOCAM_SLAVE) |
745 SOCAM_HSYNC_ACTIVE_HIGH |
746 SOCAM_HSYNC_ACTIVE_LOW |
747 SOCAM_VSYNC_ACTIVE_HIGH |
748 SOCAM_VSYNC_ACTIVE_LOW |
749 SOCAM_PCLK_SAMPLE_RISING |
750 SOCAM_PCLK_SAMPLE_FALLING;
751
752 /* If requested data width is supported by the platform, use it */
753 switch (buswidth) {
754 case 10:
755 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
756 return -EINVAL;
757 *flags |= SOCAM_DATAWIDTH_10;
758 break;
759 case 9:
760 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
761 return -EINVAL;
762 *flags |= SOCAM_DATAWIDTH_9;
763 break;
764 case 8:
765 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
766 return -EINVAL;
767 *flags |= SOCAM_DATAWIDTH_8;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300768 break;
769 default:
770 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300771 }
772
773 return 0;
774}
775
776static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300777{
778 struct soc_camera_host *ici =
779 to_soc_camera_host(icd->dev.parent);
780 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300781 unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300782 u32 cicr0, cicr1, cicr4 = 0;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300783 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300784
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300785 if (ret < 0)
786 return ret;
787
788 camera_flags = icd->ops->query_bus_param(icd);
789
790 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
791 if (!common_flags)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300792 return -EINVAL;
793
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300794 pcdev->channels = 1;
795
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300796 /* Make choises, based on platform preferences */
797 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
798 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
799 if (pcdev->platform_flags & PXA_CAMERA_HSP)
800 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
801 else
802 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
803 }
804
805 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
806 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
807 if (pcdev->platform_flags & PXA_CAMERA_VSP)
808 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
809 else
810 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
811 }
812
813 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
814 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
815 if (pcdev->platform_flags & PXA_CAMERA_PCP)
816 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
817 else
818 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
819 }
820
821 ret = icd->ops->set_bus_param(icd, common_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300822 if (ret < 0)
823 return ret;
824
825 /* Datawidth is now guaranteed to be equal to one of the three values.
826 * We fix bit-per-pixel equal to data-width... */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300827 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
828 case SOCAM_DATAWIDTH_10:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300829 dw = 4;
830 bpp = 0x40;
831 break;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300832 case SOCAM_DATAWIDTH_9:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300833 dw = 3;
834 bpp = 0x20;
835 break;
836 default:
837 /* Actually it can only be 8 now,
838 * default is just to silence compiler warnings */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300839 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300840 dw = 2;
841 bpp = 0;
842 }
843
844 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
845 cicr4 |= CICR4_PCLK_EN;
846 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
847 cicr4 |= CICR4_MCLK_EN;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300848 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300849 cicr4 |= CICR4_PCP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300850 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300851 cicr4 |= CICR4_HSP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300852 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300853 cicr4 |= CICR4_VSP;
854
855 cicr0 = CICR0;
856 if (cicr0 & CICR0_ENB)
857 CICR0 = cicr0 & ~CICR0_ENB;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300858
859 cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
860
861 switch (pixfmt) {
862 case V4L2_PIX_FMT_YUV422P:
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300863 pcdev->channels = 3;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300864 cicr1 |= CICR1_YCBCR_F;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300865 /*
866 * Normally, pxa bus wants as input UYVY format. We allow all
867 * reorderings of the YUV422 format, as no processing is done,
868 * and the YUV stream is just passed through without any
869 * transformation. Note that UYVY is the only format that
870 * should be used if pxa framebuffer Overlay2 is used.
871 */
872 case V4L2_PIX_FMT_UYVY:
873 case V4L2_PIX_FMT_VYUY:
Mike Rapoporta5462e52008-04-22 10:36:32 -0300874 case V4L2_PIX_FMT_YUYV:
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300875 case V4L2_PIX_FMT_YVYU:
Mike Rapoporta5462e52008-04-22 10:36:32 -0300876 cicr1 |= CICR1_COLOR_SP_VAL(2);
877 break;
878 case V4L2_PIX_FMT_RGB555:
879 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
880 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
881 break;
882 case V4L2_PIX_FMT_RGB565:
883 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
884 break;
885 }
886
887 CICR1 = cicr1;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300888 CICR2 = 0;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300889 CICR3 = CICR3_LPF_VAL(icd->height - 1) |
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300890 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
891 CICR4 = mclk_get_divisor(pcdev) | cicr4;
892
893 /* CIF interrupts are not used, only DMA */
894 CICR0 = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300895 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)) |
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300896 CICR0_DMAEN | CICR0_IRQ_MASK | (cicr0 & CICR0_ENB);
897
898 return 0;
899}
900
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300901static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
902 unsigned char buswidth)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300903{
904 struct soc_camera_host *ici =
905 to_soc_camera_host(icd->dev.parent);
906 struct pxa_camera_dev *pcdev = ici->priv;
907 unsigned long bus_flags, camera_flags;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300908 int ret = test_platform_param(pcdev, buswidth, &bus_flags);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300909
910 if (ret < 0)
911 return ret;
912
913 camera_flags = icd->ops->query_bus_param(icd);
914
915 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
916}
917
Robert Jarzmik2a48fc72008-12-01 09:45:35 -0300918static const struct soc_camera_data_format pxa_camera_formats[] = {
919 {
920 .name = "Planar YUV422 16 bit",
921 .depth = 16,
922 .fourcc = V4L2_PIX_FMT_YUV422P,
923 .colorspace = V4L2_COLORSPACE_JPEG,
924 },
925};
926
927static bool buswidth_supported(struct soc_camera_device *icd, int depth)
928{
929 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
930 struct pxa_camera_dev *pcdev = ici->priv;
931
932 switch (depth) {
933 case 8:
934 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
935 case 9:
936 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
937 case 10:
938 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
939 }
940 return false;
941}
942
943static int required_buswidth(const struct soc_camera_data_format *fmt)
944{
945 switch (fmt->fourcc) {
946 case V4L2_PIX_FMT_UYVY:
947 case V4L2_PIX_FMT_VYUY:
948 case V4L2_PIX_FMT_YUYV:
949 case V4L2_PIX_FMT_YVYU:
950 case V4L2_PIX_FMT_RGB565:
951 case V4L2_PIX_FMT_RGB555:
952 return 8;
953 default:
954 return fmt->depth;
955 }
956}
957
958static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
959 struct soc_camera_format_xlate *xlate)
960{
961 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
962 int formats = 0, buswidth, ret;
963
964 buswidth = required_buswidth(icd->formats + idx);
965
966 if (!buswidth_supported(icd, buswidth))
967 return 0;
968
969 ret = pxa_camera_try_bus_param(icd, buswidth);
970 if (ret < 0)
971 return 0;
972
973 switch (icd->formats[idx].fourcc) {
974 case V4L2_PIX_FMT_UYVY:
975 formats++;
976 if (xlate) {
977 xlate->host_fmt = &pxa_camera_formats[0];
978 xlate->cam_fmt = icd->formats + idx;
979 xlate->buswidth = buswidth;
980 xlate++;
981 dev_dbg(&ici->dev, "Providing format %s using %s\n",
982 pxa_camera_formats[0].name,
983 icd->formats[idx].name);
984 }
985 case V4L2_PIX_FMT_VYUY:
986 case V4L2_PIX_FMT_YUYV:
987 case V4L2_PIX_FMT_YVYU:
988 case V4L2_PIX_FMT_RGB565:
989 case V4L2_PIX_FMT_RGB555:
990 formats++;
991 if (xlate) {
992 xlate->host_fmt = icd->formats + idx;
993 xlate->cam_fmt = icd->formats + idx;
994 xlate->buswidth = buswidth;
995 xlate++;
996 dev_dbg(&ici->dev, "Providing format %s packed\n",
997 icd->formats[idx].name);
998 }
999 break;
1000 default:
1001 /* Generic pass-through */
1002 formats++;
1003 if (xlate) {
1004 xlate->host_fmt = icd->formats + idx;
1005 xlate->cam_fmt = icd->formats + idx;
1006 xlate->buswidth = icd->formats[idx].depth;
1007 xlate++;
1008 dev_dbg(&ici->dev,
1009 "Providing format %s in pass-through mode\n",
1010 icd->formats[idx].name);
1011 }
1012 }
1013
1014 return formats;
1015}
1016
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001017static int pxa_camera_set_fmt(struct soc_camera_device *icd,
1018 __u32 pixfmt, struct v4l2_rect *rect)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001019{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001020 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1021 const struct soc_camera_data_format *host_fmt, *cam_fmt = NULL;
1022 const struct soc_camera_format_xlate *xlate;
1023 int ret, buswidth;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001024
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001025 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1026 if (!xlate) {
1027 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
1028 return -EINVAL;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001029 }
1030
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001031 buswidth = xlate->buswidth;
1032 host_fmt = xlate->host_fmt;
1033 cam_fmt = xlate->cam_fmt;
1034
1035 switch (pixfmt) {
1036 case 0: /* Only geometry change */
1037 ret = icd->ops->set_fmt(icd, pixfmt, rect);
1038 break;
1039 default:
1040 ret = icd->ops->set_fmt(icd, cam_fmt->fourcc, rect);
1041 }
1042
1043 if (ret < 0)
1044 dev_warn(&ici->dev, "Failed to configure for format %x\n",
1045 pixfmt);
1046
1047 if (pixfmt && !ret) {
1048 icd->buswidth = buswidth;
1049 icd->current_fmt = host_fmt;
1050 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001051
1052 return ret;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001053}
1054
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001055static int pxa_camera_try_fmt(struct soc_camera_device *icd,
1056 struct v4l2_format *f)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001057{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001058 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1059 const struct soc_camera_format_xlate *xlate;
1060 struct v4l2_pix_format *pix = &f->fmt.pix;
1061 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001062
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001063 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1064 if (!xlate) {
1065 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001066 return -EINVAL;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001067 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001068
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001069 /* limit to pxa hardware capabilities */
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001070 if (pix->height < 32)
1071 pix->height = 32;
1072 if (pix->height > 2048)
1073 pix->height = 2048;
1074 if (pix->width < 48)
1075 pix->width = 48;
1076 if (pix->width > 2048)
1077 pix->width = 2048;
1078 pix->width &= ~0x01;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001079
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001080 pix->bytesperline = pix->width *
1081 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1082 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001083
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001084 /* limit to sensor capabilities */
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001085 return icd->ops->try_fmt(icd, f);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001086}
1087
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001088static int pxa_camera_reqbufs(struct soc_camera_file *icf,
1089 struct v4l2_requestbuffers *p)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001090{
1091 int i;
1092
1093 /* This is for locking debugging only. I removed spinlocks and now I
1094 * check whether .prepare is ever called on a linked buffer, or whether
1095 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1096 * it hadn't triggered */
1097 for (i = 0; i < p->count; i++) {
1098 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1099 struct pxa_buffer, vb);
1100 buf->inwork = 0;
1101 INIT_LIST_HEAD(&buf->vb.queue);
1102 }
1103
1104 return 0;
1105}
1106
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001107static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001108{
1109 struct soc_camera_file *icf = file->private_data;
1110 struct pxa_buffer *buf;
1111
1112 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
1113 vb.stream);
1114
1115 poll_wait(file, &buf->vb.done, pt);
1116
1117 if (buf->vb.state == VIDEOBUF_DONE ||
1118 buf->vb.state == VIDEOBUF_ERROR)
1119 return POLLIN|POLLRDNORM;
1120
1121 return 0;
1122}
1123
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001124static int pxa_camera_querycap(struct soc_camera_host *ici,
1125 struct v4l2_capability *cap)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001126{
1127 /* cap->name is set by the firendly caller:-> */
1128 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1129 cap->version = PXA_CAM_VERSION_CODE;
1130 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1131
1132 return 0;
1133}
1134
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001135static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1136{
1137 struct soc_camera_host *ici =
1138 to_soc_camera_host(icd->dev.parent);
1139 struct pxa_camera_dev *pcdev = ici->priv;
1140 int i = 0, ret = 0;
1141
1142 pcdev->save_cicr[i++] = CICR0;
1143 pcdev->save_cicr[i++] = CICR1;
1144 pcdev->save_cicr[i++] = CICR2;
1145 pcdev->save_cicr[i++] = CICR3;
1146 pcdev->save_cicr[i++] = CICR4;
1147
1148 if ((pcdev->icd) && (pcdev->icd->ops->suspend))
1149 ret = pcdev->icd->ops->suspend(pcdev->icd, state);
1150
1151 return ret;
1152}
1153
1154static int pxa_camera_resume(struct soc_camera_device *icd)
1155{
1156 struct soc_camera_host *ici =
1157 to_soc_camera_host(icd->dev.parent);
1158 struct pxa_camera_dev *pcdev = ici->priv;
1159 int i = 0, ret = 0;
1160
Eric Miao87f3dd72008-09-08 15:26:43 +08001161 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1162 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1163 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001164
1165 CICR0 = pcdev->save_cicr[i++] & ~CICR0_ENB;
1166 CICR1 = pcdev->save_cicr[i++];
1167 CICR2 = pcdev->save_cicr[i++];
1168 CICR3 = pcdev->save_cicr[i++];
1169 CICR4 = pcdev->save_cicr[i++];
1170
1171 if ((pcdev->icd) && (pcdev->icd->ops->resume))
1172 ret = pcdev->icd->ops->resume(pcdev->icd);
1173
1174 /* Restart frame capture if active buffer exists */
1175 if (!ret && pcdev->active) {
1176 /* Reset the FIFOs */
1177 CIFR |= CIFR_RESET_F;
1178 /* Enable End-Of-Frame Interrupt */
1179 CICR0 &= ~CICR0_EOFM;
1180 /* Restart the Capture Interface */
1181 CICR0 |= CICR0_ENB;
1182 }
1183
1184 return ret;
1185}
1186
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001187static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1188 .owner = THIS_MODULE,
1189 .add = pxa_camera_add_device,
1190 .remove = pxa_camera_remove_device,
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001191 .suspend = pxa_camera_suspend,
1192 .resume = pxa_camera_resume,
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001193 .get_formats = pxa_camera_get_formats,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001194 .set_fmt = pxa_camera_set_fmt,
1195 .try_fmt = pxa_camera_try_fmt,
Paulius Zaleckas092d3922008-07-11 20:50:31 -03001196 .init_videobuf = pxa_camera_init_videobuf,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001197 .reqbufs = pxa_camera_reqbufs,
1198 .poll = pxa_camera_poll,
1199 .querycap = pxa_camera_querycap,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001200 .set_bus_param = pxa_camera_set_bus_param,
1201};
1202
1203/* Should be allocated dynamically too, but we have only one. */
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001204static struct soc_camera_host pxa_soc_camera_host = {
1205 .drv_name = PXA_CAM_DRV_NAME,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001206 .ops = &pxa_soc_camera_host_ops,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001207};
1208
1209static int pxa_camera_probe(struct platform_device *pdev)
1210{
1211 struct pxa_camera_dev *pcdev;
1212 struct resource *res;
1213 void __iomem *base;
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001214 int irq;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001215 int err = 0;
1216
1217 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1218 irq = platform_get_irq(pdev, 0);
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001219 if (!res || irq < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001220 err = -ENODEV;
1221 goto exit;
1222 }
1223
1224 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1225 if (!pcdev) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001226 dev_err(&pdev->dev, "Could not allocate pcdev\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001227 err = -ENOMEM;
1228 goto exit;
1229 }
1230
1231 pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
1232 if (IS_ERR(pcdev->clk)) {
1233 err = PTR_ERR(pcdev->clk);
1234 goto exit_kfree;
1235 }
1236
1237 dev_set_drvdata(&pdev->dev, pcdev);
1238 pcdev->res = res;
1239
1240 pcdev->pdata = pdev->dev.platform_data;
1241 pcdev->platform_flags = pcdev->pdata->flags;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001242 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1243 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001244 /* Platform hasn't set available data widths. This is bad.
1245 * Warn and use a default. */
1246 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1247 "data widths, using default 10 bit\n");
1248 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1249 }
1250 pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
1251 if (!pcdev->platform_mclk_10khz) {
1252 dev_warn(&pdev->dev,
1253 "mclk_10khz == 0! Please, fix your platform data. "
1254 "Using default 20MHz\n");
1255 pcdev->platform_mclk_10khz = 2000;
1256 }
1257
1258 INIT_LIST_HEAD(&pcdev->capture);
1259 spin_lock_init(&pcdev->lock);
1260
1261 /*
1262 * Request the regions.
1263 */
1264 if (!request_mem_region(res->start, res->end - res->start + 1,
1265 PXA_CAM_DRV_NAME)) {
1266 err = -EBUSY;
1267 goto exit_clk;
1268 }
1269
1270 base = ioremap(res->start, res->end - res->start + 1);
1271 if (!base) {
1272 err = -ENOMEM;
1273 goto exit_release;
1274 }
1275 pcdev->irq = irq;
1276 pcdev->base = base;
1277 pcdev->dev = &pdev->dev;
1278
1279 /* request dma */
roel kluinde3e3b82008-09-18 17:50:15 -03001280 err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1281 pxa_camera_dma_irq_y, pcdev);
1282 if (err < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001283 dev_err(pcdev->dev, "Can't request DMA for Y\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001284 goto exit_iounmap;
1285 }
roel kluinde3e3b82008-09-18 17:50:15 -03001286 pcdev->dma_chans[0] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001287 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001288
roel kluinde3e3b82008-09-18 17:50:15 -03001289 err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1290 pxa_camera_dma_irq_u, pcdev);
1291 if (err < 0) {
Mike Rapoporta5462e52008-04-22 10:36:32 -03001292 dev_err(pcdev->dev, "Can't request DMA for U\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001293 goto exit_free_dma_y;
1294 }
roel kluinde3e3b82008-09-18 17:50:15 -03001295 pcdev->dma_chans[1] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001296 dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1297
roel kluinde3e3b82008-09-18 17:50:15 -03001298 err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1299 pxa_camera_dma_irq_v, pcdev);
1300 if (err < 0) {
Mike Rapoporta5462e52008-04-22 10:36:32 -03001301 dev_err(pcdev->dev, "Can't request DMA for V\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001302 goto exit_free_dma_u;
1303 }
roel kluinde3e3b82008-09-18 17:50:15 -03001304 pcdev->dma_chans[2] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001305 dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
1306
Eric Miao87f3dd72008-09-08 15:26:43 +08001307 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1308 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1309 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001310
1311 /* request irq */
1312 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1313 pcdev);
1314 if (err) {
1315 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1316 goto exit_free_dma;
1317 }
1318
1319 pxa_soc_camera_host.priv = pcdev;
1320 pxa_soc_camera_host.dev.parent = &pdev->dev;
1321 pxa_soc_camera_host.nr = pdev->id;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001322 err = soc_camera_host_register(&pxa_soc_camera_host);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001323 if (err)
1324 goto exit_free_irq;
1325
1326 return 0;
1327
1328exit_free_irq:
1329 free_irq(pcdev->irq, pcdev);
1330exit_free_dma:
Mike Rapoporta5462e52008-04-22 10:36:32 -03001331 pxa_free_dma(pcdev->dma_chans[2]);
1332exit_free_dma_u:
1333 pxa_free_dma(pcdev->dma_chans[1]);
1334exit_free_dma_y:
1335 pxa_free_dma(pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001336exit_iounmap:
1337 iounmap(base);
1338exit_release:
1339 release_mem_region(res->start, res->end - res->start + 1);
1340exit_clk:
1341 clk_put(pcdev->clk);
1342exit_kfree:
1343 kfree(pcdev);
1344exit:
1345 return err;
1346}
1347
1348static int __devexit pxa_camera_remove(struct platform_device *pdev)
1349{
1350 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1351 struct resource *res;
1352
1353 clk_put(pcdev->clk);
1354
Mike Rapoporta5462e52008-04-22 10:36:32 -03001355 pxa_free_dma(pcdev->dma_chans[0]);
1356 pxa_free_dma(pcdev->dma_chans[1]);
1357 pxa_free_dma(pcdev->dma_chans[2]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001358 free_irq(pcdev->irq, pcdev);
1359
1360 soc_camera_host_unregister(&pxa_soc_camera_host);
1361
1362 iounmap(pcdev->base);
1363
1364 res = pcdev->res;
1365 release_mem_region(res->start, res->end - res->start + 1);
1366
1367 kfree(pcdev);
1368
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001369 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001370
1371 return 0;
1372}
1373
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001374static struct platform_driver pxa_camera_driver = {
1375 .driver = {
1376 .name = PXA_CAM_DRV_NAME,
1377 },
1378 .probe = pxa_camera_probe,
1379 .remove = __exit_p(pxa_camera_remove),
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001380};
1381
1382
1383static int __devinit pxa_camera_init(void)
1384{
1385 return platform_driver_register(&pxa_camera_driver);
1386}
1387
1388static void __exit pxa_camera_exit(void)
1389{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001390 platform_driver_unregister(&pxa_camera_driver);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001391}
1392
1393module_init(pxa_camera_init);
1394module_exit(pxa_camera_exit);
1395
1396MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1397MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1398MODULE_LICENSE("GPL");