blob: a19bb76e175d6694a0943e272aa00838600e740c [file] [log] [blame]
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001/*
2 * V4L2 Driver for PXA camera host
3 *
4 * Copyright (C) 2006, Sascha Hauer, Pengutronix
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030013#include <linux/init.h>
14#include <linux/module.h>
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -030015#include <linux/io.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030016#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/errno.h>
19#include <linux/fs.h>
20#include <linux/interrupt.h>
21#include <linux/kernel.h>
22#include <linux/mm.h>
23#include <linux/moduleparam.h>
24#include <linux/time.h>
25#include <linux/version.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030028#include <linux/clk.h>
29
30#include <media/v4l2-common.h>
31#include <media/v4l2-dev.h>
Paulius Zaleckas092d3922008-07-11 20:50:31 -030032#include <media/videobuf-dma-sg.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030033#include <media/soc_camera.h>
34
35#include <linux/videodev2.h>
36
Eric Miaocfbaf4d2009-01-02 12:16:02 -030037#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/camera.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030039
40#define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
41#define PXA_CAM_DRV_NAME "pxa27x-camera"
42
Eric Miao5ca11fa2008-12-18 11:15:50 -030043/* Camera Interface */
44#define CICR0 0x0000
45#define CICR1 0x0004
46#define CICR2 0x0008
47#define CICR3 0x000C
48#define CICR4 0x0010
49#define CISR 0x0014
50#define CIFR 0x0018
51#define CITOR 0x001C
52#define CIBR0 0x0028
53#define CIBR1 0x0030
54#define CIBR2 0x0038
55
56#define CICR0_DMAEN (1 << 31) /* DMA request enable */
57#define CICR0_PAR_EN (1 << 30) /* Parity enable */
58#define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
59#define CICR0_ENB (1 << 28) /* Camera interface enable */
60#define CICR0_DIS (1 << 27) /* Camera interface disable */
61#define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
62#define CICR0_TOM (1 << 9) /* Time-out mask */
63#define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
64#define CICR0_FEM (1 << 7) /* FIFO-empty mask */
65#define CICR0_EOLM (1 << 6) /* End-of-line mask */
66#define CICR0_PERRM (1 << 5) /* Parity-error mask */
67#define CICR0_QDM (1 << 4) /* Quick-disable mask */
68#define CICR0_CDM (1 << 3) /* Disable-done mask */
69#define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
70#define CICR0_EOFM (1 << 1) /* End-of-frame mask */
71#define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
72
73#define CICR1_TBIT (1 << 31) /* Transparency bit */
74#define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
75#define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
76#define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
77#define CICR1_RGB_F (1 << 11) /* RGB format */
78#define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
79#define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
80#define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
81#define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
82#define CICR1_DW (0x7 << 0) /* Data width mask */
83
84#define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
85 wait count mask */
86#define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
87 wait count mask */
88#define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
89#define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
90 wait count mask */
91#define CICR2_FSW (0x7 << 0) /* Frame stabilization
92 wait count mask */
93
94#define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
95 wait count mask */
96#define CICR3_EFW (0xff << 16) /* End-of-frame line clock
97 wait count mask */
98#define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
99#define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
100 wait count mask */
101#define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
102
103#define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
104#define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
105#define CICR4_PCP (1 << 22) /* Pixel clock polarity */
106#define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
107#define CICR4_VSP (1 << 20) /* Vertical sync polarity */
108#define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
109#define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
110#define CICR4_DIV (0xff << 0) /* Clock divisor mask */
111
112#define CISR_FTO (1 << 15) /* FIFO time-out */
113#define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
114#define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
115#define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
116#define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
117#define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
118#define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
119#define CISR_EOL (1 << 8) /* End of line */
120#define CISR_PAR_ERR (1 << 7) /* Parity error */
121#define CISR_CQD (1 << 6) /* Camera interface quick disable */
122#define CISR_CDD (1 << 5) /* Camera interface disable done */
123#define CISR_SOF (1 << 4) /* Start of frame */
124#define CISR_EOF (1 << 3) /* End of frame */
125#define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
126#define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
127#define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
128
129#define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
130#define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
131#define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
132#define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
133#define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
134#define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
135#define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
136#define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
137
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300138#define CICR0_SIM_MP (0 << 24)
139#define CICR0_SIM_SP (1 << 24)
140#define CICR0_SIM_MS (2 << 24)
141#define CICR0_SIM_EP (3 << 24)
142#define CICR0_SIM_ES (4 << 24)
143
144#define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
145#define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300146#define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
147#define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
148#define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300149
150#define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
151#define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
152#define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
153#define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
154#define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
155
156#define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
157#define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
158#define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
159#define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
160
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300161#define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
162 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
163 CICR0_EOFM | CICR0_FOM)
164
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300165/*
166 * Structures
167 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300168enum pxa_camera_active_dma {
169 DMA_Y = 0x1,
170 DMA_U = 0x2,
171 DMA_V = 0x4,
172};
173
174/* descriptor needed for the PXA DMA engine */
175struct pxa_cam_dma {
176 dma_addr_t sg_dma;
177 struct pxa_dma_desc *sg_cpu;
178 size_t sg_size;
179 int sglen;
180};
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300181
182/* buffer for one video frame */
183struct pxa_buffer {
184 /* common v4l buffer stuff -- must be first */
185 struct videobuf_buffer vb;
186
187 const struct soc_camera_data_format *fmt;
188
Mike Rapoporta5462e52008-04-22 10:36:32 -0300189 /* our descriptor lists for Y, U and V channels */
190 struct pxa_cam_dma dmas[3];
191
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300192 int inwork;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300193
194 enum pxa_camera_active_dma active_dma;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300195};
196
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300197struct pxa_camera_dev {
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -0300198 struct soc_camera_host soc_host;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300199 /* PXA27x is only supposed to handle one camera on its Quick Capture
200 * interface. If anyone ever builds hardware to enable more than
201 * one camera, they will have to modify this driver too */
202 struct soc_camera_device *icd;
203 struct clk *clk;
204
205 unsigned int irq;
206 void __iomem *base;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300207
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300208 int channels;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300209 unsigned int dma_chans[3];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300210
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300211 struct pxacamera_platform_data *pdata;
212 struct resource *res;
213 unsigned long platform_flags;
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300214 unsigned long ciclk;
215 unsigned long mclk;
216 u32 mclk_divisor;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300217
218 struct list_head capture;
219
220 spinlock_t lock;
221
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300222 struct pxa_buffer *active;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300223 struct pxa_dma_desc *sg_tail[3];
Robert Jarzmik3f6ac492008-08-02 07:10:04 -0300224
225 u32 save_cicr[5];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300226};
227
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300228struct pxa_cam {
229 unsigned long flags;
230};
231
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300232static const char *pxa_cam_driver_description = "PXA_Camera";
233
234static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
235
236/*
237 * Videobuf operations
238 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300239static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
240 unsigned int *size)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300241{
242 struct soc_camera_device *icd = vq->priv_data;
243
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300244 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300245
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300246 *size = roundup(icd->user_width * icd->user_height *
Robert Jarzmik92a83372009-03-31 03:44:21 -0300247 ((icd->current_fmt->depth + 7) >> 3), 8);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300248
249 if (0 == *count)
250 *count = 32;
251 while (*size * *count > vid_limit * 1024 * 1024)
252 (*count)--;
253
254 return 0;
255}
256
257static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
258{
259 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300260 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300261 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300262 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300263
264 BUG_ON(in_interrupt());
265
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300266 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300267 &buf->vb, buf->vb.baddr, buf->vb.bsize);
268
269 /* This waits until this buffer is out of danger, i.e., until it is no
270 * longer in STATE_QUEUED or STATE_ACTIVE */
271 videobuf_waiton(&buf->vb, 0, 0);
272 videobuf_dma_unmap(vq, dma);
273 videobuf_dma_free(dma);
274
Mike Rapoporta5462e52008-04-22 10:36:32 -0300275 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
276 if (buf->dmas[i].sg_cpu)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300277 dma_free_coherent(ici->v4l2_dev.dev, buf->dmas[i].sg_size,
Mike Rapoporta5462e52008-04-22 10:36:32 -0300278 buf->dmas[i].sg_cpu,
279 buf->dmas[i].sg_dma);
280 buf->dmas[i].sg_cpu = NULL;
281 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300282
283 buf->vb.state = VIDEOBUF_NEEDS_INIT;
284}
285
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300286static int calculate_dma_sglen(struct scatterlist *sglist, int sglen,
287 int sg_first_ofs, int size)
288{
289 int i, offset, dma_len, xfer_len;
290 struct scatterlist *sg;
291
292 offset = sg_first_ofs;
293 for_each_sg(sglist, sg, sglen, i) {
294 dma_len = sg_dma_len(sg);
295
296 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
297 xfer_len = roundup(min(dma_len - offset, size), 8);
298
299 size = max(0, size - xfer_len);
300 offset = 0;
301 if (size == 0)
302 break;
303 }
304
305 BUG_ON(size != 0);
306 return i + 1;
307}
308
309/**
310 * pxa_init_dma_channel - init dma descriptors
311 * @pcdev: pxa camera device
312 * @buf: pxa buffer to find pxa dma channel
313 * @dma: dma video buffer
314 * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
315 * @cibr: camera Receive Buffer Register
316 * @size: bytes to transfer
317 * @sg_first: first element of sg_list
318 * @sg_first_ofs: offset in first element of sg_list
319 *
320 * Prepares the pxa dma descriptors to transfer one camera channel.
321 * Beware sg_first and sg_first_ofs are both input and output parameters.
322 *
323 * Returns 0 or -ENOMEM if no coherent memory is available
324 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300325static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
326 struct pxa_buffer *buf,
327 struct videobuf_dmabuf *dma, int channel,
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300328 int cibr, int size,
329 struct scatterlist **sg_first, int *sg_first_ofs)
Mike Rapoporta5462e52008-04-22 10:36:32 -0300330{
331 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300332 struct device *dev = pcdev->soc_host.v4l2_dev.dev;
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300333 struct scatterlist *sg;
334 int i, offset, sglen;
335 int dma_len = 0, xfer_len = 0;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300336
337 if (pxa_dma->sg_cpu)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300338 dma_free_coherent(dev, pxa_dma->sg_size,
Mike Rapoporta5462e52008-04-22 10:36:32 -0300339 pxa_dma->sg_cpu, pxa_dma->sg_dma);
340
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300341 sglen = calculate_dma_sglen(*sg_first, dma->sglen,
342 *sg_first_ofs, size);
343
Mike Rapoporta5462e52008-04-22 10:36:32 -0300344 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300345 pxa_dma->sg_cpu = dma_alloc_coherent(dev, pxa_dma->sg_size,
Mike Rapoporta5462e52008-04-22 10:36:32 -0300346 &pxa_dma->sg_dma, GFP_KERNEL);
347 if (!pxa_dma->sg_cpu)
348 return -ENOMEM;
349
350 pxa_dma->sglen = sglen;
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300351 offset = *sg_first_ofs;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300352
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300353 dev_dbg(dev, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n",
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300354 *sg_first, sglen, *sg_first_ofs, pxa_dma->sg_dma);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300355
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300356
357 for_each_sg(*sg_first, sg, sglen, i) {
358 dma_len = sg_dma_len(sg);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300359
360 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300361 xfer_len = roundup(min(dma_len - offset, size), 8);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300362
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300363 size = max(0, size - xfer_len);
364
365 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
366 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(sg) + offset;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300367 pxa_dma->sg_cpu[i].dcmd =
368 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300369#ifdef DEBUG
370 if (!i)
371 pxa_dma->sg_cpu[i].dcmd |= DCMD_STARTIRQEN;
372#endif
Mike Rapoporta5462e52008-04-22 10:36:32 -0300373 pxa_dma->sg_cpu[i].ddadr =
374 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300375
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300376 dev_vdbg(dev, "DMA: desc.%08x->@phys=0x%08x, len=%d\n",
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300377 pxa_dma->sg_dma + i * sizeof(struct pxa_dma_desc),
378 sg_dma_address(sg) + offset, xfer_len);
379 offset = 0;
380
381 if (size == 0)
382 break;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300383 }
384
Robert Jarzmik256b0232009-03-31 03:44:21 -0300385 pxa_dma->sg_cpu[sglen].ddadr = DDADR_STOP;
386 pxa_dma->sg_cpu[sglen].dcmd = DCMD_FLOWSRC | DCMD_BURST8 | DCMD_ENDIRQEN;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300387
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300388 /*
389 * Handle 1 special case :
390 * - in 3 planes (YUV422P format), we might finish with xfer_len equal
391 * to dma_len (end on PAGE boundary). In this case, the sg element
392 * for next plane should be the next after the last used to store the
393 * last scatter gather RAM page
394 */
395 if (xfer_len >= dma_len) {
396 *sg_first_ofs = xfer_len - dma_len;
397 *sg_first = sg_next(sg);
398 } else {
399 *sg_first_ofs = xfer_len;
400 *sg_first = sg;
401 }
402
Mike Rapoporta5462e52008-04-22 10:36:32 -0300403 return 0;
404}
405
Robert Jarzmik256b0232009-03-31 03:44:21 -0300406static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev,
407 struct pxa_buffer *buf)
408{
409 buf->active_dma = DMA_Y;
410 if (pcdev->channels == 3)
411 buf->active_dma |= DMA_U | DMA_V;
412}
413
414/*
415 * Please check the DMA prepared buffer structure in :
416 * Documentation/video4linux/pxa_camera.txt
417 * Please check also in pxa_camera_check_link_miss() to understand why DMA chain
418 * modification while DMA chain is running will work anyway.
419 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300420static int pxa_videobuf_prepare(struct videobuf_queue *vq,
421 struct videobuf_buffer *vb, enum v4l2_field field)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300422{
423 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300424 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300425 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300426 struct device *dev = pcdev->soc_host.v4l2_dev.dev;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300427 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300428 int ret;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300429 int size_y, size_u = 0, size_v = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300430
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300431 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300432 vb, vb->baddr, vb->bsize);
433
434 /* Added list head initialization on alloc */
435 WARN_ON(!list_empty(&vb->queue));
436
437#ifdef DEBUG
438 /* This can be useful if you want to see if we actually fill
439 * the buffer with something */
440 memset((void *)vb->baddr, 0xaa, vb->bsize);
441#endif
442
443 BUG_ON(NULL == icd->current_fmt);
444
445 /* I think, in buf_prepare you only have to protect global data,
446 * the actual buffer is yours */
447 buf->inwork = 1;
448
449 if (buf->fmt != icd->current_fmt ||
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300450 vb->width != icd->user_width ||
451 vb->height != icd->user_height ||
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300452 vb->field != field) {
453 buf->fmt = icd->current_fmt;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300454 vb->width = icd->user_width;
455 vb->height = icd->user_height;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300456 vb->field = field;
457 vb->state = VIDEOBUF_NEEDS_INIT;
458 }
459
460 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
461 if (0 != vb->baddr && vb->bsize < vb->size) {
462 ret = -EINVAL;
463 goto out;
464 }
465
466 if (vb->state == VIDEOBUF_NEEDS_INIT) {
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300467 int size = vb->size;
468 int next_ofs = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300469 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300470 struct scatterlist *sg;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300471
472 ret = videobuf_iolock(vq, vb, NULL);
473 if (ret)
474 goto fail;
475
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300476 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300477 size_y = size / 2;
478 size_u = size_v = size / 4;
479 } else {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300480 size_y = size;
481 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300482
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300483 sg = dma->sglist;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300484
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300485 /* init DMA for Y channel */
486 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, CIBR0, size_y,
487 &sg, &next_ofs);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300488 if (ret) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300489 dev_err(dev, "DMA initialization for Y/RGB failed\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300490 goto fail;
491 }
492
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300493 /* init DMA for U channel */
494 if (size_u)
495 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, CIBR1,
496 size_u, &sg, &next_ofs);
497 if (ret) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300498 dev_err(dev, "DMA initialization for U failed\n");
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300499 goto fail_u;
500 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300501
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300502 /* init DMA for V channel */
503 if (size_v)
504 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, CIBR2,
505 size_v, &sg, &next_ofs);
506 if (ret) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300507 dev_err(dev, "DMA initialization for V failed\n");
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300508 goto fail_v;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300509 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300510
511 vb->state = VIDEOBUF_PREPARED;
512 }
513
514 buf->inwork = 0;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300515 pxa_videobuf_set_actdma(pcdev, buf);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300516
517 return 0;
518
Mike Rapoporta5462e52008-04-22 10:36:32 -0300519fail_v:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300520 dma_free_coherent(dev, buf->dmas[1].sg_size,
Mike Rapoporta5462e52008-04-22 10:36:32 -0300521 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
522fail_u:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300523 dma_free_coherent(dev, buf->dmas[0].sg_size,
Mike Rapoporta5462e52008-04-22 10:36:32 -0300524 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300525fail:
526 free_buffer(vq, buf);
527out:
528 buf->inwork = 0;
529 return ret;
530}
531
Robert Jarzmik256b0232009-03-31 03:44:21 -0300532/**
533 * pxa_dma_start_channels - start DMA channel for active buffer
534 * @pcdev: pxa camera device
535 *
536 * Initialize DMA channels to the beginning of the active video buffer, and
537 * start these channels.
538 */
539static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev)
540{
541 int i;
542 struct pxa_buffer *active;
543
544 active = pcdev->active;
545
546 for (i = 0; i < pcdev->channels; i++) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300547 dev_dbg(pcdev->soc_host.v4l2_dev.dev,
548 "%s (channel=%d) ddadr=%08x\n", __func__,
Robert Jarzmik256b0232009-03-31 03:44:21 -0300549 i, active->dmas[i].sg_dma);
550 DDADR(pcdev->dma_chans[i]) = active->dmas[i].sg_dma;
551 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
552 }
553}
554
555static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev)
556{
557 int i;
558
559 for (i = 0; i < pcdev->channels; i++) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300560 dev_dbg(pcdev->soc_host.v4l2_dev.dev,
561 "%s (channel=%d)\n", __func__, i);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300562 DCSR(pcdev->dma_chans[i]) = 0;
563 }
564}
565
Robert Jarzmik256b0232009-03-31 03:44:21 -0300566static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev,
567 struct pxa_buffer *buf)
568{
569 int i;
570 struct pxa_dma_desc *buf_last_desc;
571
572 for (i = 0; i < pcdev->channels; i++) {
573 buf_last_desc = buf->dmas[i].sg_cpu + buf->dmas[i].sglen;
574 buf_last_desc->ddadr = DDADR_STOP;
575
Guennadi Liakhovetskiae7410e2009-03-31 03:44:22 -0300576 if (pcdev->sg_tail[i])
577 /* Link the new buffer to the old tail */
578 pcdev->sg_tail[i]->ddadr = buf->dmas[i].sg_dma;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300579
Guennadi Liakhovetskiae7410e2009-03-31 03:44:22 -0300580 /* Update the channel tail */
581 pcdev->sg_tail[i] = buf_last_desc;
582 }
Robert Jarzmik256b0232009-03-31 03:44:21 -0300583}
584
585/**
586 * pxa_camera_start_capture - start video capturing
587 * @pcdev: camera device
588 *
589 * Launch capturing. DMA channels should not be active yet. They should get
590 * activated at the end of frame interrupt, to capture only whole frames, and
591 * never begin the capture of a partial frame.
592 */
593static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev)
594{
595 unsigned long cicr0, cifr;
596
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300597 dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300598 /* Reset the FIFOs */
599 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
600 __raw_writel(cifr, pcdev->base + CIFR);
601 /* Enable End-Of-Frame Interrupt */
602 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
603 cicr0 &= ~CICR0_EOFM;
604 __raw_writel(cicr0, pcdev->base + CICR0);
605}
606
607static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev)
608{
609 unsigned long cicr0;
610
611 pxa_dma_stop_channels(pcdev);
612
613 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
614 __raw_writel(cicr0, pcdev->base + CICR0);
615
Robert Jarzmik8c62e222009-03-31 03:44:22 -0300616 pcdev->active = NULL;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300617 dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300618}
619
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300620/* Called under spinlock_irqsave(&pcdev->lock, ...) */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300621static void pxa_videobuf_queue(struct videobuf_queue *vq,
622 struct videobuf_buffer *vb)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300623{
624 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300625 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300626 struct pxa_camera_dev *pcdev = ici->priv;
627 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300628
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300629 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d active=%p\n",
630 __func__, vb, vb->baddr, vb->bsize, pcdev->active);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300631
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300632 list_add_tail(&vb->queue, &pcdev->capture);
633
634 vb->state = VIDEOBUF_ACTIVE;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300635 pxa_dma_add_tail_buf(pcdev, buf);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300636
Robert Jarzmik256b0232009-03-31 03:44:21 -0300637 if (!pcdev->active)
638 pxa_camera_start_capture(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300639}
640
641static void pxa_videobuf_release(struct videobuf_queue *vq,
642 struct videobuf_buffer *vb)
643{
644 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
645#ifdef DEBUG
646 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300647 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300648
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300649 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300650 vb, vb->baddr, vb->bsize);
651
652 switch (vb->state) {
653 case VIDEOBUF_ACTIVE:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300654 dev_dbg(dev, "%s (active)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300655 break;
656 case VIDEOBUF_QUEUED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300657 dev_dbg(dev, "%s (queued)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300658 break;
659 case VIDEOBUF_PREPARED:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300660 dev_dbg(dev, "%s (prepared)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300661 break;
662 default:
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300663 dev_dbg(dev, "%s (unknown)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300664 break;
665 }
666#endif
667
668 free_buffer(vq, buf);
669}
670
Mike Rapoporta5462e52008-04-22 10:36:32 -0300671static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
672 struct videobuf_buffer *vb,
673 struct pxa_buffer *buf)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300674{
Robert Jarzmik256b0232009-03-31 03:44:21 -0300675 int i;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300676
Mike Rapoporta5462e52008-04-22 10:36:32 -0300677 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
678 list_del_init(&vb->queue);
679 vb->state = VIDEOBUF_DONE;
680 do_gettimeofday(&vb->ts);
681 vb->field_count++;
682 wake_up(&vb->done);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300683 dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s dequeud buffer (vb=0x%p)\n",
684 __func__, vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300685
686 if (list_empty(&pcdev->capture)) {
Robert Jarzmik256b0232009-03-31 03:44:21 -0300687 pxa_camera_stop_capture(pcdev);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300688 for (i = 0; i < pcdev->channels; i++)
689 pcdev->sg_tail[i] = NULL;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300690 return;
691 }
692
693 pcdev->active = list_entry(pcdev->capture.next,
694 struct pxa_buffer, vb.queue);
695}
696
Robert Jarzmik256b0232009-03-31 03:44:21 -0300697/**
698 * pxa_camera_check_link_miss - check missed DMA linking
699 * @pcdev: camera device
700 *
701 * The DMA chaining is done with DMA running. This means a tiny temporal window
702 * remains, where a buffer is queued on the chain, while the chain is already
703 * stopped. This means the tailed buffer would never be transfered by DMA.
704 * This function restarts the capture for this corner case, where :
705 * - DADR() == DADDR_STOP
706 * - a videobuffer is queued on the pcdev->capture list
707 *
708 * Please check the "DMA hot chaining timeslice issue" in
709 * Documentation/video4linux/pxa_camera.txt
710 *
711 * Context: should only be called within the dma irq handler
712 */
713static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev)
714{
715 int i, is_dma_stopped = 1;
716
717 for (i = 0; i < pcdev->channels; i++)
718 if (DDADR(pcdev->dma_chans[i]) != DDADR_STOP)
719 is_dma_stopped = 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300720 dev_dbg(pcdev->soc_host.v4l2_dev.dev,
721 "%s : top queued buffer=%p, dma_stopped=%d\n",
Robert Jarzmik256b0232009-03-31 03:44:21 -0300722 __func__, pcdev->active, is_dma_stopped);
723 if (pcdev->active && is_dma_stopped)
724 pxa_camera_start_capture(pcdev);
725}
726
Mike Rapoporta5462e52008-04-22 10:36:32 -0300727static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
728 enum pxa_camera_active_dma act_dma)
729{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300730 struct device *dev = pcdev->soc_host.v4l2_dev.dev;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300731 struct pxa_buffer *buf;
732 unsigned long flags;
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300733 u32 status, camera_status, overrun;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300734 struct videobuf_buffer *vb;
735
736 spin_lock_irqsave(&pcdev->lock, flags);
737
Mike Rapoporta5462e52008-04-22 10:36:32 -0300738 status = DCSR(channel);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300739 DCSR(channel) = status;
740
741 camera_status = __raw_readl(pcdev->base + CISR);
742 overrun = CISR_IFO_0;
743 if (pcdev->channels == 3)
744 overrun |= CISR_IFO_1 | CISR_IFO_2;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300745
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300746 if (status & DCSR_BUSERR) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300747 dev_err(dev, "DMA Bus Error IRQ!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300748 goto out;
749 }
750
Robert Jarzmik256b0232009-03-31 03:44:21 -0300751 if (!(status & (DCSR_ENDINTR | DCSR_STARTINTR))) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300752 dev_err(dev, "Unknown DMA IRQ source, status: 0x%08x\n",
753 status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300754 goto out;
755 }
756
Robert Jarzmik8c62e222009-03-31 03:44:22 -0300757 /*
758 * pcdev->active should not be NULL in DMA irq handler.
759 *
760 * But there is one corner case : if capture was stopped due to an
761 * overrun of channel 1, and at that same channel 2 was completed.
762 *
763 * When handling the overrun in DMA irq for channel 1, we'll stop the
764 * capture and restart it (and thus set pcdev->active to NULL). But the
765 * DMA irq handler will already be pending for channel 2. So on entering
766 * the DMA irq handler for channel 2 there will be no active buffer, yet
767 * that is normal.
768 */
769 if (!pcdev->active)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300770 goto out;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300771
772 vb = &pcdev->active->vb;
773 buf = container_of(vb, struct pxa_buffer, vb);
774 WARN_ON(buf->inwork || list_empty(&vb->queue));
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300775
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300776 dev_dbg(dev, "%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n",
Robert Jarzmik256b0232009-03-31 03:44:21 -0300777 __func__, channel, status & DCSR_STARTINTR ? "SOF " : "",
778 status & DCSR_ENDINTR ? "EOF " : "", vb, DDADR(channel));
779
780 if (status & DCSR_ENDINTR) {
Robert Jarzmik8c62e222009-03-31 03:44:22 -0300781 /*
782 * It's normal if the last frame creates an overrun, as there
783 * are no more DMA descriptors to fetch from QCI fifos
784 */
785 if (camera_status & overrun &&
786 !list_is_last(pcdev->capture.next, &pcdev->capture)) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300787 dev_dbg(dev, "FIFO overrun! CISR: %x\n",
Robert Jarzmik256b0232009-03-31 03:44:21 -0300788 camera_status);
789 pxa_camera_stop_capture(pcdev);
790 pxa_camera_start_capture(pcdev);
791 goto out;
792 }
793 buf->active_dma &= ~act_dma;
794 if (!buf->active_dma) {
795 pxa_camera_wakeup(pcdev, vb, buf);
796 pxa_camera_check_link_miss(pcdev);
797 }
798 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300799
800out:
801 spin_unlock_irqrestore(&pcdev->lock, flags);
802}
803
Mike Rapoporta5462e52008-04-22 10:36:32 -0300804static void pxa_camera_dma_irq_y(int channel, void *data)
805{
806 struct pxa_camera_dev *pcdev = data;
807 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
808}
809
810static void pxa_camera_dma_irq_u(int channel, void *data)
811{
812 struct pxa_camera_dev *pcdev = data;
813 pxa_camera_dma_irq(channel, pcdev, DMA_U);
814}
815
816static void pxa_camera_dma_irq_v(int channel, void *data)
817{
818 struct pxa_camera_dev *pcdev = data;
819 pxa_camera_dma_irq(channel, pcdev, DMA_V);
820}
821
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300822static struct videobuf_queue_ops pxa_videobuf_ops = {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300823 .buf_setup = pxa_videobuf_setup,
824 .buf_prepare = pxa_videobuf_prepare,
825 .buf_queue = pxa_videobuf_queue,
826 .buf_release = pxa_videobuf_release,
827};
828
Magnus Damma034d1b2008-07-11 20:59:34 -0300829static void pxa_camera_init_videobuf(struct videobuf_queue *q,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300830 struct soc_camera_device *icd)
831{
Magnus Damma034d1b2008-07-11 20:59:34 -0300832 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
833 struct pxa_camera_dev *pcdev = ici->priv;
834
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300835 /* We must pass NULL as dev pointer, then all pci_* dma operations
836 * transform to normal dma_* ones. */
Magnus Damma034d1b2008-07-11 20:59:34 -0300837 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300838 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
839 sizeof(struct pxa_buffer), icd);
840}
841
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300842static u32 mclk_get_divisor(struct platform_device *pdev,
843 struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300844{
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300845 unsigned long mclk = pcdev->mclk;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300846 struct device *dev = &pdev->dev;
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300847 u32 div;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300848 unsigned long lcdclk;
849
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300850 lcdclk = clk_get_rate(pcdev->clk);
851 pcdev->ciclk = lcdclk;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300852
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300853 /* mclk <= ciclk / 4 (27.4.2) */
854 if (mclk > lcdclk / 4) {
855 mclk = lcdclk / 4;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300856 dev_warn(dev, "Limiting master clock to %lu\n", mclk);
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300857 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300858
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300859 /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
860 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
861
862 /* If we're not supplying MCLK, leave it at 0 */
863 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
864 pcdev->mclk = lcdclk / (2 * (div + 1));
865
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300866 dev_dbg(dev, "LCD clock %luHz, target freq %luHz, divisor %u\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300867 lcdclk, mclk, div);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300868
869 return div;
870}
871
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300872static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
873 unsigned long pclk)
874{
875 /* We want a timeout > 1 pixel time, not ">=" */
876 u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
877
878 __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
879}
880
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300881static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300882{
883 struct pxacamera_platform_data *pdata = pcdev->pdata;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300884 struct device *dev = pcdev->soc_host.v4l2_dev.dev;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300885 u32 cicr4 = 0;
886
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300887 dev_dbg(dev, "Registered platform device at %p data %p\n",
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300888 pcdev, pdata);
889
890 if (pdata && pdata->init) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300891 dev_dbg(dev, "%s: Init gpios\n", __func__);
892 pdata->init(dev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300893 }
894
Eric Miao5ca11fa2008-12-18 11:15:50 -0300895 /* disable all interrupts */
896 __raw_writel(0x3ff, pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300897
898 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
899 cicr4 |= CICR4_PCLK_EN;
900 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
901 cicr4 |= CICR4_MCLK_EN;
902 if (pcdev->platform_flags & PXA_CAMERA_PCP)
903 cicr4 |= CICR4_PCP;
904 if (pcdev->platform_flags & PXA_CAMERA_HSP)
905 cicr4 |= CICR4_HSP;
906 if (pcdev->platform_flags & PXA_CAMERA_VSP)
907 cicr4 |= CICR4_VSP;
908
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300909 __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
910
911 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
912 /* Initialise the timeout under the assumption pclk = mclk */
913 recalculate_fifo_timeout(pcdev, pcdev->mclk);
914 else
915 /* "Safe default" - 13MHz */
916 recalculate_fifo_timeout(pcdev, 13000000);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300917
918 clk_enable(pcdev->clk);
919}
920
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300921static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300922{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300923 clk_disable(pcdev->clk);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300924}
925
926static irqreturn_t pxa_camera_irq(int irq, void *data)
927{
928 struct pxa_camera_dev *pcdev = data;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300929 unsigned long status, cicr0;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300930 struct pxa_buffer *buf;
931 struct videobuf_buffer *vb;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300932
Eric Miao5ca11fa2008-12-18 11:15:50 -0300933 status = __raw_readl(pcdev->base + CISR);
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300934 dev_dbg(pcdev->soc_host.v4l2_dev.dev,
935 "Camera interrupt status 0x%lx\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300936
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300937 if (!status)
938 return IRQ_NONE;
939
Eric Miao5ca11fa2008-12-18 11:15:50 -0300940 __raw_writel(status, pcdev->base + CISR);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300941
942 if (status & CISR_EOF) {
Robert Jarzmik256b0232009-03-31 03:44:21 -0300943 pcdev->active = list_first_entry(&pcdev->capture,
944 struct pxa_buffer, vb.queue);
945 vb = &pcdev->active->vb;
946 buf = container_of(vb, struct pxa_buffer, vb);
947 pxa_videobuf_set_actdma(pcdev, buf);
948
949 pxa_dma_start_channels(pcdev);
950
Eric Miao5ca11fa2008-12-18 11:15:50 -0300951 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
952 __raw_writel(cicr0, pcdev->base + CICR0);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300953 }
954
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300955 return IRQ_HANDLED;
956}
957
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300958/*
959 * The following two functions absolutely depend on the fact, that
960 * there can be only one camera on PXA quick capture interface
961 * Called with .video_lock held
962 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300963static int pxa_camera_add_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300964{
965 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
966 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300967
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300968 if (pcdev->icd)
969 return -EBUSY;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300970
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300971 pxa_camera_activate(pcdev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300972
973 pcdev->icd = icd;
974
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300975 dev_info(icd->dev.parent, "PXA Camera driver attached to camera %d\n",
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300976 icd->devnum);
977
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300978 return 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300979}
980
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300981/* Called with .video_lock held */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300982static void pxa_camera_remove_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300983{
984 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
985 struct pxa_camera_dev *pcdev = ici->priv;
986
987 BUG_ON(icd != pcdev->icd);
988
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300989 dev_info(icd->dev.parent, "PXA Camera driver detached from camera %d\n",
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300990 icd->devnum);
991
992 /* disable capture, disable interrupts */
Eric Miao5ca11fa2008-12-18 11:15:50 -0300993 __raw_writel(0x3ff, pcdev->base + CICR0);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300994
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300995 /* Stop DMA engine */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300996 DCSR(pcdev->dma_chans[0]) = 0;
997 DCSR(pcdev->dma_chans[1]) = 0;
998 DCSR(pcdev->dma_chans[2]) = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300999
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001000 pxa_camera_deactivate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001001
1002 pcdev->icd = NULL;
1003}
1004
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001005static int test_platform_param(struct pxa_camera_dev *pcdev,
1006 unsigned char buswidth, unsigned long *flags)
1007{
1008 /*
1009 * Platform specified synchronization and pixel clock polarities are
1010 * only a recommendation and are only used during probing. The PXA270
1011 * quick capture interface supports both.
1012 */
1013 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1014 SOCAM_MASTER : SOCAM_SLAVE) |
1015 SOCAM_HSYNC_ACTIVE_HIGH |
1016 SOCAM_HSYNC_ACTIVE_LOW |
1017 SOCAM_VSYNC_ACTIVE_HIGH |
1018 SOCAM_VSYNC_ACTIVE_LOW |
Guennadi Liakhovetski2d9329f2009-02-23 12:12:58 -03001019 SOCAM_DATA_ACTIVE_HIGH |
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001020 SOCAM_PCLK_SAMPLE_RISING |
1021 SOCAM_PCLK_SAMPLE_FALLING;
1022
1023 /* If requested data width is supported by the platform, use it */
1024 switch (buswidth) {
1025 case 10:
1026 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
1027 return -EINVAL;
1028 *flags |= SOCAM_DATAWIDTH_10;
1029 break;
1030 case 9:
1031 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
1032 return -EINVAL;
1033 *flags |= SOCAM_DATAWIDTH_9;
1034 break;
1035 case 8:
1036 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
1037 return -EINVAL;
1038 *flags |= SOCAM_DATAWIDTH_8;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001039 break;
1040 default:
1041 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001042 }
1043
1044 return 0;
1045}
1046
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001047static void pxa_camera_setup_cicr(struct soc_camera_device *icd,
1048 unsigned long flags, __u32 pixfmt)
1049{
1050 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1051 struct pxa_camera_dev *pcdev = ici->priv;
1052 unsigned long dw, bpp;
1053 u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0;
1054
1055 /* Datawidth is now guaranteed to be equal to one of the three values.
1056 * We fix bit-per-pixel equal to data-width... */
1057 switch (flags & SOCAM_DATAWIDTH_MASK) {
1058 case SOCAM_DATAWIDTH_10:
1059 dw = 4;
1060 bpp = 0x40;
1061 break;
1062 case SOCAM_DATAWIDTH_9:
1063 dw = 3;
1064 bpp = 0x20;
1065 break;
1066 default:
1067 /* Actually it can only be 8 now,
1068 * default is just to silence compiler warnings */
1069 case SOCAM_DATAWIDTH_8:
1070 dw = 2;
1071 bpp = 0;
1072 }
1073
1074 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1075 cicr4 |= CICR4_PCLK_EN;
1076 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
1077 cicr4 |= CICR4_MCLK_EN;
1078 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
1079 cicr4 |= CICR4_PCP;
1080 if (flags & SOCAM_HSYNC_ACTIVE_LOW)
1081 cicr4 |= CICR4_HSP;
1082 if (flags & SOCAM_VSYNC_ACTIVE_LOW)
1083 cicr4 |= CICR4_VSP;
1084
1085 cicr0 = __raw_readl(pcdev->base + CICR0);
1086 if (cicr0 & CICR0_ENB)
1087 __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
1088
1089 cicr1 = CICR1_PPL_VAL(icd->user_width - 1) | bpp | dw;
1090
1091 switch (pixfmt) {
1092 case V4L2_PIX_FMT_YUV422P:
1093 pcdev->channels = 3;
1094 cicr1 |= CICR1_YCBCR_F;
1095 /*
1096 * Normally, pxa bus wants as input UYVY format. We allow all
1097 * reorderings of the YUV422 format, as no processing is done,
1098 * and the YUV stream is just passed through without any
1099 * transformation. Note that UYVY is the only format that
1100 * should be used if pxa framebuffer Overlay2 is used.
1101 */
1102 case V4L2_PIX_FMT_UYVY:
1103 case V4L2_PIX_FMT_VYUY:
1104 case V4L2_PIX_FMT_YUYV:
1105 case V4L2_PIX_FMT_YVYU:
1106 cicr1 |= CICR1_COLOR_SP_VAL(2);
1107 break;
1108 case V4L2_PIX_FMT_RGB555:
1109 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
1110 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
1111 break;
1112 case V4L2_PIX_FMT_RGB565:
1113 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
1114 break;
1115 }
1116
1117 cicr2 = 0;
1118 cicr3 = CICR3_LPF_VAL(icd->user_height - 1) |
1119 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
1120 cicr4 |= pcdev->mclk_divisor;
1121
1122 __raw_writel(cicr1, pcdev->base + CICR1);
1123 __raw_writel(cicr2, pcdev->base + CICR2);
1124 __raw_writel(cicr3, pcdev->base + CICR3);
1125 __raw_writel(cicr4, pcdev->base + CICR4);
1126
1127 /* CIF interrupts are not used, only DMA */
1128 cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1129 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
1130 cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
1131 __raw_writel(cicr0, pcdev->base + CICR0);
1132}
1133
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001134static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001135{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001136 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001137 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001138 unsigned long bus_flags, camera_flags, common_flags;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001139 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001140 struct pxa_cam *cam = icd->host_priv;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001141
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001142 if (ret < 0)
1143 return ret;
1144
1145 camera_flags = icd->ops->query_bus_param(icd);
1146
1147 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
1148 if (!common_flags)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001149 return -EINVAL;
1150
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -03001151 pcdev->channels = 1;
1152
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001153 /* Make choises, based on platform preferences */
1154 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
1155 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
1156 if (pcdev->platform_flags & PXA_CAMERA_HSP)
1157 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
1158 else
1159 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
1160 }
1161
1162 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
1163 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
1164 if (pcdev->platform_flags & PXA_CAMERA_VSP)
1165 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
1166 else
1167 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
1168 }
1169
1170 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
1171 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
1172 if (pcdev->platform_flags & PXA_CAMERA_PCP)
1173 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
1174 else
1175 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
1176 }
1177
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001178 cam->flags = common_flags;
1179
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001180 ret = icd->ops->set_bus_param(icd, common_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001181 if (ret < 0)
1182 return ret;
1183
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001184 pxa_camera_setup_cicr(icd, common_flags, pixfmt);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001185
1186 return 0;
1187}
1188
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001189static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
1190 unsigned char buswidth)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001191{
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001192 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001193 struct pxa_camera_dev *pcdev = ici->priv;
1194 unsigned long bus_flags, camera_flags;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001195 int ret = test_platform_param(pcdev, buswidth, &bus_flags);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001196
1197 if (ret < 0)
1198 return ret;
1199
1200 camera_flags = icd->ops->query_bus_param(icd);
1201
1202 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
1203}
1204
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001205static const struct soc_camera_data_format pxa_camera_formats[] = {
1206 {
1207 .name = "Planar YUV422 16 bit",
1208 .depth = 16,
1209 .fourcc = V4L2_PIX_FMT_YUV422P,
1210 .colorspace = V4L2_COLORSPACE_JPEG,
1211 },
1212};
1213
1214static bool buswidth_supported(struct soc_camera_device *icd, int depth)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001215{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001216 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1217 struct pxa_camera_dev *pcdev = ici->priv;
1218
1219 switch (depth) {
1220 case 8:
1221 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
1222 case 9:
1223 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
1224 case 10:
1225 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
1226 }
1227 return false;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001228}
1229
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001230static int required_buswidth(const struct soc_camera_data_format *fmt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001231{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001232 switch (fmt->fourcc) {
1233 case V4L2_PIX_FMT_UYVY:
1234 case V4L2_PIX_FMT_VYUY:
1235 case V4L2_PIX_FMT_YUYV:
1236 case V4L2_PIX_FMT_YVYU:
1237 case V4L2_PIX_FMT_RGB565:
1238 case V4L2_PIX_FMT_RGB555:
1239 return 8;
1240 default:
1241 return fmt->depth;
1242 }
1243}
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001244
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001245static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
1246 struct soc_camera_format_xlate *xlate)
1247{
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001248 struct device *dev = icd->dev.parent;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001249 int formats = 0, buswidth, ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001250 struct pxa_cam *cam;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001251
1252 buswidth = required_buswidth(icd->formats + idx);
1253
1254 if (!buswidth_supported(icd, buswidth))
1255 return 0;
1256
1257 ret = pxa_camera_try_bus_param(icd, buswidth);
1258 if (ret < 0)
1259 return 0;
1260
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001261 if (!icd->host_priv) {
1262 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1263 if (!cam)
1264 return -ENOMEM;
1265
1266 icd->host_priv = cam;
1267 } else {
1268 cam = icd->host_priv;
1269 }
1270
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001271 switch (icd->formats[idx].fourcc) {
1272 case V4L2_PIX_FMT_UYVY:
1273 formats++;
1274 if (xlate) {
1275 xlate->host_fmt = &pxa_camera_formats[0];
1276 xlate->cam_fmt = icd->formats + idx;
1277 xlate->buswidth = buswidth;
1278 xlate++;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001279 dev_dbg(dev, "Providing format %s using %s\n",
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001280 pxa_camera_formats[0].name,
1281 icd->formats[idx].name);
1282 }
1283 case V4L2_PIX_FMT_VYUY:
1284 case V4L2_PIX_FMT_YUYV:
1285 case V4L2_PIX_FMT_YVYU:
1286 case V4L2_PIX_FMT_RGB565:
1287 case V4L2_PIX_FMT_RGB555:
1288 formats++;
1289 if (xlate) {
1290 xlate->host_fmt = icd->formats + idx;
1291 xlate->cam_fmt = icd->formats + idx;
1292 xlate->buswidth = buswidth;
1293 xlate++;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001294 dev_dbg(dev, "Providing format %s packed\n",
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001295 icd->formats[idx].name);
1296 }
1297 break;
1298 default:
1299 /* Generic pass-through */
1300 formats++;
1301 if (xlate) {
1302 xlate->host_fmt = icd->formats + idx;
1303 xlate->cam_fmt = icd->formats + idx;
1304 xlate->buswidth = icd->formats[idx].depth;
1305 xlate++;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001306 dev_dbg(dev,
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001307 "Providing format %s in pass-through mode\n",
1308 icd->formats[idx].name);
1309 }
1310 }
1311
1312 return formats;
1313}
1314
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001315static void pxa_camera_put_formats(struct soc_camera_device *icd)
1316{
1317 kfree(icd->host_priv);
1318 icd->host_priv = NULL;
1319}
1320
1321static int pxa_camera_check_frame(struct v4l2_pix_format *pix)
1322{
1323 /* limit to pxa hardware capabilities */
1324 return pix->height < 32 || pix->height > 2048 || pix->width < 48 ||
1325 pix->width > 2048 || (pix->width & 0x01);
1326}
1327
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001328static int pxa_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001329 struct v4l2_crop *a)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001330{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001331 struct v4l2_rect *rect = &a->c;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001332 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001333 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001334 struct device *dev = icd->dev.parent;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001335 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001336 struct soc_camera_sense sense = {
1337 .master_clock = pcdev->mclk,
1338 .pixel_clock_max = pcdev->ciclk / 4,
1339 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001340 struct v4l2_format f;
1341 struct v4l2_pix_format *pix = &f.fmt.pix, pix_tmp;
1342 struct pxa_cam *cam = icd->host_priv;
Guennadi Liakhovetski0ad675e2009-02-23 12:11:25 -03001343 int ret;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001344
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001345 /* If PCLK is used to latch data from the sensor, check sense */
1346 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1347 icd->sense = &sense;
1348
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -03001349 ret = v4l2_subdev_call(sd, video, s_crop, a);
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001350
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001351 icd->sense = NULL;
1352
1353 if (ret < 0) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001354 dev_warn(dev, "Failed to crop to %ux%u@%u:%u\n",
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001355 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001356 return ret;
1357 }
1358
1359 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1360
1361 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1362 if (ret < 0)
1363 return ret;
1364
1365 pix_tmp = *pix;
1366 if (pxa_camera_check_frame(pix)) {
1367 /*
1368 * Camera cropping produced a frame beyond our capabilities.
1369 * FIXME: just extract a subframe, that we can process.
1370 */
1371 v4l_bound_align_image(&pix->width, 48, 2048, 1,
1372 &pix->height, 32, 2048, 0,
1373 icd->current_fmt->fourcc == V4L2_PIX_FMT_YUV422P ?
1374 4 : 0);
1375 ret = v4l2_subdev_call(sd, video, s_fmt, &f);
1376 if (ret < 0)
1377 return ret;
1378
1379 if (pxa_camera_check_frame(pix)) {
1380 dev_warn(icd->dev.parent,
1381 "Inconsistent state. Use S_FMT to repair\n");
1382 return -EINVAL;
1383 }
1384 }
1385
1386 if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001387 if (sense.pixel_clock > sense.pixel_clock_max) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001388 dev_err(dev,
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001389 "pixel clock %lu set by the camera too high!",
1390 sense.pixel_clock);
1391 return -EIO;
1392 }
1393 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1394 }
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001395
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001396 icd->user_width = pix->width;
1397 icd->user_height = pix->height;
1398
1399 pxa_camera_setup_cicr(icd, cam->flags, icd->current_fmt->fourcc);
1400
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001401 return ret;
1402}
1403
1404static int pxa_camera_set_fmt(struct soc_camera_device *icd,
1405 struct v4l2_format *f)
1406{
1407 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1408 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001409 struct device *dev = icd->dev.parent;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001410 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001411 const struct soc_camera_data_format *cam_fmt = NULL;
1412 const struct soc_camera_format_xlate *xlate = NULL;
1413 struct soc_camera_sense sense = {
1414 .master_clock = pcdev->mclk,
1415 .pixel_clock_max = pcdev->ciclk / 4,
1416 };
1417 struct v4l2_pix_format *pix = &f->fmt.pix;
1418 struct v4l2_format cam_f = *f;
1419 int ret;
1420
1421 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1422 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001423 dev_warn(dev, "Format %x not found\n", pix->pixelformat);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001424 return -EINVAL;
1425 }
1426
1427 cam_fmt = xlate->cam_fmt;
1428
1429 /* If PCLK is used to latch data from the sensor, check sense */
1430 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1431 icd->sense = &sense;
1432
1433 cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001434 ret = v4l2_subdev_call(sd, video, s_fmt, f);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001435
1436 icd->sense = NULL;
1437
1438 if (ret < 0) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001439 dev_warn(dev, "Failed to configure for format %x\n",
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001440 pix->pixelformat);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001441 } else if (pxa_camera_check_frame(pix)) {
1442 dev_warn(dev,
1443 "Camera driver produced an unsupported frame %dx%d\n",
1444 pix->width, pix->height);
1445 ret = -EINVAL;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001446 } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1447 if (sense.pixel_clock > sense.pixel_clock_max) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001448 dev_err(dev,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001449 "pixel clock %lu set by the camera too high!",
1450 sense.pixel_clock);
1451 return -EIO;
1452 }
1453 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1454 }
1455
1456 if (!ret) {
Guennadi Liakhovetski0ad675e2009-02-23 12:11:25 -03001457 icd->buswidth = xlate->buswidth;
1458 icd->current_fmt = xlate->host_fmt;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001459 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001460
1461 return ret;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001462}
1463
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001464static int pxa_camera_try_fmt(struct soc_camera_device *icd,
1465 struct v4l2_format *f)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001466{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001467 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001468 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001469 const struct soc_camera_format_xlate *xlate;
1470 struct v4l2_pix_format *pix = &f->fmt.pix;
1471 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -03001472 enum v4l2_field field;
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001473 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001474
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001475 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1476 if (!xlate) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001477 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001478 return -EINVAL;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001479 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001480
Robert Jarzmik92a83372009-03-31 03:44:21 -03001481 /*
Trent Piepho4a6b8df2009-05-30 21:45:46 -03001482 * Limit to pxa hardware capabilities. YUV422P planar format requires
1483 * images size to be a multiple of 16 bytes. If not, zeros will be
1484 * inserted between Y and U planes, and U and V planes, which violates
1485 * the YUV422P standard.
Robert Jarzmik92a83372009-03-31 03:44:21 -03001486 */
Trent Piepho4a6b8df2009-05-30 21:45:46 -03001487 v4l_bound_align_image(&pix->width, 48, 2048, 1,
1488 &pix->height, 32, 2048, 0,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001489 pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
Robert Jarzmik92a83372009-03-31 03:44:21 -03001490
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001491 pix->bytesperline = pix->width *
1492 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1493 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001494
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001495 /* camera has to see its format, but the user the original one */
1496 pix->pixelformat = xlate->cam_fmt->fourcc;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001497 /* limit to sensor capabilities */
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -03001498 ret = v4l2_subdev_call(sd, video, try_fmt, f);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001499 pix->pixelformat = pixfmt;
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001500
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -03001501 field = pix->field;
1502
1503 if (field == V4L2_FIELD_ANY) {
1504 pix->field = V4L2_FIELD_NONE;
1505 } else if (field != V4L2_FIELD_NONE) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001506 dev_err(icd->dev.parent, "Field type %d unsupported.\n", field);
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -03001507 return -EINVAL;
1508 }
1509
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001510 return ret;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001511}
1512
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001513static int pxa_camera_reqbufs(struct soc_camera_file *icf,
1514 struct v4l2_requestbuffers *p)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001515{
1516 int i;
1517
1518 /* This is for locking debugging only. I removed spinlocks and now I
1519 * check whether .prepare is ever called on a linked buffer, or whether
1520 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1521 * it hadn't triggered */
1522 for (i = 0; i < p->count; i++) {
1523 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1524 struct pxa_buffer, vb);
1525 buf->inwork = 0;
1526 INIT_LIST_HEAD(&buf->vb.queue);
1527 }
1528
1529 return 0;
1530}
1531
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001532static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001533{
1534 struct soc_camera_file *icf = file->private_data;
1535 struct pxa_buffer *buf;
1536
1537 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
1538 vb.stream);
1539
1540 poll_wait(file, &buf->vb.done, pt);
1541
1542 if (buf->vb.state == VIDEOBUF_DONE ||
1543 buf->vb.state == VIDEOBUF_ERROR)
1544 return POLLIN|POLLRDNORM;
1545
1546 return 0;
1547}
1548
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001549static int pxa_camera_querycap(struct soc_camera_host *ici,
1550 struct v4l2_capability *cap)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001551{
1552 /* cap->name is set by the firendly caller:-> */
1553 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1554 cap->version = PXA_CAM_VERSION_CODE;
1555 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1556
1557 return 0;
1558}
1559
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001560static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1561{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001562 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001563 struct pxa_camera_dev *pcdev = ici->priv;
1564 int i = 0, ret = 0;
1565
Eric Miao5ca11fa2008-12-18 11:15:50 -03001566 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
1567 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
1568 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
1569 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
1570 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001571
1572 if ((pcdev->icd) && (pcdev->icd->ops->suspend))
1573 ret = pcdev->icd->ops->suspend(pcdev->icd, state);
1574
1575 return ret;
1576}
1577
1578static int pxa_camera_resume(struct soc_camera_device *icd)
1579{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001580 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001581 struct pxa_camera_dev *pcdev = ici->priv;
1582 int i = 0, ret = 0;
1583
Eric Miao87f3dd72008-09-08 15:26:43 +08001584 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1585 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1586 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001587
Eric Miao5ca11fa2008-12-18 11:15:50 -03001588 __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
1589 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
1590 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
1591 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
1592 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001593
1594 if ((pcdev->icd) && (pcdev->icd->ops->resume))
1595 ret = pcdev->icd->ops->resume(pcdev->icd);
1596
1597 /* Restart frame capture if active buffer exists */
Robert Jarzmik256b0232009-03-31 03:44:21 -03001598 if (!ret && pcdev->active)
1599 pxa_camera_start_capture(pcdev);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001600
1601 return ret;
1602}
1603
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001604static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1605 .owner = THIS_MODULE,
1606 .add = pxa_camera_add_device,
1607 .remove = pxa_camera_remove_device,
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001608 .suspend = pxa_camera_suspend,
1609 .resume = pxa_camera_resume,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001610 .set_crop = pxa_camera_set_crop,
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001611 .get_formats = pxa_camera_get_formats,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001612 .put_formats = pxa_camera_put_formats,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001613 .set_fmt = pxa_camera_set_fmt,
1614 .try_fmt = pxa_camera_try_fmt,
Paulius Zaleckas092d3922008-07-11 20:50:31 -03001615 .init_videobuf = pxa_camera_init_videobuf,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001616 .reqbufs = pxa_camera_reqbufs,
1617 .poll = pxa_camera_poll,
1618 .querycap = pxa_camera_querycap,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001619 .set_bus_param = pxa_camera_set_bus_param,
1620};
1621
Jean Delvaree36bc312009-06-04 11:07:16 -03001622static int __devinit pxa_camera_probe(struct platform_device *pdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001623{
1624 struct pxa_camera_dev *pcdev;
1625 struct resource *res;
1626 void __iomem *base;
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001627 int irq;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001628 int err = 0;
1629
1630 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1631 irq = platform_get_irq(pdev, 0);
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001632 if (!res || irq < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001633 err = -ENODEV;
1634 goto exit;
1635 }
1636
1637 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1638 if (!pcdev) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001639 dev_err(&pdev->dev, "Could not allocate pcdev\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001640 err = -ENOMEM;
1641 goto exit;
1642 }
1643
Russell Kinge0d8b132008-11-11 17:52:32 +00001644 pcdev->clk = clk_get(&pdev->dev, NULL);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001645 if (IS_ERR(pcdev->clk)) {
1646 err = PTR_ERR(pcdev->clk);
1647 goto exit_kfree;
1648 }
1649
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001650 pcdev->res = res;
1651
1652 pcdev->pdata = pdev->dev.platform_data;
1653 pcdev->platform_flags = pcdev->pdata->flags;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001654 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1655 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001656 /* Platform hasn't set available data widths. This is bad.
1657 * Warn and use a default. */
1658 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1659 "data widths, using default 10 bit\n");
1660 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1661 }
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001662 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
1663 if (!pcdev->mclk) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001664 dev_warn(&pdev->dev,
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001665 "mclk == 0! Please, fix your platform data. "
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001666 "Using default 20MHz\n");
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001667 pcdev->mclk = 20000000;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001668 }
1669
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001670 pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev);
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001671
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001672 INIT_LIST_HEAD(&pcdev->capture);
1673 spin_lock_init(&pcdev->lock);
1674
1675 /*
1676 * Request the regions.
1677 */
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001678 if (!request_mem_region(res->start, resource_size(res),
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001679 PXA_CAM_DRV_NAME)) {
1680 err = -EBUSY;
1681 goto exit_clk;
1682 }
1683
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001684 base = ioremap(res->start, resource_size(res));
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001685 if (!base) {
1686 err = -ENOMEM;
1687 goto exit_release;
1688 }
1689 pcdev->irq = irq;
1690 pcdev->base = base;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001691
1692 /* request dma */
roel kluinde3e3b82008-09-18 17:50:15 -03001693 err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1694 pxa_camera_dma_irq_y, pcdev);
1695 if (err < 0) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001696 dev_err(&pdev->dev, "Can't request DMA for Y\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001697 goto exit_iounmap;
1698 }
roel kluinde3e3b82008-09-18 17:50:15 -03001699 pcdev->dma_chans[0] = err;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001700 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001701
roel kluinde3e3b82008-09-18 17:50:15 -03001702 err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1703 pxa_camera_dma_irq_u, pcdev);
1704 if (err < 0) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001705 dev_err(&pdev->dev, "Can't request DMA for U\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001706 goto exit_free_dma_y;
1707 }
roel kluinde3e3b82008-09-18 17:50:15 -03001708 pcdev->dma_chans[1] = err;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001709 dev_dbg(&pdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
Mike Rapoporta5462e52008-04-22 10:36:32 -03001710
roel kluinde3e3b82008-09-18 17:50:15 -03001711 err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1712 pxa_camera_dma_irq_v, pcdev);
1713 if (err < 0) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001714 dev_err(&pdev->dev, "Can't request DMA for V\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001715 goto exit_free_dma_u;
1716 }
roel kluinde3e3b82008-09-18 17:50:15 -03001717 pcdev->dma_chans[2] = err;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001718 dev_dbg(&pdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
Mike Rapoporta5462e52008-04-22 10:36:32 -03001719
Eric Miao87f3dd72008-09-08 15:26:43 +08001720 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1721 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1722 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001723
1724 /* request irq */
1725 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1726 pcdev);
1727 if (err) {
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001728 dev_err(&pdev->dev, "Camera interrupt register failed \n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001729 goto exit_free_dma;
1730 }
1731
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001732 pcdev->soc_host.drv_name = PXA_CAM_DRV_NAME;
1733 pcdev->soc_host.ops = &pxa_soc_camera_host_ops;
1734 pcdev->soc_host.priv = pcdev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001735 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001736 pcdev->soc_host.nr = pdev->id;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001737
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001738 err = soc_camera_host_register(&pcdev->soc_host);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001739 if (err)
1740 goto exit_free_irq;
1741
1742 return 0;
1743
1744exit_free_irq:
1745 free_irq(pcdev->irq, pcdev);
1746exit_free_dma:
Mike Rapoporta5462e52008-04-22 10:36:32 -03001747 pxa_free_dma(pcdev->dma_chans[2]);
1748exit_free_dma_u:
1749 pxa_free_dma(pcdev->dma_chans[1]);
1750exit_free_dma_y:
1751 pxa_free_dma(pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001752exit_iounmap:
1753 iounmap(base);
1754exit_release:
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001755 release_mem_region(res->start, resource_size(res));
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001756exit_clk:
1757 clk_put(pcdev->clk);
1758exit_kfree:
1759 kfree(pcdev);
1760exit:
1761 return err;
1762}
1763
1764static int __devexit pxa_camera_remove(struct platform_device *pdev)
1765{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001766 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1767 struct pxa_camera_dev *pcdev = container_of(soc_host,
1768 struct pxa_camera_dev, soc_host);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001769 struct resource *res;
1770
1771 clk_put(pcdev->clk);
1772
Mike Rapoporta5462e52008-04-22 10:36:32 -03001773 pxa_free_dma(pcdev->dma_chans[0]);
1774 pxa_free_dma(pcdev->dma_chans[1]);
1775 pxa_free_dma(pcdev->dma_chans[2]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001776 free_irq(pcdev->irq, pcdev);
1777
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001778 soc_camera_host_unregister(soc_host);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001779
1780 iounmap(pcdev->base);
1781
1782 res = pcdev->res;
Guennadi Liakhovetskieb6c8552009-04-24 12:55:18 -03001783 release_mem_region(res->start, resource_size(res));
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001784
1785 kfree(pcdev);
1786
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001787 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001788
1789 return 0;
1790}
1791
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001792static struct platform_driver pxa_camera_driver = {
1793 .driver = {
1794 .name = PXA_CAM_DRV_NAME,
1795 },
1796 .probe = pxa_camera_probe,
Jean Delvaree36bc312009-06-04 11:07:16 -03001797 .remove = __devexit_p(pxa_camera_remove),
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001798};
1799
1800
Jean Delvaree36bc312009-06-04 11:07:16 -03001801static int __init pxa_camera_init(void)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001802{
1803 return platform_driver_register(&pxa_camera_driver);
1804}
1805
1806static void __exit pxa_camera_exit(void)
1807{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001808 platform_driver_unregister(&pxa_camera_driver);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001809}
1810
1811module_init(pxa_camera_init);
1812module_exit(pxa_camera_exit);
1813
1814MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1815MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1816MODULE_LICENSE("GPL");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001817MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME);