Commit | Line | Data |
---|---|---|
3bc43840 GL |
1 | /* |
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 | ||
3bc43840 GL |
13 | #include <linux/init.h> |
14 | #include <linux/module.h> | |
7102b773 | 15 | #include <linux/io.h> |
3bc43840 GL |
16 | #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> | |
3bc43840 GL |
28 | #include <linux/clk.h> |
29 | ||
30 | #include <media/v4l2-common.h> | |
31 | #include <media/v4l2-dev.h> | |
092d3921 | 32 | #include <media/videobuf-dma-sg.h> |
3bc43840 GL |
33 | #include <media/soc_camera.h> |
34 | ||
35 | #include <linux/videodev2.h> | |
36 | ||
cfbaf4df | 37 | #include <mach/dma.h> |
a09e64fb | 38 | #include <mach/camera.h> |
3bc43840 GL |
39 | |
40 | #define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5) | |
41 | #define PXA_CAM_DRV_NAME "pxa27x-camera" | |
42 | ||
5ca11fa3 EM |
43 | /* 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 | ||
7102b773 GL |
138 | #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 */ | |
a5462e5b MR |
146 | #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 */ | |
7102b773 GL |
149 | |
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 | ||
3bc43840 GL |
161 | #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 | ||
3bc43840 GL |
165 | /* |
166 | * Structures | |
167 | */ | |
a5462e5b MR |
168 | enum 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 */ | |
175 | struct 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 | }; | |
3bc43840 GL |
181 | |
182 | /* buffer for one video frame */ | |
183 | struct 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 | ||
a5462e5b MR |
189 | /* our descriptor lists for Y, U and V channels */ |
190 | struct pxa_cam_dma dmas[3]; | |
191 | ||
3bc43840 | 192 | int inwork; |
a5462e5b MR |
193 | |
194 | enum pxa_camera_active_dma active_dma; | |
3bc43840 GL |
195 | }; |
196 | ||
3bc43840 | 197 | struct pxa_camera_dev { |
eb6c8558 | 198 | struct soc_camera_host soc_host; |
3bc43840 GL |
199 | /* 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; | |
a5462e5b | 207 | |
e7c50688 | 208 | int channels; |
a5462e5b | 209 | unsigned int dma_chans[3]; |
3bc43840 | 210 | |
3bc43840 GL |
211 | struct pxacamera_platform_data *pdata; |
212 | struct resource *res; | |
213 | unsigned long platform_flags; | |
cf34cba7 GL |
214 | unsigned long ciclk; |
215 | unsigned long mclk; | |
216 | u32 mclk_divisor; | |
3bc43840 GL |
217 | |
218 | struct list_head capture; | |
219 | ||
220 | spinlock_t lock; | |
221 | ||
3bc43840 | 222 | struct pxa_buffer *active; |
5aa2110f | 223 | struct pxa_dma_desc *sg_tail[3]; |
3f6ac497 RJ |
224 | |
225 | u32 save_cicr[5]; | |
3bc43840 GL |
226 | }; |
227 | ||
228 | static const char *pxa_cam_driver_description = "PXA_Camera"; | |
229 | ||
230 | static unsigned int vid_limit = 16; /* Video memory limit, in Mb */ | |
231 | ||
232 | /* | |
233 | * Videobuf operations | |
234 | */ | |
7102b773 GL |
235 | static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count, |
236 | unsigned int *size) | |
3bc43840 GL |
237 | { |
238 | struct soc_camera_device *icd = vq->priv_data; | |
239 | ||
240 | dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size); | |
241 | ||
92a8337b RJ |
242 | *size = roundup(icd->width * icd->height * |
243 | ((icd->current_fmt->depth + 7) >> 3), 8); | |
3bc43840 GL |
244 | |
245 | if (0 == *count) | |
246 | *count = 32; | |
247 | while (*size * *count > vid_limit * 1024 * 1024) | |
248 | (*count)--; | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
253 | static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf) | |
254 | { | |
255 | struct soc_camera_device *icd = vq->priv_data; | |
64f5905e | 256 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3bc43840 | 257 | struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); |
a5462e5b | 258 | int i; |
3bc43840 GL |
259 | |
260 | BUG_ON(in_interrupt()); | |
261 | ||
7e28adb2 | 262 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
3bc43840 GL |
263 | &buf->vb, buf->vb.baddr, buf->vb.bsize); |
264 | ||
265 | /* This waits until this buffer is out of danger, i.e., until it is no | |
266 | * longer in STATE_QUEUED or STATE_ACTIVE */ | |
267 | videobuf_waiton(&buf->vb, 0, 0); | |
268 | videobuf_dma_unmap(vq, dma); | |
269 | videobuf_dma_free(dma); | |
270 | ||
a5462e5b MR |
271 | for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) { |
272 | if (buf->dmas[i].sg_cpu) | |
eff505fa | 273 | dma_free_coherent(ici->dev, buf->dmas[i].sg_size, |
a5462e5b MR |
274 | buf->dmas[i].sg_cpu, |
275 | buf->dmas[i].sg_dma); | |
276 | buf->dmas[i].sg_cpu = NULL; | |
277 | } | |
3bc43840 GL |
278 | |
279 | buf->vb.state = VIDEOBUF_NEEDS_INIT; | |
280 | } | |
281 | ||
37f5aefd RJ |
282 | static int calculate_dma_sglen(struct scatterlist *sglist, int sglen, |
283 | int sg_first_ofs, int size) | |
284 | { | |
285 | int i, offset, dma_len, xfer_len; | |
286 | struct scatterlist *sg; | |
287 | ||
288 | offset = sg_first_ofs; | |
289 | for_each_sg(sglist, sg, sglen, i) { | |
290 | dma_len = sg_dma_len(sg); | |
291 | ||
292 | /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */ | |
293 | xfer_len = roundup(min(dma_len - offset, size), 8); | |
294 | ||
295 | size = max(0, size - xfer_len); | |
296 | offset = 0; | |
297 | if (size == 0) | |
298 | break; | |
299 | } | |
300 | ||
301 | BUG_ON(size != 0); | |
302 | return i + 1; | |
303 | } | |
304 | ||
305 | /** | |
306 | * pxa_init_dma_channel - init dma descriptors | |
307 | * @pcdev: pxa camera device | |
308 | * @buf: pxa buffer to find pxa dma channel | |
309 | * @dma: dma video buffer | |
310 | * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V') | |
311 | * @cibr: camera Receive Buffer Register | |
312 | * @size: bytes to transfer | |
313 | * @sg_first: first element of sg_list | |
314 | * @sg_first_ofs: offset in first element of sg_list | |
315 | * | |
316 | * Prepares the pxa dma descriptors to transfer one camera channel. | |
317 | * Beware sg_first and sg_first_ofs are both input and output parameters. | |
318 | * | |
319 | * Returns 0 or -ENOMEM if no coherent memory is available | |
320 | */ | |
a5462e5b MR |
321 | static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev, |
322 | struct pxa_buffer *buf, | |
323 | struct videobuf_dmabuf *dma, int channel, | |
37f5aefd RJ |
324 | int cibr, int size, |
325 | struct scatterlist **sg_first, int *sg_first_ofs) | |
a5462e5b MR |
326 | { |
327 | struct pxa_cam_dma *pxa_dma = &buf->dmas[channel]; | |
37f5aefd RJ |
328 | struct scatterlist *sg; |
329 | int i, offset, sglen; | |
330 | int dma_len = 0, xfer_len = 0; | |
a5462e5b MR |
331 | |
332 | if (pxa_dma->sg_cpu) | |
eff505fa | 333 | dma_free_coherent(pcdev->soc_host.dev, pxa_dma->sg_size, |
a5462e5b MR |
334 | pxa_dma->sg_cpu, pxa_dma->sg_dma); |
335 | ||
37f5aefd RJ |
336 | sglen = calculate_dma_sglen(*sg_first, dma->sglen, |
337 | *sg_first_ofs, size); | |
338 | ||
a5462e5b | 339 | pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc); |
eff505fa | 340 | pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->soc_host.dev, pxa_dma->sg_size, |
a5462e5b MR |
341 | &pxa_dma->sg_dma, GFP_KERNEL); |
342 | if (!pxa_dma->sg_cpu) | |
343 | return -ENOMEM; | |
344 | ||
345 | pxa_dma->sglen = sglen; | |
37f5aefd | 346 | offset = *sg_first_ofs; |
a5462e5b | 347 | |
eff505fa | 348 | dev_dbg(pcdev->soc_host.dev, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n", |
37f5aefd | 349 | *sg_first, sglen, *sg_first_ofs, pxa_dma->sg_dma); |
a5462e5b | 350 | |
37f5aefd RJ |
351 | |
352 | for_each_sg(*sg_first, sg, sglen, i) { | |
353 | dma_len = sg_dma_len(sg); | |
a5462e5b MR |
354 | |
355 | /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */ | |
37f5aefd | 356 | xfer_len = roundup(min(dma_len - offset, size), 8); |
a5462e5b | 357 | |
37f5aefd RJ |
358 | size = max(0, size - xfer_len); |
359 | ||
360 | pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr; | |
361 | pxa_dma->sg_cpu[i].dtadr = sg_dma_address(sg) + offset; | |
a5462e5b MR |
362 | pxa_dma->sg_cpu[i].dcmd = |
363 | DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len; | |
256b0233 RJ |
364 | #ifdef DEBUG |
365 | if (!i) | |
366 | pxa_dma->sg_cpu[i].dcmd |= DCMD_STARTIRQEN; | |
367 | #endif | |
a5462e5b MR |
368 | pxa_dma->sg_cpu[i].ddadr = |
369 | pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc); | |
37f5aefd | 370 | |
eff505fa | 371 | dev_vdbg(pcdev->soc_host.dev, "DMA: desc.%08x->@phys=0x%08x, len=%d\n", |
37f5aefd RJ |
372 | pxa_dma->sg_dma + i * sizeof(struct pxa_dma_desc), |
373 | sg_dma_address(sg) + offset, xfer_len); | |
374 | offset = 0; | |
375 | ||
376 | if (size == 0) | |
377 | break; | |
a5462e5b MR |
378 | } |
379 | ||
256b0233 RJ |
380 | pxa_dma->sg_cpu[sglen].ddadr = DDADR_STOP; |
381 | pxa_dma->sg_cpu[sglen].dcmd = DCMD_FLOWSRC | DCMD_BURST8 | DCMD_ENDIRQEN; | |
a5462e5b | 382 | |
37f5aefd RJ |
383 | /* |
384 | * Handle 1 special case : | |
385 | * - in 3 planes (YUV422P format), we might finish with xfer_len equal | |
386 | * to dma_len (end on PAGE boundary). In this case, the sg element | |
387 | * for next plane should be the next after the last used to store the | |
388 | * last scatter gather RAM page | |
389 | */ | |
390 | if (xfer_len >= dma_len) { | |
391 | *sg_first_ofs = xfer_len - dma_len; | |
392 | *sg_first = sg_next(sg); | |
393 | } else { | |
394 | *sg_first_ofs = xfer_len; | |
395 | *sg_first = sg; | |
396 | } | |
397 | ||
a5462e5b MR |
398 | return 0; |
399 | } | |
400 | ||
256b0233 RJ |
401 | static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev, |
402 | struct pxa_buffer *buf) | |
403 | { | |
404 | buf->active_dma = DMA_Y; | |
405 | if (pcdev->channels == 3) | |
406 | buf->active_dma |= DMA_U | DMA_V; | |
407 | } | |
408 | ||
409 | /* | |
410 | * Please check the DMA prepared buffer structure in : | |
411 | * Documentation/video4linux/pxa_camera.txt | |
412 | * Please check also in pxa_camera_check_link_miss() to understand why DMA chain | |
413 | * modification while DMA chain is running will work anyway. | |
414 | */ | |
7102b773 GL |
415 | static int pxa_videobuf_prepare(struct videobuf_queue *vq, |
416 | struct videobuf_buffer *vb, enum v4l2_field field) | |
3bc43840 GL |
417 | { |
418 | struct soc_camera_device *icd = vq->priv_data; | |
64f5905e | 419 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3bc43840 GL |
420 | struct pxa_camera_dev *pcdev = ici->priv; |
421 | struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb); | |
a5462e5b | 422 | int ret; |
a5462e5b | 423 | int size_y, size_u = 0, size_v = 0; |
3bc43840 | 424 | |
7e28adb2 | 425 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
3bc43840 GL |
426 | vb, vb->baddr, vb->bsize); |
427 | ||
428 | /* Added list head initialization on alloc */ | |
429 | WARN_ON(!list_empty(&vb->queue)); | |
430 | ||
431 | #ifdef DEBUG | |
432 | /* This can be useful if you want to see if we actually fill | |
433 | * the buffer with something */ | |
434 | memset((void *)vb->baddr, 0xaa, vb->bsize); | |
435 | #endif | |
436 | ||
437 | BUG_ON(NULL == icd->current_fmt); | |
438 | ||
439 | /* I think, in buf_prepare you only have to protect global data, | |
440 | * the actual buffer is yours */ | |
441 | buf->inwork = 1; | |
442 | ||
443 | if (buf->fmt != icd->current_fmt || | |
444 | vb->width != icd->width || | |
445 | vb->height != icd->height || | |
446 | vb->field != field) { | |
447 | buf->fmt = icd->current_fmt; | |
448 | vb->width = icd->width; | |
449 | vb->height = icd->height; | |
450 | vb->field = field; | |
451 | vb->state = VIDEOBUF_NEEDS_INIT; | |
452 | } | |
453 | ||
454 | vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3); | |
455 | if (0 != vb->baddr && vb->bsize < vb->size) { | |
456 | ret = -EINVAL; | |
457 | goto out; | |
458 | } | |
459 | ||
460 | if (vb->state == VIDEOBUF_NEEDS_INIT) { | |
37f5aefd RJ |
461 | int size = vb->size; |
462 | int next_ofs = 0; | |
3bc43840 | 463 | struct videobuf_dmabuf *dma = videobuf_to_dma(vb); |
37f5aefd | 464 | struct scatterlist *sg; |
3bc43840 GL |
465 | |
466 | ret = videobuf_iolock(vq, vb, NULL); | |
467 | if (ret) | |
468 | goto fail; | |
469 | ||
5aa2110f | 470 | if (pcdev->channels == 3) { |
a5462e5b MR |
471 | size_y = size / 2; |
472 | size_u = size_v = size / 4; | |
473 | } else { | |
a5462e5b MR |
474 | size_y = size; |
475 | } | |
476 | ||
37f5aefd | 477 | sg = dma->sglist; |
3bc43840 | 478 | |
37f5aefd RJ |
479 | /* init DMA for Y channel */ |
480 | ret = pxa_init_dma_channel(pcdev, buf, dma, 0, CIBR0, size_y, | |
481 | &sg, &next_ofs); | |
a5462e5b | 482 | if (ret) { |
eff505fa | 483 | dev_err(pcdev->soc_host.dev, |
a5462e5b | 484 | "DMA initialization for Y/RGB failed\n"); |
3bc43840 GL |
485 | goto fail; |
486 | } | |
487 | ||
37f5aefd RJ |
488 | /* init DMA for U channel */ |
489 | if (size_u) | |
490 | ret = pxa_init_dma_channel(pcdev, buf, dma, 1, CIBR1, | |
491 | size_u, &sg, &next_ofs); | |
492 | if (ret) { | |
eff505fa | 493 | dev_err(pcdev->soc_host.dev, |
37f5aefd RJ |
494 | "DMA initialization for U failed\n"); |
495 | goto fail_u; | |
496 | } | |
497 | ||
498 | /* init DMA for V channel */ | |
499 | if (size_v) | |
500 | ret = pxa_init_dma_channel(pcdev, buf, dma, 2, CIBR2, | |
501 | size_v, &sg, &next_ofs); | |
502 | if (ret) { | |
eff505fa | 503 | dev_err(pcdev->soc_host.dev, |
37f5aefd RJ |
504 | "DMA initialization for V failed\n"); |
505 | goto fail_v; | |
3bc43840 | 506 | } |
3bc43840 GL |
507 | |
508 | vb->state = VIDEOBUF_PREPARED; | |
509 | } | |
510 | ||
511 | buf->inwork = 0; | |
256b0233 | 512 | pxa_videobuf_set_actdma(pcdev, buf); |
3bc43840 GL |
513 | |
514 | return 0; | |
515 | ||
a5462e5b | 516 | fail_v: |
eff505fa | 517 | dma_free_coherent(pcdev->soc_host.dev, buf->dmas[1].sg_size, |
a5462e5b MR |
518 | buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma); |
519 | fail_u: | |
eff505fa | 520 | dma_free_coherent(pcdev->soc_host.dev, buf->dmas[0].sg_size, |
a5462e5b | 521 | buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma); |
3bc43840 GL |
522 | fail: |
523 | free_buffer(vq, buf); | |
524 | out: | |
525 | buf->inwork = 0; | |
526 | return ret; | |
527 | } | |
528 | ||
256b0233 RJ |
529 | /** |
530 | * pxa_dma_start_channels - start DMA channel for active buffer | |
531 | * @pcdev: pxa camera device | |
532 | * | |
533 | * Initialize DMA channels to the beginning of the active video buffer, and | |
534 | * start these channels. | |
535 | */ | |
536 | static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev) | |
537 | { | |
538 | int i; | |
539 | struct pxa_buffer *active; | |
540 | ||
541 | active = pcdev->active; | |
542 | ||
543 | for (i = 0; i < pcdev->channels; i++) { | |
eff505fa | 544 | dev_dbg(pcdev->soc_host.dev, "%s (channel=%d) ddadr=%08x\n", __func__, |
256b0233 RJ |
545 | i, active->dmas[i].sg_dma); |
546 | DDADR(pcdev->dma_chans[i]) = active->dmas[i].sg_dma; | |
547 | DCSR(pcdev->dma_chans[i]) = DCSR_RUN; | |
548 | } | |
549 | } | |
550 | ||
551 | static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev) | |
552 | { | |
553 | int i; | |
554 | ||
555 | for (i = 0; i < pcdev->channels; i++) { | |
eff505fa | 556 | dev_dbg(pcdev->soc_host.dev, "%s (channel=%d)\n", __func__, i); |
256b0233 RJ |
557 | DCSR(pcdev->dma_chans[i]) = 0; |
558 | } | |
559 | } | |
560 | ||
256b0233 RJ |
561 | static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev, |
562 | struct pxa_buffer *buf) | |
563 | { | |
564 | int i; | |
565 | struct pxa_dma_desc *buf_last_desc; | |
566 | ||
567 | for (i = 0; i < pcdev->channels; i++) { | |
568 | buf_last_desc = buf->dmas[i].sg_cpu + buf->dmas[i].sglen; | |
569 | buf_last_desc->ddadr = DDADR_STOP; | |
570 | ||
ae7410e7 GL |
571 | if (pcdev->sg_tail[i]) |
572 | /* Link the new buffer to the old tail */ | |
573 | pcdev->sg_tail[i]->ddadr = buf->dmas[i].sg_dma; | |
256b0233 | 574 | |
ae7410e7 GL |
575 | /* Update the channel tail */ |
576 | pcdev->sg_tail[i] = buf_last_desc; | |
577 | } | |
256b0233 RJ |
578 | } |
579 | ||
580 | /** | |
581 | * pxa_camera_start_capture - start video capturing | |
582 | * @pcdev: camera device | |
583 | * | |
584 | * Launch capturing. DMA channels should not be active yet. They should get | |
585 | * activated at the end of frame interrupt, to capture only whole frames, and | |
586 | * never begin the capture of a partial frame. | |
587 | */ | |
588 | static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev) | |
589 | { | |
590 | unsigned long cicr0, cifr; | |
591 | ||
eff505fa | 592 | dev_dbg(pcdev->soc_host.dev, "%s\n", __func__); |
256b0233 RJ |
593 | /* Reset the FIFOs */ |
594 | cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F; | |
595 | __raw_writel(cifr, pcdev->base + CIFR); | |
596 | /* Enable End-Of-Frame Interrupt */ | |
597 | cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB; | |
598 | cicr0 &= ~CICR0_EOFM; | |
599 | __raw_writel(cicr0, pcdev->base + CICR0); | |
600 | } | |
601 | ||
602 | static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev) | |
603 | { | |
604 | unsigned long cicr0; | |
605 | ||
606 | pxa_dma_stop_channels(pcdev); | |
607 | ||
608 | cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB; | |
609 | __raw_writel(cicr0, pcdev->base + CICR0); | |
610 | ||
8c62e221 | 611 | pcdev->active = NULL; |
eff505fa | 612 | dev_dbg(pcdev->soc_host.dev, "%s\n", __func__); |
256b0233 RJ |
613 | } |
614 | ||
2dd54a54 | 615 | /* Called under spinlock_irqsave(&pcdev->lock, ...) */ |
7102b773 GL |
616 | static void pxa_videobuf_queue(struct videobuf_queue *vq, |
617 | struct videobuf_buffer *vb) | |
3bc43840 GL |
618 | { |
619 | struct soc_camera_device *icd = vq->priv_data; | |
64f5905e | 620 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3bc43840 GL |
621 | struct pxa_camera_dev *pcdev = ici->priv; |
622 | struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb); | |
3bc43840 | 623 | |
256b0233 RJ |
624 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d active=%p\n", __func__, |
625 | vb, vb->baddr, vb->bsize, pcdev->active); | |
626 | ||
3bc43840 GL |
627 | list_add_tail(&vb->queue, &pcdev->capture); |
628 | ||
629 | vb->state = VIDEOBUF_ACTIVE; | |
256b0233 | 630 | pxa_dma_add_tail_buf(pcdev, buf); |
3bc43840 | 631 | |
256b0233 RJ |
632 | if (!pcdev->active) |
633 | pxa_camera_start_capture(pcdev); | |
3bc43840 GL |
634 | } |
635 | ||
636 | static void pxa_videobuf_release(struct videobuf_queue *vq, | |
637 | struct videobuf_buffer *vb) | |
638 | { | |
639 | struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb); | |
640 | #ifdef DEBUG | |
641 | struct soc_camera_device *icd = vq->priv_data; | |
642 | ||
7e28adb2 | 643 | dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
3bc43840 GL |
644 | vb, vb->baddr, vb->bsize); |
645 | ||
646 | switch (vb->state) { | |
647 | case VIDEOBUF_ACTIVE: | |
7e28adb2 | 648 | dev_dbg(&icd->dev, "%s (active)\n", __func__); |
3bc43840 GL |
649 | break; |
650 | case VIDEOBUF_QUEUED: | |
7e28adb2 | 651 | dev_dbg(&icd->dev, "%s (queued)\n", __func__); |
3bc43840 GL |
652 | break; |
653 | case VIDEOBUF_PREPARED: | |
7e28adb2 | 654 | dev_dbg(&icd->dev, "%s (prepared)\n", __func__); |
3bc43840 GL |
655 | break; |
656 | default: | |
7e28adb2 | 657 | dev_dbg(&icd->dev, "%s (unknown)\n", __func__); |
3bc43840 GL |
658 | break; |
659 | } | |
660 | #endif | |
661 | ||
662 | free_buffer(vq, buf); | |
663 | } | |
664 | ||
a5462e5b MR |
665 | static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev, |
666 | struct videobuf_buffer *vb, | |
667 | struct pxa_buffer *buf) | |
668 | { | |
256b0233 | 669 | int i; |
5ca11fa3 | 670 | |
a5462e5b MR |
671 | /* _init is used to debug races, see comment in pxa_camera_reqbufs() */ |
672 | list_del_init(&vb->queue); | |
673 | vb->state = VIDEOBUF_DONE; | |
674 | do_gettimeofday(&vb->ts); | |
675 | vb->field_count++; | |
676 | wake_up(&vb->done); | |
eff505fa | 677 | dev_dbg(pcdev->soc_host.dev, "%s dequeud buffer (vb=0x%p)\n", __func__, vb); |
a5462e5b MR |
678 | |
679 | if (list_empty(&pcdev->capture)) { | |
256b0233 | 680 | pxa_camera_stop_capture(pcdev); |
256b0233 RJ |
681 | for (i = 0; i < pcdev->channels; i++) |
682 | pcdev->sg_tail[i] = NULL; | |
a5462e5b MR |
683 | return; |
684 | } | |
685 | ||
686 | pcdev->active = list_entry(pcdev->capture.next, | |
687 | struct pxa_buffer, vb.queue); | |
688 | } | |
689 | ||
256b0233 RJ |
690 | /** |
691 | * pxa_camera_check_link_miss - check missed DMA linking | |
692 | * @pcdev: camera device | |
693 | * | |
694 | * The DMA chaining is done with DMA running. This means a tiny temporal window | |
695 | * remains, where a buffer is queued on the chain, while the chain is already | |
696 | * stopped. This means the tailed buffer would never be transfered by DMA. | |
697 | * This function restarts the capture for this corner case, where : | |
698 | * - DADR() == DADDR_STOP | |
699 | * - a videobuffer is queued on the pcdev->capture list | |
700 | * | |
701 | * Please check the "DMA hot chaining timeslice issue" in | |
702 | * Documentation/video4linux/pxa_camera.txt | |
703 | * | |
704 | * Context: should only be called within the dma irq handler | |
705 | */ | |
706 | static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev) | |
707 | { | |
708 | int i, is_dma_stopped = 1; | |
709 | ||
710 | for (i = 0; i < pcdev->channels; i++) | |
711 | if (DDADR(pcdev->dma_chans[i]) != DDADR_STOP) | |
712 | is_dma_stopped = 0; | |
eff505fa | 713 | dev_dbg(pcdev->soc_host.dev, "%s : top queued buffer=%p, dma_stopped=%d\n", |
256b0233 RJ |
714 | __func__, pcdev->active, is_dma_stopped); |
715 | if (pcdev->active && is_dma_stopped) | |
716 | pxa_camera_start_capture(pcdev); | |
717 | } | |
718 | ||
a5462e5b MR |
719 | static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev, |
720 | enum pxa_camera_active_dma act_dma) | |
3bc43840 | 721 | { |
3bc43840 GL |
722 | struct pxa_buffer *buf; |
723 | unsigned long flags; | |
e7c50688 | 724 | u32 status, camera_status, overrun; |
3bc43840 GL |
725 | struct videobuf_buffer *vb; |
726 | ||
727 | spin_lock_irqsave(&pcdev->lock, flags); | |
728 | ||
a5462e5b | 729 | status = DCSR(channel); |
256b0233 RJ |
730 | DCSR(channel) = status; |
731 | ||
732 | camera_status = __raw_readl(pcdev->base + CISR); | |
733 | overrun = CISR_IFO_0; | |
734 | if (pcdev->channels == 3) | |
735 | overrun |= CISR_IFO_1 | CISR_IFO_2; | |
7102b773 | 736 | |
3bc43840 | 737 | if (status & DCSR_BUSERR) { |
eff505fa | 738 | dev_err(pcdev->soc_host.dev, "DMA Bus Error IRQ!\n"); |
3bc43840 GL |
739 | goto out; |
740 | } | |
741 | ||
256b0233 | 742 | if (!(status & (DCSR_ENDINTR | DCSR_STARTINTR))) { |
eff505fa | 743 | dev_err(pcdev->soc_host.dev, "Unknown DMA IRQ source, " |
7102b773 | 744 | "status: 0x%08x\n", status); |
3bc43840 GL |
745 | goto out; |
746 | } | |
747 | ||
8c62e221 RJ |
748 | /* |
749 | * pcdev->active should not be NULL in DMA irq handler. | |
750 | * | |
751 | * But there is one corner case : if capture was stopped due to an | |
752 | * overrun of channel 1, and at that same channel 2 was completed. | |
753 | * | |
754 | * When handling the overrun in DMA irq for channel 1, we'll stop the | |
755 | * capture and restart it (and thus set pcdev->active to NULL). But the | |
756 | * DMA irq handler will already be pending for channel 2. So on entering | |
757 | * the DMA irq handler for channel 2 there will be no active buffer, yet | |
758 | * that is normal. | |
759 | */ | |
760 | if (!pcdev->active) | |
3bc43840 | 761 | goto out; |
3bc43840 GL |
762 | |
763 | vb = &pcdev->active->vb; | |
764 | buf = container_of(vb, struct pxa_buffer, vb); | |
765 | WARN_ON(buf->inwork || list_empty(&vb->queue)); | |
3bc43840 | 766 | |
eff505fa | 767 | dev_dbg(pcdev->soc_host.dev, "%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n", |
256b0233 RJ |
768 | __func__, channel, status & DCSR_STARTINTR ? "SOF " : "", |
769 | status & DCSR_ENDINTR ? "EOF " : "", vb, DDADR(channel)); | |
770 | ||
771 | if (status & DCSR_ENDINTR) { | |
8c62e221 RJ |
772 | /* |
773 | * It's normal if the last frame creates an overrun, as there | |
774 | * are no more DMA descriptors to fetch from QCI fifos | |
775 | */ | |
776 | if (camera_status & overrun && | |
777 | !list_is_last(pcdev->capture.next, &pcdev->capture)) { | |
eff505fa | 778 | dev_dbg(pcdev->soc_host.dev, "FIFO overrun! CISR: %x\n", |
256b0233 RJ |
779 | camera_status); |
780 | pxa_camera_stop_capture(pcdev); | |
781 | pxa_camera_start_capture(pcdev); | |
782 | goto out; | |
783 | } | |
784 | buf->active_dma &= ~act_dma; | |
785 | if (!buf->active_dma) { | |
786 | pxa_camera_wakeup(pcdev, vb, buf); | |
787 | pxa_camera_check_link_miss(pcdev); | |
788 | } | |
789 | } | |
3bc43840 GL |
790 | |
791 | out: | |
792 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
793 | } | |
794 | ||
a5462e5b MR |
795 | static void pxa_camera_dma_irq_y(int channel, void *data) |
796 | { | |
797 | struct pxa_camera_dev *pcdev = data; | |
798 | pxa_camera_dma_irq(channel, pcdev, DMA_Y); | |
799 | } | |
800 | ||
801 | static void pxa_camera_dma_irq_u(int channel, void *data) | |
802 | { | |
803 | struct pxa_camera_dev *pcdev = data; | |
804 | pxa_camera_dma_irq(channel, pcdev, DMA_U); | |
805 | } | |
806 | ||
807 | static void pxa_camera_dma_irq_v(int channel, void *data) | |
808 | { | |
809 | struct pxa_camera_dev *pcdev = data; | |
810 | pxa_camera_dma_irq(channel, pcdev, DMA_V); | |
811 | } | |
812 | ||
7102b773 | 813 | static struct videobuf_queue_ops pxa_videobuf_ops = { |
3bc43840 GL |
814 | .buf_setup = pxa_videobuf_setup, |
815 | .buf_prepare = pxa_videobuf_prepare, | |
816 | .buf_queue = pxa_videobuf_queue, | |
817 | .buf_release = pxa_videobuf_release, | |
818 | }; | |
819 | ||
a034d1b7 | 820 | static void pxa_camera_init_videobuf(struct videobuf_queue *q, |
092d3921 PZ |
821 | struct soc_camera_device *icd) |
822 | { | |
a034d1b7 MD |
823 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
824 | struct pxa_camera_dev *pcdev = ici->priv; | |
825 | ||
092d3921 PZ |
826 | /* We must pass NULL as dev pointer, then all pci_* dma operations |
827 | * transform to normal dma_* ones. */ | |
a034d1b7 | 828 | videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock, |
092d3921 PZ |
829 | V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE, |
830 | sizeof(struct pxa_buffer), icd); | |
831 | } | |
832 | ||
40e2e092 GL |
833 | static u32 mclk_get_divisor(struct platform_device *pdev, |
834 | struct pxa_camera_dev *pcdev) | |
3bc43840 | 835 | { |
cf34cba7 GL |
836 | unsigned long mclk = pcdev->mclk; |
837 | u32 div; | |
3bc43840 GL |
838 | unsigned long lcdclk; |
839 | ||
cf34cba7 GL |
840 | lcdclk = clk_get_rate(pcdev->clk); |
841 | pcdev->ciclk = lcdclk; | |
3bc43840 | 842 | |
cf34cba7 GL |
843 | /* mclk <= ciclk / 4 (27.4.2) */ |
844 | if (mclk > lcdclk / 4) { | |
845 | mclk = lcdclk / 4; | |
40e2e092 | 846 | dev_warn(&pdev->dev, "Limiting master clock to %lu\n", mclk); |
cf34cba7 GL |
847 | } |
848 | ||
849 | /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */ | |
850 | div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1; | |
3bc43840 | 851 | |
cf34cba7 GL |
852 | /* If we're not supplying MCLK, leave it at 0 */ |
853 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
854 | pcdev->mclk = lcdclk / (2 * (div + 1)); | |
3bc43840 | 855 | |
40e2e092 GL |
856 | dev_dbg(&pdev->dev, "LCD clock %luHz, target freq %luHz, divisor %u\n", |
857 | lcdclk, mclk, div); | |
3bc43840 GL |
858 | |
859 | return div; | |
860 | } | |
861 | ||
cf34cba7 GL |
862 | static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev, |
863 | unsigned long pclk) | |
864 | { | |
865 | /* We want a timeout > 1 pixel time, not ">=" */ | |
866 | u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1; | |
867 | ||
868 | __raw_writel(ciclk_per_pixel, pcdev->base + CITOR); | |
869 | } | |
870 | ||
7102b773 | 871 | static void pxa_camera_activate(struct pxa_camera_dev *pcdev) |
3bc43840 GL |
872 | { |
873 | struct pxacamera_platform_data *pdata = pcdev->pdata; | |
874 | u32 cicr4 = 0; | |
875 | ||
eff505fa | 876 | dev_dbg(pcdev->soc_host.dev, "Registered platform device at %p data %p\n", |
3bc43840 GL |
877 | pcdev, pdata); |
878 | ||
879 | if (pdata && pdata->init) { | |
eff505fa GL |
880 | dev_dbg(pcdev->soc_host.dev, "%s: Init gpios\n", __func__); |
881 | pdata->init(pcdev->soc_host.dev); | |
3bc43840 GL |
882 | } |
883 | ||
5ca11fa3 EM |
884 | /* disable all interrupts */ |
885 | __raw_writel(0x3ff, pcdev->base + CICR0); | |
3bc43840 GL |
886 | |
887 | if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN) | |
888 | cicr4 |= CICR4_PCLK_EN; | |
889 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
890 | cicr4 |= CICR4_MCLK_EN; | |
891 | if (pcdev->platform_flags & PXA_CAMERA_PCP) | |
892 | cicr4 |= CICR4_PCP; | |
893 | if (pcdev->platform_flags & PXA_CAMERA_HSP) | |
894 | cicr4 |= CICR4_HSP; | |
895 | if (pcdev->platform_flags & PXA_CAMERA_VSP) | |
896 | cicr4 |= CICR4_VSP; | |
897 | ||
cf34cba7 GL |
898 | __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4); |
899 | ||
900 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
901 | /* Initialise the timeout under the assumption pclk = mclk */ | |
902 | recalculate_fifo_timeout(pcdev, pcdev->mclk); | |
903 | else | |
904 | /* "Safe default" - 13MHz */ | |
905 | recalculate_fifo_timeout(pcdev, 13000000); | |
3bc43840 GL |
906 | |
907 | clk_enable(pcdev->clk); | |
908 | } | |
909 | ||
7102b773 | 910 | static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev) |
3bc43840 | 911 | { |
3bc43840 | 912 | clk_disable(pcdev->clk); |
3bc43840 GL |
913 | } |
914 | ||
915 | static irqreturn_t pxa_camera_irq(int irq, void *data) | |
916 | { | |
917 | struct pxa_camera_dev *pcdev = data; | |
5ca11fa3 | 918 | unsigned long status, cicr0; |
256b0233 RJ |
919 | struct pxa_buffer *buf; |
920 | struct videobuf_buffer *vb; | |
3bc43840 | 921 | |
5ca11fa3 | 922 | status = __raw_readl(pcdev->base + CISR); |
eff505fa | 923 | dev_dbg(pcdev->soc_host.dev, "Camera interrupt status 0x%lx\n", status); |
3bc43840 | 924 | |
e7c50688 GL |
925 | if (!status) |
926 | return IRQ_NONE; | |
927 | ||
5ca11fa3 | 928 | __raw_writel(status, pcdev->base + CISR); |
e7c50688 GL |
929 | |
930 | if (status & CISR_EOF) { | |
256b0233 RJ |
931 | pcdev->active = list_first_entry(&pcdev->capture, |
932 | struct pxa_buffer, vb.queue); | |
933 | vb = &pcdev->active->vb; | |
934 | buf = container_of(vb, struct pxa_buffer, vb); | |
935 | pxa_videobuf_set_actdma(pcdev, buf); | |
936 | ||
937 | pxa_dma_start_channels(pcdev); | |
938 | ||
5ca11fa3 EM |
939 | cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM; |
940 | __raw_writel(cicr0, pcdev->base + CICR0); | |
e7c50688 GL |
941 | } |
942 | ||
3bc43840 GL |
943 | return IRQ_HANDLED; |
944 | } | |
945 | ||
1c3bb743 GL |
946 | /* |
947 | * The following two functions absolutely depend on the fact, that | |
948 | * there can be only one camera on PXA quick capture interface | |
949 | * Called with .video_lock held | |
950 | */ | |
7102b773 | 951 | static int pxa_camera_add_device(struct soc_camera_device *icd) |
3bc43840 GL |
952 | { |
953 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
954 | struct pxa_camera_dev *pcdev = ici->priv; | |
955 | int ret; | |
956 | ||
3bc43840 GL |
957 | if (pcdev->icd) { |
958 | ret = -EBUSY; | |
959 | goto ebusy; | |
960 | } | |
961 | ||
7102b773 | 962 | pxa_camera_activate(pcdev); |
3bc43840 | 963 | ret = icd->ops->init(icd); |
40e2e092 GL |
964 | if (ret < 0) |
965 | goto einit; | |
966 | ||
967 | pcdev->icd = icd; | |
3bc43840 | 968 | |
40e2e092 GL |
969 | dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n", |
970 | icd->devnum); | |
3bc43840 | 971 | |
40e2e092 GL |
972 | return 0; |
973 | ||
974 | einit: | |
975 | pxa_camera_deactivate(pcdev); | |
3bc43840 | 976 | ebusy: |
3bc43840 GL |
977 | return ret; |
978 | } | |
979 | ||
1c3bb743 | 980 | /* Called with .video_lock held */ |
7102b773 | 981 | static void pxa_camera_remove_device(struct soc_camera_device *icd) |
3bc43840 GL |
982 | { |
983 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
984 | struct pxa_camera_dev *pcdev = ici->priv; | |
985 | ||
986 | BUG_ON(icd != pcdev->icd); | |
987 | ||
988 | dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n", | |
989 | icd->devnum); | |
990 | ||
991 | /* disable capture, disable interrupts */ | |
5ca11fa3 | 992 | __raw_writel(0x3ff, pcdev->base + CICR0); |
a5462e5b | 993 | |
3bc43840 | 994 | /* Stop DMA engine */ |
a5462e5b MR |
995 | DCSR(pcdev->dma_chans[0]) = 0; |
996 | DCSR(pcdev->dma_chans[1]) = 0; | |
997 | DCSR(pcdev->dma_chans[2]) = 0; | |
3bc43840 GL |
998 | |
999 | icd->ops->release(icd); | |
1000 | ||
7102b773 | 1001 | pxa_camera_deactivate(pcdev); |
3bc43840 GL |
1002 | |
1003 | pcdev->icd = NULL; | |
1004 | } | |
1005 | ||
ad5f2e85 GL |
1006 | static int test_platform_param(struct pxa_camera_dev *pcdev, |
1007 | unsigned char buswidth, unsigned long *flags) | |
3bc43840 | 1008 | { |
ad5f2e85 GL |
1009 | /* |
1010 | * Platform specified synchronization and pixel clock polarities are | |
1011 | * only a recommendation and are only used during probing. The PXA270 | |
1012 | * quick capture interface supports both. | |
1013 | */ | |
1014 | *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ? | |
1015 | SOCAM_MASTER : SOCAM_SLAVE) | | |
1016 | SOCAM_HSYNC_ACTIVE_HIGH | | |
1017 | SOCAM_HSYNC_ACTIVE_LOW | | |
1018 | SOCAM_VSYNC_ACTIVE_HIGH | | |
1019 | SOCAM_VSYNC_ACTIVE_LOW | | |
2d9329f3 | 1020 | SOCAM_DATA_ACTIVE_HIGH | |
ad5f2e85 GL |
1021 | SOCAM_PCLK_SAMPLE_RISING | |
1022 | SOCAM_PCLK_SAMPLE_FALLING; | |
3bc43840 GL |
1023 | |
1024 | /* If requested data width is supported by the platform, use it */ | |
ad5f2e85 | 1025 | switch (buswidth) { |
3bc43840 | 1026 | case 10: |
ad5f2e85 GL |
1027 | if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10)) |
1028 | return -EINVAL; | |
1029 | *flags |= SOCAM_DATAWIDTH_10; | |
3bc43840 GL |
1030 | break; |
1031 | case 9: | |
ad5f2e85 GL |
1032 | if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9)) |
1033 | return -EINVAL; | |
1034 | *flags |= SOCAM_DATAWIDTH_9; | |
3bc43840 GL |
1035 | break; |
1036 | case 8: | |
ad5f2e85 GL |
1037 | if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8)) |
1038 | return -EINVAL; | |
1039 | *flags |= SOCAM_DATAWIDTH_8; | |
2a48fc73 RJ |
1040 | break; |
1041 | default: | |
1042 | return -EINVAL; | |
3bc43840 | 1043 | } |
ad5f2e85 GL |
1044 | |
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt) | |
1049 | { | |
64f5905e | 1050 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
ad5f2e85 GL |
1051 | struct pxa_camera_dev *pcdev = ici->priv; |
1052 | unsigned long dw, bpp, bus_flags, camera_flags, common_flags; | |
5ca11fa3 | 1053 | u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0; |
ad5f2e85 GL |
1054 | int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags); |
1055 | ||
1056 | if (ret < 0) | |
1057 | return ret; | |
1058 | ||
1059 | camera_flags = icd->ops->query_bus_param(icd); | |
1060 | ||
1061 | common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags); | |
1062 | if (!common_flags) | |
3bc43840 GL |
1063 | return -EINVAL; |
1064 | ||
e7c50688 GL |
1065 | pcdev->channels = 1; |
1066 | ||
ad5f2e85 GL |
1067 | /* Make choises, based on platform preferences */ |
1068 | if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) && | |
1069 | (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) { | |
1070 | if (pcdev->platform_flags & PXA_CAMERA_HSP) | |
1071 | common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH; | |
1072 | else | |
1073 | common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW; | |
1074 | } | |
1075 | ||
1076 | if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) && | |
1077 | (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) { | |
1078 | if (pcdev->platform_flags & PXA_CAMERA_VSP) | |
1079 | common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH; | |
1080 | else | |
1081 | common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW; | |
1082 | } | |
1083 | ||
1084 | if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) && | |
1085 | (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) { | |
1086 | if (pcdev->platform_flags & PXA_CAMERA_PCP) | |
1087 | common_flags &= ~SOCAM_PCLK_SAMPLE_RISING; | |
1088 | else | |
1089 | common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING; | |
1090 | } | |
1091 | ||
1092 | ret = icd->ops->set_bus_param(icd, common_flags); | |
3bc43840 GL |
1093 | if (ret < 0) |
1094 | return ret; | |
1095 | ||
1096 | /* Datawidth is now guaranteed to be equal to one of the three values. | |
1097 | * We fix bit-per-pixel equal to data-width... */ | |
ad5f2e85 GL |
1098 | switch (common_flags & SOCAM_DATAWIDTH_MASK) { |
1099 | case SOCAM_DATAWIDTH_10: | |
3bc43840 GL |
1100 | dw = 4; |
1101 | bpp = 0x40; | |
1102 | break; | |
ad5f2e85 | 1103 | case SOCAM_DATAWIDTH_9: |
3bc43840 GL |
1104 | dw = 3; |
1105 | bpp = 0x20; | |
1106 | break; | |
1107 | default: | |
1108 | /* Actually it can only be 8 now, | |
1109 | * default is just to silence compiler warnings */ | |
ad5f2e85 | 1110 | case SOCAM_DATAWIDTH_8: |
3bc43840 GL |
1111 | dw = 2; |
1112 | bpp = 0; | |
1113 | } | |
1114 | ||
1115 | if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN) | |
1116 | cicr4 |= CICR4_PCLK_EN; | |
1117 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
1118 | cicr4 |= CICR4_MCLK_EN; | |
ad5f2e85 | 1119 | if (common_flags & SOCAM_PCLK_SAMPLE_FALLING) |
3bc43840 | 1120 | cicr4 |= CICR4_PCP; |
ad5f2e85 | 1121 | if (common_flags & SOCAM_HSYNC_ACTIVE_LOW) |
3bc43840 | 1122 | cicr4 |= CICR4_HSP; |
ad5f2e85 | 1123 | if (common_flags & SOCAM_VSYNC_ACTIVE_LOW) |
3bc43840 GL |
1124 | cicr4 |= CICR4_VSP; |
1125 | ||
5ca11fa3 | 1126 | cicr0 = __raw_readl(pcdev->base + CICR0); |
3bc43840 | 1127 | if (cicr0 & CICR0_ENB) |
5ca11fa3 | 1128 | __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0); |
a5462e5b MR |
1129 | |
1130 | cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw; | |
1131 | ||
1132 | switch (pixfmt) { | |
1133 | case V4L2_PIX_FMT_YUV422P: | |
e7c50688 | 1134 | pcdev->channels = 3; |
a5462e5b | 1135 | cicr1 |= CICR1_YCBCR_F; |
2a48fc73 RJ |
1136 | /* |
1137 | * Normally, pxa bus wants as input UYVY format. We allow all | |
1138 | * reorderings of the YUV422 format, as no processing is done, | |
1139 | * and the YUV stream is just passed through without any | |
1140 | * transformation. Note that UYVY is the only format that | |
1141 | * should be used if pxa framebuffer Overlay2 is used. | |
1142 | */ | |
1143 | case V4L2_PIX_FMT_UYVY: | |
1144 | case V4L2_PIX_FMT_VYUY: | |
a5462e5b | 1145 | case V4L2_PIX_FMT_YUYV: |
2a48fc73 | 1146 | case V4L2_PIX_FMT_YVYU: |
a5462e5b MR |
1147 | cicr1 |= CICR1_COLOR_SP_VAL(2); |
1148 | break; | |
1149 | case V4L2_PIX_FMT_RGB555: | |
1150 | cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) | | |
1151 | CICR1_TBIT | CICR1_COLOR_SP_VAL(1); | |
1152 | break; | |
1153 | case V4L2_PIX_FMT_RGB565: | |
1154 | cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2); | |
1155 | break; | |
1156 | } | |
1157 | ||
5ca11fa3 EM |
1158 | cicr2 = 0; |
1159 | cicr3 = CICR3_LPF_VAL(icd->height - 1) | | |
3bc43840 | 1160 | CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top)); |
cf34cba7 | 1161 | cicr4 |= pcdev->mclk_divisor; |
5ca11fa3 EM |
1162 | |
1163 | __raw_writel(cicr1, pcdev->base + CICR1); | |
1164 | __raw_writel(cicr2, pcdev->base + CICR2); | |
1165 | __raw_writel(cicr3, pcdev->base + CICR3); | |
1166 | __raw_writel(cicr4, pcdev->base + CICR4); | |
3bc43840 GL |
1167 | |
1168 | /* CIF interrupts are not used, only DMA */ | |
5ca11fa3 EM |
1169 | cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ? |
1170 | CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)); | |
1171 | cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK; | |
1172 | __raw_writel(cicr0, pcdev->base + CICR0); | |
3bc43840 GL |
1173 | |
1174 | return 0; | |
1175 | } | |
1176 | ||
2a48fc73 RJ |
1177 | static int pxa_camera_try_bus_param(struct soc_camera_device *icd, |
1178 | unsigned char buswidth) | |
ad5f2e85 | 1179 | { |
cf34cba7 | 1180 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
ad5f2e85 GL |
1181 | struct pxa_camera_dev *pcdev = ici->priv; |
1182 | unsigned long bus_flags, camera_flags; | |
2a48fc73 | 1183 | int ret = test_platform_param(pcdev, buswidth, &bus_flags); |
ad5f2e85 GL |
1184 | |
1185 | if (ret < 0) | |
1186 | return ret; | |
1187 | ||
1188 | camera_flags = icd->ops->query_bus_param(icd); | |
1189 | ||
1190 | return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL; | |
1191 | } | |
1192 | ||
2a48fc73 RJ |
1193 | static const struct soc_camera_data_format pxa_camera_formats[] = { |
1194 | { | |
1195 | .name = "Planar YUV422 16 bit", | |
1196 | .depth = 16, | |
1197 | .fourcc = V4L2_PIX_FMT_YUV422P, | |
1198 | .colorspace = V4L2_COLORSPACE_JPEG, | |
1199 | }, | |
1200 | }; | |
1201 | ||
1202 | static bool buswidth_supported(struct soc_camera_device *icd, int depth) | |
ad5f2e85 | 1203 | { |
2a48fc73 RJ |
1204 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
1205 | struct pxa_camera_dev *pcdev = ici->priv; | |
1206 | ||
1207 | switch (depth) { | |
1208 | case 8: | |
1209 | return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8); | |
1210 | case 9: | |
1211 | return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9); | |
1212 | case 10: | |
1213 | return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10); | |
1214 | } | |
1215 | return false; | |
ad5f2e85 GL |
1216 | } |
1217 | ||
2a48fc73 | 1218 | static int required_buswidth(const struct soc_camera_data_format *fmt) |
3bc43840 | 1219 | { |
2a48fc73 RJ |
1220 | switch (fmt->fourcc) { |
1221 | case V4L2_PIX_FMT_UYVY: | |
1222 | case V4L2_PIX_FMT_VYUY: | |
1223 | case V4L2_PIX_FMT_YUYV: | |
1224 | case V4L2_PIX_FMT_YVYU: | |
1225 | case V4L2_PIX_FMT_RGB565: | |
1226 | case V4L2_PIX_FMT_RGB555: | |
1227 | return 8; | |
1228 | default: | |
1229 | return fmt->depth; | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx, | |
1234 | struct soc_camera_format_xlate *xlate) | |
1235 | { | |
1236 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
1237 | int formats = 0, buswidth, ret; | |
1238 | ||
1239 | buswidth = required_buswidth(icd->formats + idx); | |
1240 | ||
1241 | if (!buswidth_supported(icd, buswidth)) | |
1242 | return 0; | |
3bc43840 | 1243 | |
2a48fc73 RJ |
1244 | ret = pxa_camera_try_bus_param(icd, buswidth); |
1245 | if (ret < 0) | |
1246 | return 0; | |
1247 | ||
1248 | switch (icd->formats[idx].fourcc) { | |
1249 | case V4L2_PIX_FMT_UYVY: | |
1250 | formats++; | |
1251 | if (xlate) { | |
1252 | xlate->host_fmt = &pxa_camera_formats[0]; | |
1253 | xlate->cam_fmt = icd->formats + idx; | |
1254 | xlate->buswidth = buswidth; | |
1255 | xlate++; | |
eff505fa | 1256 | dev_dbg(ici->dev, "Providing format %s using %s\n", |
2a48fc73 RJ |
1257 | pxa_camera_formats[0].name, |
1258 | icd->formats[idx].name); | |
1259 | } | |
1260 | case V4L2_PIX_FMT_VYUY: | |
1261 | case V4L2_PIX_FMT_YUYV: | |
1262 | case V4L2_PIX_FMT_YVYU: | |
1263 | case V4L2_PIX_FMT_RGB565: | |
1264 | case V4L2_PIX_FMT_RGB555: | |
1265 | formats++; | |
1266 | if (xlate) { | |
1267 | xlate->host_fmt = icd->formats + idx; | |
1268 | xlate->cam_fmt = icd->formats + idx; | |
1269 | xlate->buswidth = buswidth; | |
1270 | xlate++; | |
eff505fa | 1271 | dev_dbg(ici->dev, "Providing format %s packed\n", |
2a48fc73 RJ |
1272 | icd->formats[idx].name); |
1273 | } | |
1274 | break; | |
1275 | default: | |
1276 | /* Generic pass-through */ | |
1277 | formats++; | |
1278 | if (xlate) { | |
1279 | xlate->host_fmt = icd->formats + idx; | |
1280 | xlate->cam_fmt = icd->formats + idx; | |
1281 | xlate->buswidth = icd->formats[idx].depth; | |
1282 | xlate++; | |
eff505fa | 1283 | dev_dbg(ici->dev, |
2a48fc73 RJ |
1284 | "Providing format %s in pass-through mode\n", |
1285 | icd->formats[idx].name); | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | return formats; | |
1290 | } | |
1291 | ||
09e231b3 GL |
1292 | static int pxa_camera_set_crop(struct soc_camera_device *icd, |
1293 | struct v4l2_rect *rect) | |
1294 | { | |
1295 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
1296 | struct pxa_camera_dev *pcdev = ici->priv; | |
1297 | struct soc_camera_sense sense = { | |
1298 | .master_clock = pcdev->mclk, | |
1299 | .pixel_clock_max = pcdev->ciclk / 4, | |
1300 | }; | |
1301 | int ret; | |
1302 | ||
1303 | /* If PCLK is used to latch data from the sensor, check sense */ | |
1304 | if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN) | |
1305 | icd->sense = &sense; | |
1306 | ||
1307 | ret = icd->ops->set_crop(icd, rect); | |
1308 | ||
1309 | icd->sense = NULL; | |
1310 | ||
1311 | if (ret < 0) { | |
eff505fa | 1312 | dev_warn(ici->dev, "Failed to crop to %ux%u@%u:%u\n", |
09e231b3 GL |
1313 | rect->width, rect->height, rect->left, rect->top); |
1314 | } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { | |
1315 | if (sense.pixel_clock > sense.pixel_clock_max) { | |
eff505fa | 1316 | dev_err(ici->dev, |
09e231b3 GL |
1317 | "pixel clock %lu set by the camera too high!", |
1318 | sense.pixel_clock); | |
1319 | return -EIO; | |
1320 | } | |
1321 | recalculate_fifo_timeout(pcdev, sense.pixel_clock); | |
1322 | } | |
1323 | ||
1324 | return ret; | |
1325 | } | |
1326 | ||
d8fac217 | 1327 | static int pxa_camera_set_fmt(struct soc_camera_device *icd, |
09e231b3 | 1328 | struct v4l2_format *f) |
ad5f2e85 | 1329 | { |
2a48fc73 | 1330 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
cf34cba7 | 1331 | struct pxa_camera_dev *pcdev = ici->priv; |
0ad675eb GL |
1332 | const struct soc_camera_data_format *cam_fmt = NULL; |
1333 | const struct soc_camera_format_xlate *xlate = NULL; | |
cf34cba7 GL |
1334 | struct soc_camera_sense sense = { |
1335 | .master_clock = pcdev->mclk, | |
1336 | .pixel_clock_max = pcdev->ciclk / 4, | |
1337 | }; | |
09e231b3 GL |
1338 | struct v4l2_pix_format *pix = &f->fmt.pix; |
1339 | struct v4l2_format cam_f = *f; | |
0ad675eb | 1340 | int ret; |
25c4d74e | 1341 | |
09e231b3 GL |
1342 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
1343 | if (!xlate) { | |
eff505fa | 1344 | dev_warn(ici->dev, "Format %x not found\n", pix->pixelformat); |
09e231b3 | 1345 | return -EINVAL; |
0ad675eb | 1346 | } |
2a48fc73 | 1347 | |
09e231b3 GL |
1348 | cam_fmt = xlate->cam_fmt; |
1349 | ||
cf34cba7 GL |
1350 | /* If PCLK is used to latch data from the sensor, check sense */ |
1351 | if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN) | |
1352 | icd->sense = &sense; | |
1353 | ||
09e231b3 GL |
1354 | cam_f.fmt.pix.pixelformat = cam_fmt->fourcc; |
1355 | ret = icd->ops->set_fmt(icd, &cam_f); | |
2a48fc73 | 1356 | |
cf34cba7 GL |
1357 | icd->sense = NULL; |
1358 | ||
1359 | if (ret < 0) { | |
eff505fa | 1360 | dev_warn(ici->dev, "Failed to configure for format %x\n", |
09e231b3 | 1361 | pix->pixelformat); |
cf34cba7 GL |
1362 | } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { |
1363 | if (sense.pixel_clock > sense.pixel_clock_max) { | |
eff505fa | 1364 | dev_err(ici->dev, |
cf34cba7 GL |
1365 | "pixel clock %lu set by the camera too high!", |
1366 | sense.pixel_clock); | |
1367 | return -EIO; | |
1368 | } | |
1369 | recalculate_fifo_timeout(pcdev, sense.pixel_clock); | |
1370 | } | |
2a48fc73 | 1371 | |
09e231b3 | 1372 | if (!ret) { |
0ad675eb GL |
1373 | icd->buswidth = xlate->buswidth; |
1374 | icd->current_fmt = xlate->host_fmt; | |
2a48fc73 | 1375 | } |
25c4d74e GL |
1376 | |
1377 | return ret; | |
ad5f2e85 GL |
1378 | } |
1379 | ||
d8fac217 GL |
1380 | static int pxa_camera_try_fmt(struct soc_camera_device *icd, |
1381 | struct v4l2_format *f) | |
3bc43840 | 1382 | { |
2a48fc73 RJ |
1383 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
1384 | const struct soc_camera_format_xlate *xlate; | |
1385 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
1386 | __u32 pixfmt = pix->pixelformat; | |
06daa1af | 1387 | enum v4l2_field field; |
bf507158 | 1388 | int ret; |
a2c8c68c | 1389 | |
2a48fc73 RJ |
1390 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
1391 | if (!xlate) { | |
eff505fa | 1392 | dev_warn(ici->dev, "Format %x not found\n", pixfmt); |
25c4d74e | 1393 | return -EINVAL; |
2a48fc73 | 1394 | } |
25c4d74e | 1395 | |
92a8337b | 1396 | /* |
4a6b8df2 TP |
1397 | * Limit to pxa hardware capabilities. YUV422P planar format requires |
1398 | * images size to be a multiple of 16 bytes. If not, zeros will be | |
1399 | * inserted between Y and U planes, and U and V planes, which violates | |
1400 | * the YUV422P standard. | |
92a8337b | 1401 | */ |
4a6b8df2 TP |
1402 | v4l_bound_align_image(&pix->width, 48, 2048, 1, |
1403 | &pix->height, 32, 2048, 0, | |
1404 | xlate->host_fmt->fourcc == V4L2_PIX_FMT_YUV422P ? 4 : 0); | |
92a8337b | 1405 | |
2a48fc73 RJ |
1406 | pix->bytesperline = pix->width * |
1407 | DIV_ROUND_UP(xlate->host_fmt->depth, 8); | |
1408 | pix->sizeimage = pix->height * pix->bytesperline; | |
25c4d74e | 1409 | |
bf507158 GL |
1410 | /* camera has to see its format, but the user the original one */ |
1411 | pix->pixelformat = xlate->cam_fmt->fourcc; | |
ad5f2e85 | 1412 | /* limit to sensor capabilities */ |
bf507158 GL |
1413 | ret = icd->ops->try_fmt(icd, f); |
1414 | pix->pixelformat = xlate->host_fmt->fourcc; | |
1415 | ||
06daa1af GL |
1416 | field = pix->field; |
1417 | ||
1418 | if (field == V4L2_FIELD_ANY) { | |
1419 | pix->field = V4L2_FIELD_NONE; | |
1420 | } else if (field != V4L2_FIELD_NONE) { | |
1421 | dev_err(&icd->dev, "Field type %d unsupported.\n", field); | |
1422 | return -EINVAL; | |
1423 | } | |
1424 | ||
bf507158 | 1425 | return ret; |
3bc43840 GL |
1426 | } |
1427 | ||
7102b773 GL |
1428 | static int pxa_camera_reqbufs(struct soc_camera_file *icf, |
1429 | struct v4l2_requestbuffers *p) | |
3bc43840 GL |
1430 | { |
1431 | int i; | |
1432 | ||
1433 | /* This is for locking debugging only. I removed spinlocks and now I | |
1434 | * check whether .prepare is ever called on a linked buffer, or whether | |
1435 | * a dma IRQ can occur for an in-work or unlinked buffer. Until now | |
1436 | * it hadn't triggered */ | |
1437 | for (i = 0; i < p->count; i++) { | |
1438 | struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i], | |
1439 | struct pxa_buffer, vb); | |
1440 | buf->inwork = 0; | |
1441 | INIT_LIST_HEAD(&buf->vb.queue); | |
1442 | } | |
1443 | ||
1444 | return 0; | |
1445 | } | |
1446 | ||
7102b773 | 1447 | static unsigned int pxa_camera_poll(struct file *file, poll_table *pt) |
3bc43840 GL |
1448 | { |
1449 | struct soc_camera_file *icf = file->private_data; | |
1450 | struct pxa_buffer *buf; | |
1451 | ||
1452 | buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer, | |
1453 | vb.stream); | |
1454 | ||
1455 | poll_wait(file, &buf->vb.done, pt); | |
1456 | ||
1457 | if (buf->vb.state == VIDEOBUF_DONE || | |
1458 | buf->vb.state == VIDEOBUF_ERROR) | |
1459 | return POLLIN|POLLRDNORM; | |
1460 | ||
1461 | return 0; | |
1462 | } | |
1463 | ||
7102b773 GL |
1464 | static int pxa_camera_querycap(struct soc_camera_host *ici, |
1465 | struct v4l2_capability *cap) | |
3bc43840 GL |
1466 | { |
1467 | /* cap->name is set by the firendly caller:-> */ | |
1468 | strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card)); | |
1469 | cap->version = PXA_CAM_VERSION_CODE; | |
1470 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; | |
1471 | ||
1472 | return 0; | |
1473 | } | |
1474 | ||
3f6ac497 RJ |
1475 | static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state) |
1476 | { | |
64f5905e | 1477 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3f6ac497 RJ |
1478 | struct pxa_camera_dev *pcdev = ici->priv; |
1479 | int i = 0, ret = 0; | |
1480 | ||
5ca11fa3 EM |
1481 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0); |
1482 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1); | |
1483 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2); | |
1484 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3); | |
1485 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4); | |
3f6ac497 RJ |
1486 | |
1487 | if ((pcdev->icd) && (pcdev->icd->ops->suspend)) | |
1488 | ret = pcdev->icd->ops->suspend(pcdev->icd, state); | |
1489 | ||
1490 | return ret; | |
1491 | } | |
1492 | ||
1493 | static int pxa_camera_resume(struct soc_camera_device *icd) | |
1494 | { | |
64f5905e | 1495 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3f6ac497 RJ |
1496 | struct pxa_camera_dev *pcdev = ici->priv; |
1497 | int i = 0, ret = 0; | |
1498 | ||
87f3dd77 EM |
1499 | DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD; |
1500 | DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD; | |
1501 | DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD; | |
3f6ac497 | 1502 | |
5ca11fa3 EM |
1503 | __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0); |
1504 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1); | |
1505 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2); | |
1506 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3); | |
1507 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4); | |
3f6ac497 RJ |
1508 | |
1509 | if ((pcdev->icd) && (pcdev->icd->ops->resume)) | |
1510 | ret = pcdev->icd->ops->resume(pcdev->icd); | |
1511 | ||
1512 | /* Restart frame capture if active buffer exists */ | |
256b0233 RJ |
1513 | if (!ret && pcdev->active) |
1514 | pxa_camera_start_capture(pcdev); | |
3f6ac497 RJ |
1515 | |
1516 | return ret; | |
1517 | } | |
1518 | ||
b8d9904c GL |
1519 | static struct soc_camera_host_ops pxa_soc_camera_host_ops = { |
1520 | .owner = THIS_MODULE, | |
1521 | .add = pxa_camera_add_device, | |
1522 | .remove = pxa_camera_remove_device, | |
3f6ac497 RJ |
1523 | .suspend = pxa_camera_suspend, |
1524 | .resume = pxa_camera_resume, | |
09e231b3 | 1525 | .set_crop = pxa_camera_set_crop, |
2a48fc73 | 1526 | .get_formats = pxa_camera_get_formats, |
d8fac217 GL |
1527 | .set_fmt = pxa_camera_set_fmt, |
1528 | .try_fmt = pxa_camera_try_fmt, | |
092d3921 | 1529 | .init_videobuf = pxa_camera_init_videobuf, |
b8d9904c GL |
1530 | .reqbufs = pxa_camera_reqbufs, |
1531 | .poll = pxa_camera_poll, | |
1532 | .querycap = pxa_camera_querycap, | |
b8d9904c GL |
1533 | .set_bus_param = pxa_camera_set_bus_param, |
1534 | }; | |
1535 | ||
e36bc31f | 1536 | static int __devinit pxa_camera_probe(struct platform_device *pdev) |
3bc43840 GL |
1537 | { |
1538 | struct pxa_camera_dev *pcdev; | |
1539 | struct resource *res; | |
1540 | void __iomem *base; | |
02da4659 | 1541 | int irq; |
3bc43840 GL |
1542 | int err = 0; |
1543 | ||
1544 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1545 | irq = platform_get_irq(pdev, 0); | |
02da4659 | 1546 | if (!res || irq < 0) { |
3bc43840 GL |
1547 | err = -ENODEV; |
1548 | goto exit; | |
1549 | } | |
1550 | ||
1551 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); | |
1552 | if (!pcdev) { | |
7102b773 | 1553 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
3bc43840 GL |
1554 | err = -ENOMEM; |
1555 | goto exit; | |
1556 | } | |
1557 | ||
e0d8b13a | 1558 | pcdev->clk = clk_get(&pdev->dev, NULL); |
3bc43840 GL |
1559 | if (IS_ERR(pcdev->clk)) { |
1560 | err = PTR_ERR(pcdev->clk); | |
1561 | goto exit_kfree; | |
1562 | } | |
1563 | ||
3bc43840 GL |
1564 | pcdev->res = res; |
1565 | ||
1566 | pcdev->pdata = pdev->dev.platform_data; | |
1567 | pcdev->platform_flags = pcdev->pdata->flags; | |
ad5f2e85 GL |
1568 | if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 | |
1569 | PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) { | |
3bc43840 GL |
1570 | /* Platform hasn't set available data widths. This is bad. |
1571 | * Warn and use a default. */ | |
1572 | dev_warn(&pdev->dev, "WARNING! Platform hasn't set available " | |
1573 | "data widths, using default 10 bit\n"); | |
1574 | pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10; | |
1575 | } | |
cf34cba7 GL |
1576 | pcdev->mclk = pcdev->pdata->mclk_10khz * 10000; |
1577 | if (!pcdev->mclk) { | |
3bc43840 | 1578 | dev_warn(&pdev->dev, |
cf34cba7 | 1579 | "mclk == 0! Please, fix your platform data. " |
3bc43840 | 1580 | "Using default 20MHz\n"); |
cf34cba7 | 1581 | pcdev->mclk = 20000000; |
3bc43840 GL |
1582 | } |
1583 | ||
40e2e092 | 1584 | pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev); |
cf34cba7 | 1585 | |
3bc43840 GL |
1586 | INIT_LIST_HEAD(&pcdev->capture); |
1587 | spin_lock_init(&pcdev->lock); | |
1588 | ||
1589 | /* | |
1590 | * Request the regions. | |
1591 | */ | |
eb6c8558 | 1592 | if (!request_mem_region(res->start, resource_size(res), |
3bc43840 GL |
1593 | PXA_CAM_DRV_NAME)) { |
1594 | err = -EBUSY; | |
1595 | goto exit_clk; | |
1596 | } | |
1597 | ||
eb6c8558 | 1598 | base = ioremap(res->start, resource_size(res)); |
3bc43840 GL |
1599 | if (!base) { |
1600 | err = -ENOMEM; | |
1601 | goto exit_release; | |
1602 | } | |
1603 | pcdev->irq = irq; | |
1604 | pcdev->base = base; | |
3bc43840 GL |
1605 | |
1606 | /* request dma */ | |
de3e3b82 | 1607 | err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH, |
1608 | pxa_camera_dma_irq_y, pcdev); | |
1609 | if (err < 0) { | |
eff505fa | 1610 | dev_err(&pdev->dev, "Can't request DMA for Y\n"); |
3bc43840 GL |
1611 | goto exit_iounmap; |
1612 | } | |
de3e3b82 | 1613 | pcdev->dma_chans[0] = err; |
eff505fa | 1614 | dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]); |
a5462e5b | 1615 | |
de3e3b82 | 1616 | err = pxa_request_dma("CI_U", DMA_PRIO_HIGH, |
1617 | pxa_camera_dma_irq_u, pcdev); | |
1618 | if (err < 0) { | |
eff505fa | 1619 | dev_err(&pdev->dev, "Can't request DMA for U\n"); |
a5462e5b MR |
1620 | goto exit_free_dma_y; |
1621 | } | |
de3e3b82 | 1622 | pcdev->dma_chans[1] = err; |
eff505fa | 1623 | dev_dbg(&pdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]); |
a5462e5b | 1624 | |
de3e3b82 | 1625 | err = pxa_request_dma("CI_V", DMA_PRIO_HIGH, |
1626 | pxa_camera_dma_irq_v, pcdev); | |
1627 | if (err < 0) { | |
eff505fa | 1628 | dev_err(&pdev->dev, "Can't request DMA for V\n"); |
a5462e5b MR |
1629 | goto exit_free_dma_u; |
1630 | } | |
de3e3b82 | 1631 | pcdev->dma_chans[2] = err; |
eff505fa | 1632 | dev_dbg(&pdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]); |
3bc43840 | 1633 | |
87f3dd77 EM |
1634 | DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD; |
1635 | DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD; | |
1636 | DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD; | |
3bc43840 GL |
1637 | |
1638 | /* request irq */ | |
1639 | err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME, | |
1640 | pcdev); | |
1641 | if (err) { | |
eff505fa | 1642 | dev_err(&pdev->dev, "Camera interrupt register failed \n"); |
3bc43840 GL |
1643 | goto exit_free_dma; |
1644 | } | |
1645 | ||
eb6c8558 GL |
1646 | pcdev->soc_host.drv_name = PXA_CAM_DRV_NAME; |
1647 | pcdev->soc_host.ops = &pxa_soc_camera_host_ops; | |
1648 | pcdev->soc_host.priv = pcdev; | |
40e2e092 | 1649 | pcdev->soc_host.dev = &pdev->dev; |
eb6c8558 | 1650 | pcdev->soc_host.nr = pdev->id; |
eff505fa | 1651 | |
eb6c8558 | 1652 | err = soc_camera_host_register(&pcdev->soc_host); |
3bc43840 GL |
1653 | if (err) |
1654 | goto exit_free_irq; | |
1655 | ||
1656 | return 0; | |
1657 | ||
1658 | exit_free_irq: | |
1659 | free_irq(pcdev->irq, pcdev); | |
1660 | exit_free_dma: | |
a5462e5b MR |
1661 | pxa_free_dma(pcdev->dma_chans[2]); |
1662 | exit_free_dma_u: | |
1663 | pxa_free_dma(pcdev->dma_chans[1]); | |
1664 | exit_free_dma_y: | |
1665 | pxa_free_dma(pcdev->dma_chans[0]); | |
3bc43840 GL |
1666 | exit_iounmap: |
1667 | iounmap(base); | |
1668 | exit_release: | |
eb6c8558 | 1669 | release_mem_region(res->start, resource_size(res)); |
3bc43840 GL |
1670 | exit_clk: |
1671 | clk_put(pcdev->clk); | |
1672 | exit_kfree: | |
1673 | kfree(pcdev); | |
1674 | exit: | |
1675 | return err; | |
1676 | } | |
1677 | ||
1678 | static int __devexit pxa_camera_remove(struct platform_device *pdev) | |
1679 | { | |
eff505fa GL |
1680 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
1681 | struct pxa_camera_dev *pcdev = container_of(soc_host, | |
1682 | struct pxa_camera_dev, soc_host); | |
3bc43840 GL |
1683 | struct resource *res; |
1684 | ||
1685 | clk_put(pcdev->clk); | |
1686 | ||
a5462e5b MR |
1687 | pxa_free_dma(pcdev->dma_chans[0]); |
1688 | pxa_free_dma(pcdev->dma_chans[1]); | |
1689 | pxa_free_dma(pcdev->dma_chans[2]); | |
3bc43840 GL |
1690 | free_irq(pcdev->irq, pcdev); |
1691 | ||
eff505fa | 1692 | soc_camera_host_unregister(soc_host); |
3bc43840 GL |
1693 | |
1694 | iounmap(pcdev->base); | |
1695 | ||
1696 | res = pcdev->res; | |
eb6c8558 | 1697 | release_mem_region(res->start, resource_size(res)); |
3bc43840 GL |
1698 | |
1699 | kfree(pcdev); | |
1700 | ||
7102b773 | 1701 | dev_info(&pdev->dev, "PXA Camera driver unloaded\n"); |
3bc43840 | 1702 | |
3bc43840 GL |
1703 | return 0; |
1704 | } | |
1705 | ||
3bc43840 GL |
1706 | static struct platform_driver pxa_camera_driver = { |
1707 | .driver = { | |
1708 | .name = PXA_CAM_DRV_NAME, | |
1709 | }, | |
1710 | .probe = pxa_camera_probe, | |
e36bc31f | 1711 | .remove = __devexit_p(pxa_camera_remove), |
3bc43840 GL |
1712 | }; |
1713 | ||
1714 | ||
e36bc31f | 1715 | static int __init pxa_camera_init(void) |
3bc43840 GL |
1716 | { |
1717 | return platform_driver_register(&pxa_camera_driver); | |
1718 | } | |
1719 | ||
1720 | static void __exit pxa_camera_exit(void) | |
1721 | { | |
01c1e4ca | 1722 | platform_driver_unregister(&pxa_camera_driver); |
3bc43840 GL |
1723 | } |
1724 | ||
1725 | module_init(pxa_camera_init); | |
1726 | module_exit(pxa_camera_exit); | |
1727 | ||
1728 | MODULE_DESCRIPTION("PXA27x SoC Camera Host driver"); | |
1729 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
1730 | MODULE_LICENSE("GPL"); | |
40e2e092 | 1731 | MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME); |