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