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 | ||
6a6c8786 GL |
228 | struct pxa_cam { |
229 | unsigned long flags; | |
230 | }; | |
231 | ||
3bc43840 GL |
232 | static const char *pxa_cam_driver_description = "PXA_Camera"; |
233 | ||
234 | static unsigned int vid_limit = 16; /* Video memory limit, in Mb */ | |
235 | ||
236 | /* | |
237 | * Videobuf operations | |
238 | */ | |
7102b773 GL |
239 | static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count, |
240 | unsigned int *size) | |
3bc43840 GL |
241 | { |
242 | struct soc_camera_device *icd = vq->priv_data; | |
243 | ||
0166b743 | 244 | dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size); |
3bc43840 | 245 | |
6a6c8786 | 246 | *size = roundup(icd->user_width * icd->user_height * |
92a8337b | 247 | ((icd->current_fmt->depth + 7) >> 3), 8); |
3bc43840 GL |
248 | |
249 | if (0 == *count) | |
250 | *count = 32; | |
251 | while (*size * *count > vid_limit * 1024 * 1024) | |
252 | (*count)--; | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf) | |
258 | { | |
259 | struct soc_camera_device *icd = vq->priv_data; | |
64f5905e | 260 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3bc43840 | 261 | struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); |
a5462e5b | 262 | int i; |
3bc43840 GL |
263 | |
264 | BUG_ON(in_interrupt()); | |
265 | ||
0166b743 | 266 | dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
3bc43840 GL |
267 | &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 | ||
a5462e5b MR |
275 | for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) { |
276 | if (buf->dmas[i].sg_cpu) | |
979ea1dd | 277 | dma_free_coherent(ici->v4l2_dev.dev, buf->dmas[i].sg_size, |
a5462e5b MR |
278 | buf->dmas[i].sg_cpu, |
279 | buf->dmas[i].sg_dma); | |
280 | buf->dmas[i].sg_cpu = NULL; | |
281 | } | |
3bc43840 GL |
282 | |
283 | buf->vb.state = VIDEOBUF_NEEDS_INIT; | |
284 | } | |
285 | ||
37f5aefd RJ |
286 | static 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 | */ | |
a5462e5b MR |
325 | static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev, |
326 | struct pxa_buffer *buf, | |
327 | struct videobuf_dmabuf *dma, int channel, | |
37f5aefd RJ |
328 | int cibr, int size, |
329 | struct scatterlist **sg_first, int *sg_first_ofs) | |
a5462e5b MR |
330 | { |
331 | struct pxa_cam_dma *pxa_dma = &buf->dmas[channel]; | |
979ea1dd | 332 | struct device *dev = pcdev->soc_host.v4l2_dev.dev; |
37f5aefd RJ |
333 | struct scatterlist *sg; |
334 | int i, offset, sglen; | |
335 | int dma_len = 0, xfer_len = 0; | |
a5462e5b MR |
336 | |
337 | if (pxa_dma->sg_cpu) | |
979ea1dd | 338 | dma_free_coherent(dev, pxa_dma->sg_size, |
a5462e5b MR |
339 | pxa_dma->sg_cpu, pxa_dma->sg_dma); |
340 | ||
37f5aefd RJ |
341 | sglen = calculate_dma_sglen(*sg_first, dma->sglen, |
342 | *sg_first_ofs, size); | |
343 | ||
a5462e5b | 344 | pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc); |
979ea1dd | 345 | pxa_dma->sg_cpu = dma_alloc_coherent(dev, pxa_dma->sg_size, |
a5462e5b MR |
346 | &pxa_dma->sg_dma, GFP_KERNEL); |
347 | if (!pxa_dma->sg_cpu) | |
348 | return -ENOMEM; | |
349 | ||
350 | pxa_dma->sglen = sglen; | |
37f5aefd | 351 | offset = *sg_first_ofs; |
a5462e5b | 352 | |
979ea1dd | 353 | dev_dbg(dev, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n", |
37f5aefd | 354 | *sg_first, sglen, *sg_first_ofs, pxa_dma->sg_dma); |
a5462e5b | 355 | |
37f5aefd RJ |
356 | |
357 | for_each_sg(*sg_first, sg, sglen, i) { | |
358 | dma_len = sg_dma_len(sg); | |
a5462e5b MR |
359 | |
360 | /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */ | |
37f5aefd | 361 | xfer_len = roundup(min(dma_len - offset, size), 8); |
a5462e5b | 362 | |
37f5aefd RJ |
363 | 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; | |
a5462e5b MR |
367 | pxa_dma->sg_cpu[i].dcmd = |
368 | DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len; | |
256b0233 RJ |
369 | #ifdef DEBUG |
370 | if (!i) | |
371 | pxa_dma->sg_cpu[i].dcmd |= DCMD_STARTIRQEN; | |
372 | #endif | |
a5462e5b MR |
373 | pxa_dma->sg_cpu[i].ddadr = |
374 | pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc); | |
37f5aefd | 375 | |
979ea1dd | 376 | dev_vdbg(dev, "DMA: desc.%08x->@phys=0x%08x, len=%d\n", |
37f5aefd RJ |
377 | 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; | |
a5462e5b MR |
383 | } |
384 | ||
256b0233 RJ |
385 | pxa_dma->sg_cpu[sglen].ddadr = DDADR_STOP; |
386 | pxa_dma->sg_cpu[sglen].dcmd = DCMD_FLOWSRC | DCMD_BURST8 | DCMD_ENDIRQEN; | |
a5462e5b | 387 | |
37f5aefd RJ |
388 | /* |
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 | ||
a5462e5b MR |
403 | return 0; |
404 | } | |
405 | ||
256b0233 RJ |
406 | static 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 | */ | |
7102b773 GL |
420 | static int pxa_videobuf_prepare(struct videobuf_queue *vq, |
421 | struct videobuf_buffer *vb, enum v4l2_field field) | |
3bc43840 GL |
422 | { |
423 | struct soc_camera_device *icd = vq->priv_data; | |
64f5905e | 424 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3bc43840 | 425 | struct pxa_camera_dev *pcdev = ici->priv; |
979ea1dd | 426 | struct device *dev = pcdev->soc_host.v4l2_dev.dev; |
3bc43840 | 427 | struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb); |
a5462e5b | 428 | int ret; |
a5462e5b | 429 | int size_y, size_u = 0, size_v = 0; |
3bc43840 | 430 | |
979ea1dd | 431 | dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
3bc43840 GL |
432 | 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 || | |
6a6c8786 GL |
450 | vb->width != icd->user_width || |
451 | vb->height != icd->user_height || | |
3bc43840 GL |
452 | vb->field != field) { |
453 | buf->fmt = icd->current_fmt; | |
6a6c8786 GL |
454 | vb->width = icd->user_width; |
455 | vb->height = icd->user_height; | |
3bc43840 GL |
456 | 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) { | |
37f5aefd RJ |
467 | int size = vb->size; |
468 | int next_ofs = 0; | |
3bc43840 | 469 | struct videobuf_dmabuf *dma = videobuf_to_dma(vb); |
37f5aefd | 470 | struct scatterlist *sg; |
3bc43840 GL |
471 | |
472 | ret = videobuf_iolock(vq, vb, NULL); | |
473 | if (ret) | |
474 | goto fail; | |
475 | ||
5aa2110f | 476 | if (pcdev->channels == 3) { |
a5462e5b MR |
477 | size_y = size / 2; |
478 | size_u = size_v = size / 4; | |
479 | } else { | |
a5462e5b MR |
480 | size_y = size; |
481 | } | |
482 | ||
37f5aefd | 483 | sg = dma->sglist; |
3bc43840 | 484 | |
37f5aefd RJ |
485 | /* init DMA for Y channel */ |
486 | ret = pxa_init_dma_channel(pcdev, buf, dma, 0, CIBR0, size_y, | |
487 | &sg, &next_ofs); | |
a5462e5b | 488 | if (ret) { |
979ea1dd | 489 | dev_err(dev, "DMA initialization for Y/RGB failed\n"); |
3bc43840 GL |
490 | goto fail; |
491 | } | |
492 | ||
37f5aefd RJ |
493 | /* 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) { | |
979ea1dd | 498 | dev_err(dev, "DMA initialization for U failed\n"); |
37f5aefd RJ |
499 | goto fail_u; |
500 | } | |
501 | ||
502 | /* 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) { | |
979ea1dd | 507 | dev_err(dev, "DMA initialization for V failed\n"); |
37f5aefd | 508 | goto fail_v; |
3bc43840 | 509 | } |
3bc43840 GL |
510 | |
511 | vb->state = VIDEOBUF_PREPARED; | |
512 | } | |
513 | ||
514 | buf->inwork = 0; | |
256b0233 | 515 | pxa_videobuf_set_actdma(pcdev, buf); |
3bc43840 GL |
516 | |
517 | return 0; | |
518 | ||
a5462e5b | 519 | fail_v: |
979ea1dd | 520 | dma_free_coherent(dev, buf->dmas[1].sg_size, |
a5462e5b MR |
521 | buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma); |
522 | fail_u: | |
979ea1dd | 523 | dma_free_coherent(dev, buf->dmas[0].sg_size, |
a5462e5b | 524 | buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma); |
3bc43840 GL |
525 | fail: |
526 | free_buffer(vq, buf); | |
527 | out: | |
528 | buf->inwork = 0; | |
529 | return ret; | |
530 | } | |
531 | ||
256b0233 RJ |
532 | /** |
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 | */ | |
539 | static 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++) { | |
0166b743 GL |
547 | dev_dbg(pcdev->soc_host.v4l2_dev.dev, |
548 | "%s (channel=%d) ddadr=%08x\n", __func__, | |
256b0233 RJ |
549 | 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 | ||
555 | static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev) | |
556 | { | |
557 | int i; | |
558 | ||
559 | for (i = 0; i < pcdev->channels; i++) { | |
0166b743 GL |
560 | dev_dbg(pcdev->soc_host.v4l2_dev.dev, |
561 | "%s (channel=%d)\n", __func__, i); | |
256b0233 RJ |
562 | DCSR(pcdev->dma_chans[i]) = 0; |
563 | } | |
564 | } | |
565 | ||
256b0233 RJ |
566 | static 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 | ||
ae7410e7 GL |
576 | 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; | |
256b0233 | 579 | |
ae7410e7 GL |
580 | /* Update the channel tail */ |
581 | pcdev->sg_tail[i] = buf_last_desc; | |
582 | } | |
256b0233 RJ |
583 | } |
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 | */ | |
593 | static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev) | |
594 | { | |
595 | unsigned long cicr0, cifr; | |
596 | ||
979ea1dd | 597 | dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__); |
256b0233 RJ |
598 | /* 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 | ||
607 | static 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 | ||
8c62e221 | 616 | pcdev->active = NULL; |
979ea1dd | 617 | dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__); |
256b0233 RJ |
618 | } |
619 | ||
2dd54a54 | 620 | /* Called under spinlock_irqsave(&pcdev->lock, ...) */ |
7102b773 GL |
621 | static void pxa_videobuf_queue(struct videobuf_queue *vq, |
622 | struct videobuf_buffer *vb) | |
3bc43840 GL |
623 | { |
624 | struct soc_camera_device *icd = vq->priv_data; | |
64f5905e | 625 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3bc43840 GL |
626 | struct pxa_camera_dev *pcdev = ici->priv; |
627 | struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb); | |
3bc43840 | 628 | |
0166b743 GL |
629 | dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d active=%p\n", |
630 | __func__, vb, vb->baddr, vb->bsize, pcdev->active); | |
256b0233 | 631 | |
3bc43840 GL |
632 | list_add_tail(&vb->queue, &pcdev->capture); |
633 | ||
634 | vb->state = VIDEOBUF_ACTIVE; | |
256b0233 | 635 | pxa_dma_add_tail_buf(pcdev, buf); |
3bc43840 | 636 | |
256b0233 RJ |
637 | if (!pcdev->active) |
638 | pxa_camera_start_capture(pcdev); | |
3bc43840 GL |
639 | } |
640 | ||
641 | static 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; | |
0166b743 | 647 | struct device *dev = icd->dev.parent; |
3bc43840 | 648 | |
0166b743 | 649 | dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, |
3bc43840 GL |
650 | vb, vb->baddr, vb->bsize); |
651 | ||
652 | switch (vb->state) { | |
653 | case VIDEOBUF_ACTIVE: | |
0166b743 | 654 | dev_dbg(dev, "%s (active)\n", __func__); |
3bc43840 GL |
655 | break; |
656 | case VIDEOBUF_QUEUED: | |
0166b743 | 657 | dev_dbg(dev, "%s (queued)\n", __func__); |
3bc43840 GL |
658 | break; |
659 | case VIDEOBUF_PREPARED: | |
0166b743 | 660 | dev_dbg(dev, "%s (prepared)\n", __func__); |
3bc43840 GL |
661 | break; |
662 | default: | |
0166b743 | 663 | dev_dbg(dev, "%s (unknown)\n", __func__); |
3bc43840 GL |
664 | break; |
665 | } | |
666 | #endif | |
667 | ||
668 | free_buffer(vq, buf); | |
669 | } | |
670 | ||
a5462e5b MR |
671 | static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev, |
672 | struct videobuf_buffer *vb, | |
673 | struct pxa_buffer *buf) | |
674 | { | |
256b0233 | 675 | int i; |
5ca11fa3 | 676 | |
a5462e5b MR |
677 | /* _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); | |
979ea1dd GL |
683 | dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s dequeud buffer (vb=0x%p)\n", |
684 | __func__, vb); | |
a5462e5b MR |
685 | |
686 | if (list_empty(&pcdev->capture)) { | |
256b0233 | 687 | pxa_camera_stop_capture(pcdev); |
256b0233 RJ |
688 | for (i = 0; i < pcdev->channels; i++) |
689 | pcdev->sg_tail[i] = NULL; | |
a5462e5b MR |
690 | return; |
691 | } | |
692 | ||
693 | pcdev->active = list_entry(pcdev->capture.next, | |
694 | struct pxa_buffer, vb.queue); | |
695 | } | |
696 | ||
256b0233 RJ |
697 | /** |
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 | */ | |
713 | static 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; | |
979ea1dd GL |
720 | dev_dbg(pcdev->soc_host.v4l2_dev.dev, |
721 | "%s : top queued buffer=%p, dma_stopped=%d\n", | |
256b0233 RJ |
722 | __func__, pcdev->active, is_dma_stopped); |
723 | if (pcdev->active && is_dma_stopped) | |
724 | pxa_camera_start_capture(pcdev); | |
725 | } | |
726 | ||
a5462e5b MR |
727 | static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev, |
728 | enum pxa_camera_active_dma act_dma) | |
3bc43840 | 729 | { |
979ea1dd | 730 | struct device *dev = pcdev->soc_host.v4l2_dev.dev; |
3bc43840 GL |
731 | struct pxa_buffer *buf; |
732 | unsigned long flags; | |
e7c50688 | 733 | u32 status, camera_status, overrun; |
3bc43840 GL |
734 | struct videobuf_buffer *vb; |
735 | ||
736 | spin_lock_irqsave(&pcdev->lock, flags); | |
737 | ||
a5462e5b | 738 | status = DCSR(channel); |
256b0233 RJ |
739 | 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; | |
7102b773 | 745 | |
3bc43840 | 746 | if (status & DCSR_BUSERR) { |
979ea1dd | 747 | dev_err(dev, "DMA Bus Error IRQ!\n"); |
3bc43840 GL |
748 | goto out; |
749 | } | |
750 | ||
256b0233 | 751 | if (!(status & (DCSR_ENDINTR | DCSR_STARTINTR))) { |
979ea1dd GL |
752 | dev_err(dev, "Unknown DMA IRQ source, status: 0x%08x\n", |
753 | status); | |
3bc43840 GL |
754 | goto out; |
755 | } | |
756 | ||
8c62e221 RJ |
757 | /* |
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) | |
3bc43840 | 770 | goto out; |
3bc43840 GL |
771 | |
772 | vb = &pcdev->active->vb; | |
773 | buf = container_of(vb, struct pxa_buffer, vb); | |
774 | WARN_ON(buf->inwork || list_empty(&vb->queue)); | |
3bc43840 | 775 | |
979ea1dd | 776 | dev_dbg(dev, "%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n", |
256b0233 RJ |
777 | __func__, channel, status & DCSR_STARTINTR ? "SOF " : "", |
778 | status & DCSR_ENDINTR ? "EOF " : "", vb, DDADR(channel)); | |
779 | ||
780 | if (status & DCSR_ENDINTR) { | |
8c62e221 RJ |
781 | /* |
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)) { | |
979ea1dd | 787 | dev_dbg(dev, "FIFO overrun! CISR: %x\n", |
256b0233 RJ |
788 | 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 | } | |
3bc43840 GL |
799 | |
800 | out: | |
801 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
802 | } | |
803 | ||
a5462e5b MR |
804 | static 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 | ||
810 | static 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 | ||
816 | static 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 | ||
7102b773 | 822 | static struct videobuf_queue_ops pxa_videobuf_ops = { |
3bc43840 GL |
823 | .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 | ||
a034d1b7 | 829 | static void pxa_camera_init_videobuf(struct videobuf_queue *q, |
092d3921 PZ |
830 | struct soc_camera_device *icd) |
831 | { | |
a034d1b7 MD |
832 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
833 | struct pxa_camera_dev *pcdev = ici->priv; | |
834 | ||
092d3921 PZ |
835 | /* We must pass NULL as dev pointer, then all pci_* dma operations |
836 | * transform to normal dma_* ones. */ | |
a034d1b7 | 837 | videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock, |
092d3921 PZ |
838 | V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE, |
839 | sizeof(struct pxa_buffer), icd); | |
840 | } | |
841 | ||
40e2e092 GL |
842 | static u32 mclk_get_divisor(struct platform_device *pdev, |
843 | struct pxa_camera_dev *pcdev) | |
3bc43840 | 844 | { |
cf34cba7 | 845 | unsigned long mclk = pcdev->mclk; |
6a6c8786 | 846 | struct device *dev = &pdev->dev; |
cf34cba7 | 847 | u32 div; |
3bc43840 GL |
848 | unsigned long lcdclk; |
849 | ||
cf34cba7 GL |
850 | lcdclk = clk_get_rate(pcdev->clk); |
851 | pcdev->ciclk = lcdclk; | |
3bc43840 | 852 | |
cf34cba7 GL |
853 | /* mclk <= ciclk / 4 (27.4.2) */ |
854 | if (mclk > lcdclk / 4) { | |
855 | mclk = lcdclk / 4; | |
979ea1dd | 856 | dev_warn(dev, "Limiting master clock to %lu\n", mclk); |
cf34cba7 GL |
857 | } |
858 | ||
859 | /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */ | |
860 | div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1; | |
3bc43840 | 861 | |
cf34cba7 GL |
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)); | |
3bc43840 | 865 | |
979ea1dd | 866 | dev_dbg(dev, "LCD clock %luHz, target freq %luHz, divisor %u\n", |
40e2e092 | 867 | lcdclk, mclk, div); |
3bc43840 GL |
868 | |
869 | return div; | |
870 | } | |
871 | ||
cf34cba7 GL |
872 | static 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 | ||
7102b773 | 881 | static void pxa_camera_activate(struct pxa_camera_dev *pcdev) |
3bc43840 GL |
882 | { |
883 | struct pxacamera_platform_data *pdata = pcdev->pdata; | |
979ea1dd | 884 | struct device *dev = pcdev->soc_host.v4l2_dev.dev; |
3bc43840 GL |
885 | u32 cicr4 = 0; |
886 | ||
979ea1dd | 887 | dev_dbg(dev, "Registered platform device at %p data %p\n", |
3bc43840 GL |
888 | pcdev, pdata); |
889 | ||
890 | if (pdata && pdata->init) { | |
979ea1dd GL |
891 | dev_dbg(dev, "%s: Init gpios\n", __func__); |
892 | pdata->init(dev); | |
3bc43840 GL |
893 | } |
894 | ||
5ca11fa3 EM |
895 | /* disable all interrupts */ |
896 | __raw_writel(0x3ff, pcdev->base + CICR0); | |
3bc43840 GL |
897 | |
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 | ||
cf34cba7 GL |
909 | __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); | |
3bc43840 GL |
917 | |
918 | clk_enable(pcdev->clk); | |
919 | } | |
920 | ||
7102b773 | 921 | static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev) |
3bc43840 | 922 | { |
3bc43840 | 923 | clk_disable(pcdev->clk); |
3bc43840 GL |
924 | } |
925 | ||
926 | static irqreturn_t pxa_camera_irq(int irq, void *data) | |
927 | { | |
928 | struct pxa_camera_dev *pcdev = data; | |
5ca11fa3 | 929 | unsigned long status, cicr0; |
256b0233 RJ |
930 | struct pxa_buffer *buf; |
931 | struct videobuf_buffer *vb; | |
3bc43840 | 932 | |
5ca11fa3 | 933 | status = __raw_readl(pcdev->base + CISR); |
0166b743 GL |
934 | dev_dbg(pcdev->soc_host.v4l2_dev.dev, |
935 | "Camera interrupt status 0x%lx\n", status); | |
3bc43840 | 936 | |
e7c50688 GL |
937 | if (!status) |
938 | return IRQ_NONE; | |
939 | ||
5ca11fa3 | 940 | __raw_writel(status, pcdev->base + CISR); |
e7c50688 GL |
941 | |
942 | if (status & CISR_EOF) { | |
256b0233 RJ |
943 | 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 | ||
5ca11fa3 EM |
951 | cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM; |
952 | __raw_writel(cicr0, pcdev->base + CICR0); | |
e7c50688 GL |
953 | } |
954 | ||
3bc43840 GL |
955 | return IRQ_HANDLED; |
956 | } | |
957 | ||
1c3bb743 GL |
958 | /* |
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 | */ | |
7102b773 | 963 | static int pxa_camera_add_device(struct soc_camera_device *icd) |
3bc43840 GL |
964 | { |
965 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
966 | struct pxa_camera_dev *pcdev = ici->priv; | |
3bc43840 | 967 | |
979ea1dd GL |
968 | if (pcdev->icd) |
969 | return -EBUSY; | |
3bc43840 | 970 | |
7102b773 | 971 | pxa_camera_activate(pcdev); |
40e2e092 GL |
972 | |
973 | pcdev->icd = icd; | |
3bc43840 | 974 | |
0166b743 | 975 | dev_info(icd->dev.parent, "PXA Camera driver attached to camera %d\n", |
40e2e092 | 976 | icd->devnum); |
3bc43840 | 977 | |
40e2e092 | 978 | return 0; |
3bc43840 GL |
979 | } |
980 | ||
1c3bb743 | 981 | /* Called with .video_lock held */ |
7102b773 | 982 | static void pxa_camera_remove_device(struct soc_camera_device *icd) |
3bc43840 GL |
983 | { |
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 | ||
0166b743 | 989 | dev_info(icd->dev.parent, "PXA Camera driver detached from camera %d\n", |
3bc43840 GL |
990 | icd->devnum); |
991 | ||
992 | /* disable capture, disable interrupts */ | |
5ca11fa3 | 993 | __raw_writel(0x3ff, pcdev->base + CICR0); |
a5462e5b | 994 | |
3bc43840 | 995 | /* Stop DMA engine */ |
a5462e5b MR |
996 | DCSR(pcdev->dma_chans[0]) = 0; |
997 | DCSR(pcdev->dma_chans[1]) = 0; | |
998 | DCSR(pcdev->dma_chans[2]) = 0; | |
3bc43840 | 999 | |
7102b773 | 1000 | pxa_camera_deactivate(pcdev); |
3bc43840 GL |
1001 | |
1002 | pcdev->icd = NULL; | |
1003 | } | |
1004 | ||
ad5f2e85 GL |
1005 | static int test_platform_param(struct pxa_camera_dev *pcdev, |
1006 | unsigned char buswidth, unsigned long *flags) | |
3bc43840 | 1007 | { |
ad5f2e85 GL |
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 | | |
2d9329f3 | 1019 | SOCAM_DATA_ACTIVE_HIGH | |
ad5f2e85 GL |
1020 | SOCAM_PCLK_SAMPLE_RISING | |
1021 | SOCAM_PCLK_SAMPLE_FALLING; | |
3bc43840 GL |
1022 | |
1023 | /* If requested data width is supported by the platform, use it */ | |
ad5f2e85 | 1024 | switch (buswidth) { |
3bc43840 | 1025 | case 10: |
ad5f2e85 GL |
1026 | if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10)) |
1027 | return -EINVAL; | |
1028 | *flags |= SOCAM_DATAWIDTH_10; | |
3bc43840 GL |
1029 | break; |
1030 | case 9: | |
ad5f2e85 GL |
1031 | if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9)) |
1032 | return -EINVAL; | |
1033 | *flags |= SOCAM_DATAWIDTH_9; | |
3bc43840 GL |
1034 | break; |
1035 | case 8: | |
ad5f2e85 GL |
1036 | if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8)) |
1037 | return -EINVAL; | |
1038 | *flags |= SOCAM_DATAWIDTH_8; | |
2a48fc73 RJ |
1039 | break; |
1040 | default: | |
1041 | return -EINVAL; | |
3bc43840 | 1042 | } |
ad5f2e85 GL |
1043 | |
1044 | return 0; | |
1045 | } | |
1046 | ||
6a6c8786 GL |
1047 | static void pxa_camera_setup_cicr(struct soc_camera_device *icd, |
1048 | unsigned long flags, __u32 pixfmt) | |
ad5f2e85 | 1049 | { |
64f5905e | 1050 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
ad5f2e85 | 1051 | struct pxa_camera_dev *pcdev = ici->priv; |
6a6c8786 | 1052 | unsigned long dw, bpp; |
5ca11fa3 | 1053 | u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0; |
3bc43840 GL |
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... */ | |
6a6c8786 | 1057 | switch (flags & SOCAM_DATAWIDTH_MASK) { |
ad5f2e85 | 1058 | case SOCAM_DATAWIDTH_10: |
3bc43840 GL |
1059 | dw = 4; |
1060 | bpp = 0x40; | |
1061 | break; | |
ad5f2e85 | 1062 | case SOCAM_DATAWIDTH_9: |
3bc43840 GL |
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 */ | |
ad5f2e85 | 1069 | case SOCAM_DATAWIDTH_8: |
3bc43840 GL |
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; | |
6a6c8786 | 1078 | if (flags & SOCAM_PCLK_SAMPLE_FALLING) |
3bc43840 | 1079 | cicr4 |= CICR4_PCP; |
6a6c8786 | 1080 | if (flags & SOCAM_HSYNC_ACTIVE_LOW) |
3bc43840 | 1081 | cicr4 |= CICR4_HSP; |
6a6c8786 | 1082 | if (flags & SOCAM_VSYNC_ACTIVE_LOW) |
3bc43840 GL |
1083 | cicr4 |= CICR4_VSP; |
1084 | ||
5ca11fa3 | 1085 | cicr0 = __raw_readl(pcdev->base + CICR0); |
3bc43840 | 1086 | if (cicr0 & CICR0_ENB) |
5ca11fa3 | 1087 | __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0); |
a5462e5b | 1088 | |
6a6c8786 | 1089 | cicr1 = CICR1_PPL_VAL(icd->user_width - 1) | bpp | dw; |
a5462e5b MR |
1090 | |
1091 | switch (pixfmt) { | |
1092 | case V4L2_PIX_FMT_YUV422P: | |
e7c50688 | 1093 | pcdev->channels = 3; |
a5462e5b | 1094 | cicr1 |= CICR1_YCBCR_F; |
2a48fc73 RJ |
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: | |
a5462e5b | 1104 | case V4L2_PIX_FMT_YUYV: |
2a48fc73 | 1105 | case V4L2_PIX_FMT_YVYU: |
a5462e5b MR |
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 | ||
5ca11fa3 | 1117 | cicr2 = 0; |
6a6c8786 | 1118 | cicr3 = CICR3_LPF_VAL(icd->user_height - 1) | |
3bc43840 | 1119 | CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top)); |
cf34cba7 | 1120 | cicr4 |= pcdev->mclk_divisor; |
5ca11fa3 EM |
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); | |
3bc43840 GL |
1126 | |
1127 | /* CIF interrupts are not used, only DMA */ | |
5ca11fa3 EM |
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); | |
6a6c8786 GL |
1132 | } |
1133 | ||
1134 | static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt) | |
1135 | { | |
1136 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
1137 | struct pxa_camera_dev *pcdev = ici->priv; | |
1138 | unsigned long bus_flags, camera_flags, common_flags; | |
1139 | int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags); | |
1140 | struct pxa_cam *cam = icd->host_priv; | |
1141 | ||
1142 | 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) | |
1149 | return -EINVAL; | |
1150 | ||
1151 | pcdev->channels = 1; | |
1152 | ||
1153 | /* 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 | ||
1178 | cam->flags = common_flags; | |
1179 | ||
1180 | ret = icd->ops->set_bus_param(icd, common_flags); | |
1181 | if (ret < 0) | |
1182 | return ret; | |
1183 | ||
1184 | pxa_camera_setup_cicr(icd, common_flags, pixfmt); | |
3bc43840 GL |
1185 | |
1186 | return 0; | |
1187 | } | |
1188 | ||
2a48fc73 RJ |
1189 | static int pxa_camera_try_bus_param(struct soc_camera_device *icd, |
1190 | unsigned char buswidth) | |
ad5f2e85 | 1191 | { |
cf34cba7 | 1192 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
ad5f2e85 GL |
1193 | struct pxa_camera_dev *pcdev = ici->priv; |
1194 | unsigned long bus_flags, camera_flags; | |
2a48fc73 | 1195 | int ret = test_platform_param(pcdev, buswidth, &bus_flags); |
ad5f2e85 GL |
1196 | |
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 | ||
2a48fc73 RJ |
1205 | static 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 | ||
1214 | static bool buswidth_supported(struct soc_camera_device *icd, int depth) | |
ad5f2e85 | 1215 | { |
2a48fc73 RJ |
1216 | 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; | |
ad5f2e85 GL |
1228 | } |
1229 | ||
2a48fc73 | 1230 | static int required_buswidth(const struct soc_camera_data_format *fmt) |
3bc43840 | 1231 | { |
2a48fc73 RJ |
1232 | 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 | } | |
1244 | ||
1245 | static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx, | |
1246 | struct soc_camera_format_xlate *xlate) | |
1247 | { | |
0166b743 | 1248 | struct device *dev = icd->dev.parent; |
2a48fc73 | 1249 | int formats = 0, buswidth, ret; |
6a6c8786 | 1250 | struct pxa_cam *cam; |
2a48fc73 RJ |
1251 | |
1252 | buswidth = required_buswidth(icd->formats + idx); | |
1253 | ||
1254 | if (!buswidth_supported(icd, buswidth)) | |
1255 | return 0; | |
3bc43840 | 1256 | |
2a48fc73 RJ |
1257 | ret = pxa_camera_try_bus_param(icd, buswidth); |
1258 | if (ret < 0) | |
1259 | return 0; | |
1260 | ||
6a6c8786 GL |
1261 | 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 | ||
2a48fc73 RJ |
1271 | 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++; | |
0166b743 | 1279 | dev_dbg(dev, "Providing format %s using %s\n", |
2a48fc73 RJ |
1280 | 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++; | |
0166b743 | 1294 | dev_dbg(dev, "Providing format %s packed\n", |
2a48fc73 RJ |
1295 | 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++; | |
0166b743 | 1306 | dev_dbg(dev, |
2a48fc73 RJ |
1307 | "Providing format %s in pass-through mode\n", |
1308 | icd->formats[idx].name); | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | return formats; | |
1313 | } | |
1314 | ||
6a6c8786 GL |
1315 | static void pxa_camera_put_formats(struct soc_camera_device *icd) |
1316 | { | |
1317 | kfree(icd->host_priv); | |
1318 | icd->host_priv = NULL; | |
1319 | } | |
1320 | ||
1321 | static 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 | ||
09e231b3 | 1328 | static int pxa_camera_set_crop(struct soc_camera_device *icd, |
08590b96 | 1329 | struct v4l2_crop *a) |
09e231b3 | 1330 | { |
08590b96 | 1331 | struct v4l2_rect *rect = &a->c; |
09e231b3 GL |
1332 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
1333 | struct pxa_camera_dev *pcdev = ici->priv; | |
0166b743 | 1334 | struct device *dev = icd->dev.parent; |
c9c1f1c0 | 1335 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
09e231b3 GL |
1336 | struct soc_camera_sense sense = { |
1337 | .master_clock = pcdev->mclk, | |
1338 | .pixel_clock_max = pcdev->ciclk / 4, | |
1339 | }; | |
6a6c8786 GL |
1340 | struct v4l2_format f; |
1341 | struct v4l2_pix_format *pix = &f.fmt.pix, pix_tmp; | |
1342 | struct pxa_cam *cam = icd->host_priv; | |
09e231b3 GL |
1343 | int ret; |
1344 | ||
1345 | /* 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 | ||
08590b96 | 1349 | ret = v4l2_subdev_call(sd, video, s_crop, a); |
09e231b3 GL |
1350 | |
1351 | icd->sense = NULL; | |
1352 | ||
1353 | if (ret < 0) { | |
0166b743 | 1354 | dev_warn(dev, "Failed to crop to %ux%u@%u:%u\n", |
09e231b3 | 1355 | rect->width, rect->height, rect->left, rect->top); |
6a6c8786 GL |
1356 | 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) { | |
09e231b3 | 1387 | if (sense.pixel_clock > sense.pixel_clock_max) { |
0166b743 | 1388 | dev_err(dev, |
09e231b3 GL |
1389 | "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 | } | |
1395 | ||
6a6c8786 GL |
1396 | 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 | ||
09e231b3 GL |
1401 | return ret; |
1402 | } | |
1403 | ||
d8fac217 | 1404 | static int pxa_camera_set_fmt(struct soc_camera_device *icd, |
09e231b3 | 1405 | struct v4l2_format *f) |
ad5f2e85 | 1406 | { |
2a48fc73 | 1407 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
cf34cba7 | 1408 | struct pxa_camera_dev *pcdev = ici->priv; |
0166b743 | 1409 | struct device *dev = icd->dev.parent; |
c9c1f1c0 | 1410 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
0ad675eb GL |
1411 | const struct soc_camera_data_format *cam_fmt = NULL; |
1412 | const struct soc_camera_format_xlate *xlate = NULL; | |
cf34cba7 GL |
1413 | struct soc_camera_sense sense = { |
1414 | .master_clock = pcdev->mclk, | |
1415 | .pixel_clock_max = pcdev->ciclk / 4, | |
1416 | }; | |
09e231b3 GL |
1417 | struct v4l2_pix_format *pix = &f->fmt.pix; |
1418 | struct v4l2_format cam_f = *f; | |
0ad675eb | 1419 | int ret; |
25c4d74e | 1420 | |
09e231b3 GL |
1421 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
1422 | if (!xlate) { | |
0166b743 | 1423 | dev_warn(dev, "Format %x not found\n", pix->pixelformat); |
09e231b3 | 1424 | return -EINVAL; |
0ad675eb | 1425 | } |
2a48fc73 | 1426 | |
09e231b3 GL |
1427 | cam_fmt = xlate->cam_fmt; |
1428 | ||
cf34cba7 GL |
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 | ||
09e231b3 | 1433 | cam_f.fmt.pix.pixelformat = cam_fmt->fourcc; |
c9c1f1c0 | 1434 | ret = v4l2_subdev_call(sd, video, s_fmt, f); |
2a48fc73 | 1435 | |
cf34cba7 GL |
1436 | icd->sense = NULL; |
1437 | ||
1438 | if (ret < 0) { | |
0166b743 | 1439 | dev_warn(dev, "Failed to configure for format %x\n", |
09e231b3 | 1440 | pix->pixelformat); |
6a6c8786 GL |
1441 | } 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; | |
cf34cba7 GL |
1446 | } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { |
1447 | if (sense.pixel_clock > sense.pixel_clock_max) { | |
0166b743 | 1448 | dev_err(dev, |
cf34cba7 GL |
1449 | "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 | } | |
2a48fc73 | 1455 | |
09e231b3 | 1456 | if (!ret) { |
0ad675eb GL |
1457 | icd->buswidth = xlate->buswidth; |
1458 | icd->current_fmt = xlate->host_fmt; | |
2a48fc73 | 1459 | } |
25c4d74e GL |
1460 | |
1461 | return ret; | |
ad5f2e85 GL |
1462 | } |
1463 | ||
d8fac217 GL |
1464 | static int pxa_camera_try_fmt(struct soc_camera_device *icd, |
1465 | struct v4l2_format *f) | |
3bc43840 | 1466 | { |
2a48fc73 | 1467 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
c9c1f1c0 | 1468 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
2a48fc73 RJ |
1469 | const struct soc_camera_format_xlate *xlate; |
1470 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
1471 | __u32 pixfmt = pix->pixelformat; | |
06daa1af | 1472 | enum v4l2_field field; |
bf507158 | 1473 | int ret; |
a2c8c68c | 1474 | |
2a48fc73 RJ |
1475 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
1476 | if (!xlate) { | |
979ea1dd | 1477 | dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt); |
25c4d74e | 1478 | return -EINVAL; |
2a48fc73 | 1479 | } |
25c4d74e | 1480 | |
92a8337b | 1481 | /* |
4a6b8df2 TP |
1482 | * 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. | |
92a8337b | 1486 | */ |
4a6b8df2 TP |
1487 | v4l_bound_align_image(&pix->width, 48, 2048, 1, |
1488 | &pix->height, 32, 2048, 0, | |
6a6c8786 | 1489 | pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0); |
92a8337b | 1490 | |
2a48fc73 RJ |
1491 | pix->bytesperline = pix->width * |
1492 | DIV_ROUND_UP(xlate->host_fmt->depth, 8); | |
1493 | pix->sizeimage = pix->height * pix->bytesperline; | |
25c4d74e | 1494 | |
bf507158 GL |
1495 | /* camera has to see its format, but the user the original one */ |
1496 | pix->pixelformat = xlate->cam_fmt->fourcc; | |
ad5f2e85 | 1497 | /* limit to sensor capabilities */ |
c9c1f1c0 | 1498 | ret = v4l2_subdev_call(sd, video, try_fmt, f); |
6a6c8786 | 1499 | pix->pixelformat = pixfmt; |
bf507158 | 1500 | |
06daa1af GL |
1501 | field = pix->field; |
1502 | ||
1503 | if (field == V4L2_FIELD_ANY) { | |
1504 | pix->field = V4L2_FIELD_NONE; | |
1505 | } else if (field != V4L2_FIELD_NONE) { | |
0166b743 | 1506 | dev_err(icd->dev.parent, "Field type %d unsupported.\n", field); |
06daa1af GL |
1507 | return -EINVAL; |
1508 | } | |
1509 | ||
bf507158 | 1510 | return ret; |
3bc43840 GL |
1511 | } |
1512 | ||
7102b773 GL |
1513 | static int pxa_camera_reqbufs(struct soc_camera_file *icf, |
1514 | struct v4l2_requestbuffers *p) | |
3bc43840 GL |
1515 | { |
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 | ||
7102b773 | 1532 | static unsigned int pxa_camera_poll(struct file *file, poll_table *pt) |
3bc43840 GL |
1533 | { |
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 | ||
7102b773 GL |
1549 | static int pxa_camera_querycap(struct soc_camera_host *ici, |
1550 | struct v4l2_capability *cap) | |
3bc43840 GL |
1551 | { |
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 | ||
3f6ac497 RJ |
1560 | static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state) |
1561 | { | |
64f5905e | 1562 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3f6ac497 RJ |
1563 | struct pxa_camera_dev *pcdev = ici->priv; |
1564 | int i = 0, ret = 0; | |
1565 | ||
5ca11fa3 EM |
1566 | 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); | |
3f6ac497 RJ |
1571 | |
1572 | if ((pcdev->icd) && (pcdev->icd->ops->suspend)) | |
1573 | ret = pcdev->icd->ops->suspend(pcdev->icd, state); | |
1574 | ||
1575 | return ret; | |
1576 | } | |
1577 | ||
1578 | static int pxa_camera_resume(struct soc_camera_device *icd) | |
1579 | { | |
64f5905e | 1580 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
3f6ac497 RJ |
1581 | struct pxa_camera_dev *pcdev = ici->priv; |
1582 | int i = 0, ret = 0; | |
1583 | ||
87f3dd77 EM |
1584 | 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; | |
3f6ac497 | 1587 | |
5ca11fa3 EM |
1588 | __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); | |
3f6ac497 RJ |
1593 | |
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 */ | |
256b0233 RJ |
1598 | if (!ret && pcdev->active) |
1599 | pxa_camera_start_capture(pcdev); | |
3f6ac497 RJ |
1600 | |
1601 | return ret; | |
1602 | } | |
1603 | ||
b8d9904c GL |
1604 | static 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, | |
3f6ac497 RJ |
1608 | .suspend = pxa_camera_suspend, |
1609 | .resume = pxa_camera_resume, | |
09e231b3 | 1610 | .set_crop = pxa_camera_set_crop, |
2a48fc73 | 1611 | .get_formats = pxa_camera_get_formats, |
6a6c8786 | 1612 | .put_formats = pxa_camera_put_formats, |
d8fac217 GL |
1613 | .set_fmt = pxa_camera_set_fmt, |
1614 | .try_fmt = pxa_camera_try_fmt, | |
092d3921 | 1615 | .init_videobuf = pxa_camera_init_videobuf, |
b8d9904c GL |
1616 | .reqbufs = pxa_camera_reqbufs, |
1617 | .poll = pxa_camera_poll, | |
1618 | .querycap = pxa_camera_querycap, | |
b8d9904c GL |
1619 | .set_bus_param = pxa_camera_set_bus_param, |
1620 | }; | |
1621 | ||
e36bc31f | 1622 | static int __devinit pxa_camera_probe(struct platform_device *pdev) |
3bc43840 GL |
1623 | { |
1624 | struct pxa_camera_dev *pcdev; | |
1625 | struct resource *res; | |
1626 | void __iomem *base; | |
02da4659 | 1627 | int irq; |
3bc43840 GL |
1628 | int err = 0; |
1629 | ||
1630 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1631 | irq = platform_get_irq(pdev, 0); | |
02da4659 | 1632 | if (!res || irq < 0) { |
3bc43840 GL |
1633 | err = -ENODEV; |
1634 | goto exit; | |
1635 | } | |
1636 | ||
1637 | pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL); | |
1638 | if (!pcdev) { | |
7102b773 | 1639 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
3bc43840 GL |
1640 | err = -ENOMEM; |
1641 | goto exit; | |
1642 | } | |
1643 | ||
e0d8b13a | 1644 | pcdev->clk = clk_get(&pdev->dev, NULL); |
3bc43840 GL |
1645 | if (IS_ERR(pcdev->clk)) { |
1646 | err = PTR_ERR(pcdev->clk); | |
1647 | goto exit_kfree; | |
1648 | } | |
1649 | ||
3bc43840 GL |
1650 | pcdev->res = res; |
1651 | ||
1652 | pcdev->pdata = pdev->dev.platform_data; | |
1653 | pcdev->platform_flags = pcdev->pdata->flags; | |
ad5f2e85 GL |
1654 | if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 | |
1655 | PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) { | |
3bc43840 GL |
1656 | /* 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 | } | |
cf34cba7 GL |
1662 | pcdev->mclk = pcdev->pdata->mclk_10khz * 10000; |
1663 | if (!pcdev->mclk) { | |
3bc43840 | 1664 | dev_warn(&pdev->dev, |
cf34cba7 | 1665 | "mclk == 0! Please, fix your platform data. " |
3bc43840 | 1666 | "Using default 20MHz\n"); |
cf34cba7 | 1667 | pcdev->mclk = 20000000; |
3bc43840 GL |
1668 | } |
1669 | ||
40e2e092 | 1670 | pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev); |
cf34cba7 | 1671 | |
3bc43840 GL |
1672 | INIT_LIST_HEAD(&pcdev->capture); |
1673 | spin_lock_init(&pcdev->lock); | |
1674 | ||
1675 | /* | |
1676 | * Request the regions. | |
1677 | */ | |
eb6c8558 | 1678 | if (!request_mem_region(res->start, resource_size(res), |
3bc43840 GL |
1679 | PXA_CAM_DRV_NAME)) { |
1680 | err = -EBUSY; | |
1681 | goto exit_clk; | |
1682 | } | |
1683 | ||
eb6c8558 | 1684 | base = ioremap(res->start, resource_size(res)); |
3bc43840 GL |
1685 | if (!base) { |
1686 | err = -ENOMEM; | |
1687 | goto exit_release; | |
1688 | } | |
1689 | pcdev->irq = irq; | |
1690 | pcdev->base = base; | |
3bc43840 GL |
1691 | |
1692 | /* request dma */ | |
de3e3b82 | 1693 | err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH, |
1694 | pxa_camera_dma_irq_y, pcdev); | |
1695 | if (err < 0) { | |
eff505fa | 1696 | dev_err(&pdev->dev, "Can't request DMA for Y\n"); |
3bc43840 GL |
1697 | goto exit_iounmap; |
1698 | } | |
de3e3b82 | 1699 | pcdev->dma_chans[0] = err; |
eff505fa | 1700 | dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]); |
a5462e5b | 1701 | |
de3e3b82 | 1702 | err = pxa_request_dma("CI_U", DMA_PRIO_HIGH, |
1703 | pxa_camera_dma_irq_u, pcdev); | |
1704 | if (err < 0) { | |
eff505fa | 1705 | dev_err(&pdev->dev, "Can't request DMA for U\n"); |
a5462e5b MR |
1706 | goto exit_free_dma_y; |
1707 | } | |
de3e3b82 | 1708 | pcdev->dma_chans[1] = err; |
eff505fa | 1709 | dev_dbg(&pdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]); |
a5462e5b | 1710 | |
de3e3b82 | 1711 | err = pxa_request_dma("CI_V", DMA_PRIO_HIGH, |
1712 | pxa_camera_dma_irq_v, pcdev); | |
1713 | if (err < 0) { | |
eff505fa | 1714 | dev_err(&pdev->dev, "Can't request DMA for V\n"); |
a5462e5b MR |
1715 | goto exit_free_dma_u; |
1716 | } | |
de3e3b82 | 1717 | pcdev->dma_chans[2] = err; |
eff505fa | 1718 | dev_dbg(&pdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]); |
3bc43840 | 1719 | |
87f3dd77 EM |
1720 | 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; | |
3bc43840 GL |
1723 | |
1724 | /* request irq */ | |
1725 | err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME, | |
1726 | pcdev); | |
1727 | if (err) { | |
eff505fa | 1728 | dev_err(&pdev->dev, "Camera interrupt register failed \n"); |
3bc43840 GL |
1729 | goto exit_free_dma; |
1730 | } | |
1731 | ||
eb6c8558 GL |
1732 | 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; | |
979ea1dd | 1735 | pcdev->soc_host.v4l2_dev.dev = &pdev->dev; |
eb6c8558 | 1736 | pcdev->soc_host.nr = pdev->id; |
eff505fa | 1737 | |
eb6c8558 | 1738 | err = soc_camera_host_register(&pcdev->soc_host); |
3bc43840 GL |
1739 | if (err) |
1740 | goto exit_free_irq; | |
1741 | ||
1742 | return 0; | |
1743 | ||
1744 | exit_free_irq: | |
1745 | free_irq(pcdev->irq, pcdev); | |
1746 | exit_free_dma: | |
a5462e5b MR |
1747 | pxa_free_dma(pcdev->dma_chans[2]); |
1748 | exit_free_dma_u: | |
1749 | pxa_free_dma(pcdev->dma_chans[1]); | |
1750 | exit_free_dma_y: | |
1751 | pxa_free_dma(pcdev->dma_chans[0]); | |
3bc43840 GL |
1752 | exit_iounmap: |
1753 | iounmap(base); | |
1754 | exit_release: | |
eb6c8558 | 1755 | release_mem_region(res->start, resource_size(res)); |
3bc43840 GL |
1756 | exit_clk: |
1757 | clk_put(pcdev->clk); | |
1758 | exit_kfree: | |
1759 | kfree(pcdev); | |
1760 | exit: | |
1761 | return err; | |
1762 | } | |
1763 | ||
1764 | static int __devexit pxa_camera_remove(struct platform_device *pdev) | |
1765 | { | |
eff505fa GL |
1766 | 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); | |
3bc43840 GL |
1769 | struct resource *res; |
1770 | ||
1771 | clk_put(pcdev->clk); | |
1772 | ||
a5462e5b MR |
1773 | pxa_free_dma(pcdev->dma_chans[0]); |
1774 | pxa_free_dma(pcdev->dma_chans[1]); | |
1775 | pxa_free_dma(pcdev->dma_chans[2]); | |
3bc43840 GL |
1776 | free_irq(pcdev->irq, pcdev); |
1777 | ||
eff505fa | 1778 | soc_camera_host_unregister(soc_host); |
3bc43840 GL |
1779 | |
1780 | iounmap(pcdev->base); | |
1781 | ||
1782 | res = pcdev->res; | |
eb6c8558 | 1783 | release_mem_region(res->start, resource_size(res)); |
3bc43840 GL |
1784 | |
1785 | kfree(pcdev); | |
1786 | ||
7102b773 | 1787 | dev_info(&pdev->dev, "PXA Camera driver unloaded\n"); |
3bc43840 | 1788 | |
3bc43840 GL |
1789 | return 0; |
1790 | } | |
1791 | ||
3bc43840 GL |
1792 | static struct platform_driver pxa_camera_driver = { |
1793 | .driver = { | |
1794 | .name = PXA_CAM_DRV_NAME, | |
1795 | }, | |
1796 | .probe = pxa_camera_probe, | |
e36bc31f | 1797 | .remove = __devexit_p(pxa_camera_remove), |
3bc43840 GL |
1798 | }; |
1799 | ||
1800 | ||
e36bc31f | 1801 | static int __init pxa_camera_init(void) |
3bc43840 GL |
1802 | { |
1803 | return platform_driver_register(&pxa_camera_driver); | |
1804 | } | |
1805 | ||
1806 | static void __exit pxa_camera_exit(void) | |
1807 | { | |
01c1e4ca | 1808 | platform_driver_unregister(&pxa_camera_driver); |
3bc43840 GL |
1809 | } |
1810 | ||
1811 | module_init(pxa_camera_init); | |
1812 | module_exit(pxa_camera_exit); | |
1813 | ||
1814 | MODULE_DESCRIPTION("PXA27x SoC Camera Host driver"); | |
1815 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
1816 | MODULE_LICENSE("GPL"); | |
40e2e092 | 1817 | MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME); |