]>
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> | |
283e4a82 | 6 | * Copyright (C) 2016, Robert Jarzmik <robert.jarzmik@free.fr> |
3bc43840 GL |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | */ | |
13 | ||
3bc43840 GL |
14 | #include <linux/init.h> |
15 | #include <linux/module.h> | |
7102b773 | 16 | #include <linux/io.h> |
3bc43840 | 17 | #include <linux/delay.h> |
283e4a82 | 18 | #include <linux/device.h> |
3bc43840 | 19 | #include <linux/dma-mapping.h> |
8efdb135 | 20 | #include <linux/err.h> |
3bc43840 GL |
21 | #include <linux/errno.h> |
22 | #include <linux/fs.h> | |
23 | #include <linux/interrupt.h> | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/mm.h> | |
26 | #include <linux/moduleparam.h> | |
283e4a82 | 27 | #include <linux/of.h> |
3bc43840 | 28 | #include <linux/time.h> |
3bc43840 GL |
29 | #include <linux/device.h> |
30 | #include <linux/platform_device.h> | |
3bc43840 | 31 | #include <linux/clk.h> |
d514edac | 32 | #include <linux/sched.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
1e77d55a RJ |
34 | #include <linux/dmaengine.h> |
35 | #include <linux/dma-mapping.h> | |
36 | #include <linux/dma/pxa-dma.h> | |
3bc43840 | 37 | |
283e4a82 RJ |
38 | #include <media/v4l2-async.h> |
39 | #include <media/v4l2-clk.h> | |
3bc43840 | 40 | #include <media/v4l2-common.h> |
283e4a82 RJ |
41 | #include <media/v4l2-device.h> |
42 | #include <media/v4l2-ioctl.h> | |
e9a1d94f | 43 | #include <media/v4l2-of.h> |
3bc43840 | 44 | |
283e4a82 RJ |
45 | #include <media/drv-intf/soc_mediabus.h> |
46 | #include <media/videobuf2-dma-sg.h> | |
47 | ||
3bc43840 GL |
48 | #include <linux/videodev2.h> |
49 | ||
a71daaa1 | 50 | #include <linux/platform_data/media/camera-pxa.h> |
3bc43840 | 51 | |
64dc3c1a | 52 | #define PXA_CAM_VERSION "0.0.6" |
3bc43840 GL |
53 | #define PXA_CAM_DRV_NAME "pxa27x-camera" |
54 | ||
283e4a82 RJ |
55 | #define DEFAULT_WIDTH 640 |
56 | #define DEFAULT_HEIGHT 480 | |
57 | ||
5ca11fa3 EM |
58 | /* Camera Interface */ |
59 | #define CICR0 0x0000 | |
60 | #define CICR1 0x0004 | |
61 | #define CICR2 0x0008 | |
62 | #define CICR3 0x000C | |
63 | #define CICR4 0x0010 | |
64 | #define CISR 0x0014 | |
65 | #define CIFR 0x0018 | |
66 | #define CITOR 0x001C | |
67 | #define CIBR0 0x0028 | |
68 | #define CIBR1 0x0030 | |
69 | #define CIBR2 0x0038 | |
70 | ||
71 | #define CICR0_DMAEN (1 << 31) /* DMA request enable */ | |
72 | #define CICR0_PAR_EN (1 << 30) /* Parity enable */ | |
73 | #define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */ | |
74 | #define CICR0_ENB (1 << 28) /* Camera interface enable */ | |
75 | #define CICR0_DIS (1 << 27) /* Camera interface disable */ | |
76 | #define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */ | |
77 | #define CICR0_TOM (1 << 9) /* Time-out mask */ | |
78 | #define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */ | |
79 | #define CICR0_FEM (1 << 7) /* FIFO-empty mask */ | |
80 | #define CICR0_EOLM (1 << 6) /* End-of-line mask */ | |
81 | #define CICR0_PERRM (1 << 5) /* Parity-error mask */ | |
82 | #define CICR0_QDM (1 << 4) /* Quick-disable mask */ | |
83 | #define CICR0_CDM (1 << 3) /* Disable-done mask */ | |
84 | #define CICR0_SOFM (1 << 2) /* Start-of-frame mask */ | |
85 | #define CICR0_EOFM (1 << 1) /* End-of-frame mask */ | |
86 | #define CICR0_FOM (1 << 0) /* FIFO-overrun mask */ | |
87 | ||
88 | #define CICR1_TBIT (1 << 31) /* Transparency bit */ | |
89 | #define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */ | |
90 | #define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */ | |
91 | #define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */ | |
92 | #define CICR1_RGB_F (1 << 11) /* RGB format */ | |
93 | #define CICR1_YCBCR_F (1 << 10) /* YCbCr format */ | |
94 | #define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */ | |
95 | #define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */ | |
96 | #define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */ | |
97 | #define CICR1_DW (0x7 << 0) /* Data width mask */ | |
98 | ||
99 | #define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock | |
100 | wait count mask */ | |
101 | #define CICR2_ELW (0xff << 16) /* End-of-line pixel clock | |
102 | wait count mask */ | |
103 | #define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */ | |
104 | #define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock | |
105 | wait count mask */ | |
106 | #define CICR2_FSW (0x7 << 0) /* Frame stabilization | |
107 | wait count mask */ | |
108 | ||
109 | #define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock | |
110 | wait count mask */ | |
111 | #define CICR3_EFW (0xff << 16) /* End-of-frame line clock | |
112 | wait count mask */ | |
113 | #define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */ | |
114 | #define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock | |
115 | wait count mask */ | |
116 | #define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */ | |
117 | ||
118 | #define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */ | |
119 | #define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */ | |
120 | #define CICR4_PCP (1 << 22) /* Pixel clock polarity */ | |
121 | #define CICR4_HSP (1 << 21) /* Horizontal sync polarity */ | |
122 | #define CICR4_VSP (1 << 20) /* Vertical sync polarity */ | |
123 | #define CICR4_MCLK_EN (1 << 19) /* MCLK enable */ | |
124 | #define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */ | |
125 | #define CICR4_DIV (0xff << 0) /* Clock divisor mask */ | |
126 | ||
127 | #define CISR_FTO (1 << 15) /* FIFO time-out */ | |
128 | #define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */ | |
129 | #define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */ | |
130 | #define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */ | |
131 | #define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */ | |
132 | #define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */ | |
133 | #define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */ | |
134 | #define CISR_EOL (1 << 8) /* End of line */ | |
135 | #define CISR_PAR_ERR (1 << 7) /* Parity error */ | |
136 | #define CISR_CQD (1 << 6) /* Camera interface quick disable */ | |
137 | #define CISR_CDD (1 << 5) /* Camera interface disable done */ | |
138 | #define CISR_SOF (1 << 4) /* Start of frame */ | |
139 | #define CISR_EOF (1 << 3) /* End of frame */ | |
140 | #define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */ | |
141 | #define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */ | |
142 | #define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */ | |
143 | ||
144 | #define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */ | |
145 | #define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */ | |
146 | #define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */ | |
147 | #define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */ | |
148 | #define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */ | |
149 | #define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */ | |
150 | #define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */ | |
151 | #define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */ | |
152 | ||
7102b773 GL |
153 | #define CICR0_SIM_MP (0 << 24) |
154 | #define CICR0_SIM_SP (1 << 24) | |
155 | #define CICR0_SIM_MS (2 << 24) | |
156 | #define CICR0_SIM_EP (3 << 24) | |
157 | #define CICR0_SIM_ES (4 << 24) | |
158 | ||
159 | #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */ | |
160 | #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */ | |
a5462e5b MR |
161 | #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */ |
162 | #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */ | |
163 | #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */ | |
7102b773 GL |
164 | |
165 | #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */ | |
166 | #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */ | |
167 | #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */ | |
168 | #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */ | |
169 | #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */ | |
170 | ||
171 | #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */ | |
172 | #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */ | |
173 | #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */ | |
174 | #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */ | |
175 | ||
3bc43840 GL |
176 | #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \ |
177 | CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \ | |
178 | CICR0_EOFM | CICR0_FOM) | |
179 | ||
b36bcbd6 | 180 | #define sensor_call(cam, o, f, args...) \ |
283e4a82 RJ |
181 | v4l2_subdev_call(cam->sensor, o, f, ##args) |
182 | ||
183 | /* | |
184 | * Format handling | |
185 | */ | |
186 | /** | |
187 | * struct soc_camera_format_xlate - match between host and sensor formats | |
188 | * @code: code of a sensor provided format | |
189 | * @host_fmt: host format after host translation from code | |
190 | * | |
191 | * Host and sensor translation structure. Used in table of host and sensor | |
192 | * formats matchings in soc_camera_device. A host can override the generic list | |
193 | * generation by implementing get_formats(), and use it for format checks and | |
194 | * format setup. | |
195 | */ | |
196 | struct soc_camera_format_xlate { | |
197 | u32 code; | |
198 | const struct soc_mbus_pixelfmt *host_fmt; | |
199 | }; | |
b36bcbd6 | 200 | |
3bc43840 GL |
201 | /* |
202 | * Structures | |
203 | */ | |
a5462e5b MR |
204 | enum pxa_camera_active_dma { |
205 | DMA_Y = 0x1, | |
206 | DMA_U = 0x2, | |
207 | DMA_V = 0x4, | |
208 | }; | |
209 | ||
3bc43840 GL |
210 | /* buffer for one video frame */ |
211 | struct pxa_buffer { | |
212 | /* common v4l buffer stuff -- must be first */ | |
e009ebd5 RJ |
213 | struct vb2_v4l2_buffer vbuf; |
214 | struct list_head queue; | |
27ffaeb0 | 215 | u32 code; |
e009ebd5 | 216 | int nb_planes; |
a5462e5b | 217 | /* our descriptor lists for Y, U and V channels */ |
1e77d55a RJ |
218 | struct dma_async_tx_descriptor *descs[3]; |
219 | dma_cookie_t cookie[3]; | |
220 | struct scatterlist *sg[3]; | |
221 | int sg_len[3]; | |
e009ebd5 | 222 | size_t plane_sizes[3]; |
760697be GL |
223 | int inwork; |
224 | enum pxa_camera_active_dma active_dma; | |
3bc43840 GL |
225 | }; |
226 | ||
3bc43840 | 227 | struct pxa_camera_dev { |
283e4a82 RJ |
228 | struct v4l2_device v4l2_dev; |
229 | struct video_device vdev; | |
230 | struct v4l2_async_notifier notifier; | |
231 | struct vb2_queue vb2_vq; | |
232 | struct v4l2_subdev *sensor; | |
233 | struct soc_camera_format_xlate *user_formats; | |
234 | const struct soc_camera_format_xlate *current_fmt; | |
235 | struct v4l2_pix_format current_pix; | |
236 | ||
237 | struct v4l2_async_subdev asd; | |
238 | struct v4l2_async_subdev *asds[1]; | |
239 | ||
5d28d525 GL |
240 | /* |
241 | * PXA27x is only supposed to handle one camera on its Quick Capture | |
3bc43840 | 242 | * interface. If anyone ever builds hardware to enable more than |
5d28d525 GL |
243 | * one camera, they will have to modify this driver too |
244 | */ | |
3bc43840 GL |
245 | struct clk *clk; |
246 | ||
247 | unsigned int irq; | |
248 | void __iomem *base; | |
a5462e5b | 249 | |
e7c50688 | 250 | int channels; |
1e77d55a | 251 | struct dma_chan *dma_chans[3]; |
3bc43840 | 252 | |
3bc43840 GL |
253 | struct pxacamera_platform_data *pdata; |
254 | struct resource *res; | |
255 | unsigned long platform_flags; | |
cf34cba7 GL |
256 | unsigned long ciclk; |
257 | unsigned long mclk; | |
258 | u32 mclk_divisor; | |
283e4a82 | 259 | struct v4l2_clk *mclk_clk; |
679419aa | 260 | u16 width_flags; /* max 10 bits */ |
3bc43840 GL |
261 | |
262 | struct list_head capture; | |
263 | ||
264 | spinlock_t lock; | |
283e4a82 | 265 | struct mutex mlock; |
61634976 | 266 | unsigned int buf_sequence; |
3bc43840 | 267 | |
3bc43840 | 268 | struct pxa_buffer *active; |
e623ebe6 | 269 | struct tasklet_struct task_eof; |
3f6ac497 RJ |
270 | |
271 | u32 save_cicr[5]; | |
3bc43840 GL |
272 | }; |
273 | ||
6a6c8786 GL |
274 | struct pxa_cam { |
275 | unsigned long flags; | |
276 | }; | |
277 | ||
3bc43840 GL |
278 | static const char *pxa_cam_driver_description = "PXA_Camera"; |
279 | ||
283e4a82 RJ |
280 | /* |
281 | * Format translation functions | |
282 | */ | |
283 | const struct soc_camera_format_xlate *soc_mbus_xlate_by_fourcc( | |
284 | struct soc_camera_format_xlate *user_formats, unsigned int fourcc) | |
285 | { | |
286 | unsigned int i; | |
287 | ||
288 | for (i = 0; user_formats[i].code; i++) | |
289 | if (user_formats[i].host_fmt->fourcc == fourcc) | |
290 | return user_formats + i; | |
291 | return NULL; | |
292 | } | |
293 | ||
294 | static struct soc_camera_format_xlate *soc_mbus_build_fmts_xlate( | |
295 | struct v4l2_device *v4l2_dev, struct v4l2_subdev *subdev, | |
296 | int (*get_formats)(struct v4l2_device *, unsigned int, | |
297 | struct soc_camera_format_xlate *xlate)) | |
295ab497 | 298 | { |
283e4a82 RJ |
299 | unsigned int i, fmts = 0, raw_fmts = 0; |
300 | int ret; | |
301 | struct v4l2_subdev_mbus_code_enum code = { | |
302 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
303 | }; | |
304 | struct soc_camera_format_xlate *user_formats; | |
305 | ||
306 | while (!v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) { | |
307 | raw_fmts++; | |
308 | code.index++; | |
309 | } | |
310 | ||
311 | /* | |
312 | * First pass - only count formats this host-sensor | |
313 | * configuration can provide | |
314 | */ | |
315 | for (i = 0; i < raw_fmts; i++) { | |
316 | ret = get_formats(v4l2_dev, i, NULL); | |
317 | if (ret < 0) | |
318 | return ERR_PTR(ret); | |
319 | fmts += ret; | |
320 | } | |
321 | ||
322 | if (!fmts) | |
323 | return ERR_PTR(-ENXIO); | |
295ab497 | 324 | |
283e4a82 RJ |
325 | user_formats = kcalloc(fmts + 1, sizeof(*user_formats), GFP_KERNEL); |
326 | if (!user_formats) | |
327 | return ERR_PTR(-ENOMEM); | |
328 | ||
329 | /* Second pass - actually fill data formats */ | |
330 | fmts = 0; | |
331 | for (i = 0; i < raw_fmts; i++) { | |
332 | ret = get_formats(v4l2_dev, i, user_formats + fmts); | |
333 | if (ret < 0) | |
334 | goto egfmt; | |
335 | fmts += ret; | |
336 | } | |
337 | user_formats[fmts].code = 0; | |
338 | ||
339 | return user_formats; | |
340 | egfmt: | |
341 | kfree(user_formats); | |
342 | return ERR_PTR(ret); | |
295ab497 RJ |
343 | } |
344 | ||
3bc43840 GL |
345 | /* |
346 | * Videobuf operations | |
347 | */ | |
e009ebd5 | 348 | static struct pxa_buffer *vb2_to_pxa_buffer(struct vb2_buffer *vb) |
3bc43840 | 349 | { |
e009ebd5 | 350 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
3bc43840 | 351 | |
e009ebd5 | 352 | return container_of(vbuf, struct pxa_buffer, vbuf); |
3bc43840 GL |
353 | } |
354 | ||
e009ebd5 | 355 | static struct device *pcdev_to_dev(struct pxa_camera_dev *pcdev) |
3bc43840 | 356 | { |
283e4a82 RJ |
357 | return pcdev->v4l2_dev.dev; |
358 | } | |
359 | ||
360 | static struct pxa_camera_dev *v4l2_dev_to_pcdev(struct v4l2_device *v4l2_dev) | |
361 | { | |
362 | return container_of(v4l2_dev, struct pxa_camera_dev, v4l2_dev); | |
37f5aefd RJ |
363 | } |
364 | ||
1e77d55a | 365 | static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev, |
e5853918 RJ |
366 | enum pxa_camera_active_dma act_dma); |
367 | ||
1e77d55a | 368 | static void pxa_camera_dma_irq_y(void *data) |
e5853918 RJ |
369 | { |
370 | struct pxa_camera_dev *pcdev = data; | |
371 | ||
1e77d55a | 372 | pxa_camera_dma_irq(pcdev, DMA_Y); |
e5853918 RJ |
373 | } |
374 | ||
1e77d55a | 375 | static void pxa_camera_dma_irq_u(void *data) |
e5853918 RJ |
376 | { |
377 | struct pxa_camera_dev *pcdev = data; | |
378 | ||
1e77d55a | 379 | pxa_camera_dma_irq(pcdev, DMA_U); |
e5853918 RJ |
380 | } |
381 | ||
1e77d55a | 382 | static void pxa_camera_dma_irq_v(void *data) |
e5853918 RJ |
383 | { |
384 | struct pxa_camera_dev *pcdev = data; | |
385 | ||
1e77d55a | 386 | pxa_camera_dma_irq(pcdev, DMA_V); |
e5853918 RJ |
387 | } |
388 | ||
37f5aefd RJ |
389 | /** |
390 | * pxa_init_dma_channel - init dma descriptors | |
391 | * @pcdev: pxa camera device | |
e009ebd5 | 392 | * @vb: videobuffer2 buffer |
37f5aefd RJ |
393 | * @dma: dma video buffer |
394 | * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V') | |
395 | * @cibr: camera Receive Buffer Register | |
37f5aefd RJ |
396 | * |
397 | * Prepares the pxa dma descriptors to transfer one camera channel. | |
37f5aefd | 398 | * |
1e77d55a | 399 | * Returns 0 if success or -ENOMEM if no memory is available |
37f5aefd | 400 | */ |
a5462e5b | 401 | static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev, |
e009ebd5 RJ |
402 | struct pxa_buffer *buf, int channel, |
403 | struct scatterlist *sg, int sglen) | |
a5462e5b | 404 | { |
1e77d55a | 405 | struct dma_chan *dma_chan = pcdev->dma_chans[channel]; |
1e77d55a RJ |
406 | struct dma_async_tx_descriptor *tx; |
407 | ||
408 | tx = dmaengine_prep_slave_sg(dma_chan, sg, sglen, DMA_DEV_TO_MEM, | |
409 | DMA_PREP_INTERRUPT | DMA_CTRL_REUSE); | |
410 | if (!tx) { | |
e009ebd5 | 411 | dev_err(pcdev_to_dev(pcdev), |
1e77d55a RJ |
412 | "dmaengine_prep_slave_sg failed\n"); |
413 | goto fail; | |
a5462e5b MR |
414 | } |
415 | ||
1e77d55a RJ |
416 | tx->callback_param = pcdev; |
417 | switch (channel) { | |
418 | case 0: | |
419 | tx->callback = pxa_camera_dma_irq_y; | |
420 | break; | |
421 | case 1: | |
422 | tx->callback = pxa_camera_dma_irq_u; | |
423 | break; | |
424 | case 2: | |
425 | tx->callback = pxa_camera_dma_irq_v; | |
426 | break; | |
37f5aefd RJ |
427 | } |
428 | ||
1e77d55a | 429 | buf->descs[channel] = tx; |
a5462e5b | 430 | return 0; |
1e77d55a | 431 | fail: |
e009ebd5 RJ |
432 | dev_dbg(pcdev_to_dev(pcdev), |
433 | "%s (vb=%p) dma_tx=%p\n", | |
434 | __func__, buf, tx); | |
1e77d55a RJ |
435 | |
436 | return -ENOMEM; | |
a5462e5b MR |
437 | } |
438 | ||
256b0233 RJ |
439 | static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev, |
440 | struct pxa_buffer *buf) | |
441 | { | |
442 | buf->active_dma = DMA_Y; | |
283e4a82 | 443 | if (buf->nb_planes == 3) |
256b0233 RJ |
444 | buf->active_dma |= DMA_U | DMA_V; |
445 | } | |
446 | ||
256b0233 RJ |
447 | /** |
448 | * pxa_dma_start_channels - start DMA channel for active buffer | |
449 | * @pcdev: pxa camera device | |
450 | * | |
451 | * Initialize DMA channels to the beginning of the active video buffer, and | |
452 | * start these channels. | |
453 | */ | |
454 | static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev) | |
455 | { | |
456 | int i; | |
457 | struct pxa_buffer *active; | |
458 | ||
459 | active = pcdev->active; | |
460 | ||
461 | for (i = 0; i < pcdev->channels; i++) { | |
e009ebd5 | 462 | dev_dbg(pcdev_to_dev(pcdev), |
1e77d55a RJ |
463 | "%s (channel=%d)\n", __func__, i); |
464 | dma_async_issue_pending(pcdev->dma_chans[i]); | |
256b0233 RJ |
465 | } |
466 | } | |
467 | ||
468 | static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev) | |
469 | { | |
470 | int i; | |
471 | ||
472 | for (i = 0; i < pcdev->channels; i++) { | |
e009ebd5 | 473 | dev_dbg(pcdev_to_dev(pcdev), |
0166b743 | 474 | "%s (channel=%d)\n", __func__, i); |
1e77d55a | 475 | dmaengine_terminate_all(pcdev->dma_chans[i]); |
256b0233 RJ |
476 | } |
477 | } | |
478 | ||
256b0233 RJ |
479 | static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev, |
480 | struct pxa_buffer *buf) | |
481 | { | |
482 | int i; | |
256b0233 RJ |
483 | |
484 | for (i = 0; i < pcdev->channels; i++) { | |
1e77d55a | 485 | buf->cookie[i] = dmaengine_submit(buf->descs[i]); |
e009ebd5 | 486 | dev_dbg(pcdev_to_dev(pcdev), |
1e77d55a RJ |
487 | "%s (channel=%d) : submit vb=%p cookie=%d\n", |
488 | __func__, i, buf, buf->descs[i]->cookie); | |
ae7410e7 | 489 | } |
256b0233 RJ |
490 | } |
491 | ||
492 | /** | |
493 | * pxa_camera_start_capture - start video capturing | |
494 | * @pcdev: camera device | |
495 | * | |
496 | * Launch capturing. DMA channels should not be active yet. They should get | |
497 | * activated at the end of frame interrupt, to capture only whole frames, and | |
498 | * never begin the capture of a partial frame. | |
499 | */ | |
500 | static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev) | |
501 | { | |
a47f6be4 | 502 | unsigned long cicr0; |
256b0233 | 503 | |
e009ebd5 | 504 | dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__); |
e623ebe6 | 505 | __raw_writel(__raw_readl(pcdev->base + CISR), pcdev->base + CISR); |
256b0233 RJ |
506 | /* Enable End-Of-Frame Interrupt */ |
507 | cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB; | |
508 | cicr0 &= ~CICR0_EOFM; | |
509 | __raw_writel(cicr0, pcdev->base + CICR0); | |
510 | } | |
511 | ||
512 | static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev) | |
513 | { | |
514 | unsigned long cicr0; | |
515 | ||
516 | pxa_dma_stop_channels(pcdev); | |
517 | ||
518 | cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB; | |
519 | __raw_writel(cicr0, pcdev->base + CICR0); | |
520 | ||
8c62e221 | 521 | pcdev->active = NULL; |
e009ebd5 | 522 | dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__); |
3bc43840 GL |
523 | } |
524 | ||
a5462e5b | 525 | static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev, |
fcdf9bbc RJ |
526 | struct pxa_buffer *buf, |
527 | enum vb2_buffer_state state) | |
a5462e5b | 528 | { |
e009ebd5 | 529 | struct vb2_buffer *vb = &buf->vbuf.vb2_buf; |
61634976 | 530 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
e009ebd5 | 531 | |
a5462e5b | 532 | /* _init is used to debug races, see comment in pxa_camera_reqbufs() */ |
e009ebd5 RJ |
533 | list_del_init(&buf->queue); |
534 | vb->timestamp = ktime_get_ns(); | |
61634976 RJ |
535 | vbuf->sequence = pcdev->buf_sequence++; |
536 | vbuf->field = V4L2_FIELD_NONE; | |
e009ebd5 RJ |
537 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
538 | dev_dbg(pcdev_to_dev(pcdev), "%s dequeud buffer (buf=0x%p)\n", | |
539 | __func__, buf); | |
a5462e5b MR |
540 | |
541 | if (list_empty(&pcdev->capture)) { | |
256b0233 | 542 | pxa_camera_stop_capture(pcdev); |
a5462e5b MR |
543 | return; |
544 | } | |
545 | ||
546 | pcdev->active = list_entry(pcdev->capture.next, | |
e009ebd5 | 547 | struct pxa_buffer, queue); |
a5462e5b MR |
548 | } |
549 | ||
256b0233 RJ |
550 | /** |
551 | * pxa_camera_check_link_miss - check missed DMA linking | |
552 | * @pcdev: camera device | |
553 | * | |
554 | * The DMA chaining is done with DMA running. This means a tiny temporal window | |
555 | * remains, where a buffer is queued on the chain, while the chain is already | |
25985edc | 556 | * stopped. This means the tailed buffer would never be transferred by DMA. |
256b0233 RJ |
557 | * This function restarts the capture for this corner case, where : |
558 | * - DADR() == DADDR_STOP | |
559 | * - a videobuffer is queued on the pcdev->capture list | |
560 | * | |
561 | * Please check the "DMA hot chaining timeslice issue" in | |
562 | * Documentation/video4linux/pxa_camera.txt | |
563 | * | |
564 | * Context: should only be called within the dma irq handler | |
565 | */ | |
1e77d55a RJ |
566 | static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev, |
567 | dma_cookie_t last_submitted, | |
568 | dma_cookie_t last_issued) | |
256b0233 | 569 | { |
1e77d55a | 570 | bool is_dma_stopped = last_submitted != last_issued; |
256b0233 | 571 | |
e009ebd5 | 572 | dev_dbg(pcdev_to_dev(pcdev), |
1e77d55a | 573 | "%s : top queued buffer=%p, is_dma_stopped=%d\n", |
256b0233 | 574 | __func__, pcdev->active, is_dma_stopped); |
1e77d55a | 575 | |
256b0233 RJ |
576 | if (pcdev->active && is_dma_stopped) |
577 | pxa_camera_start_capture(pcdev); | |
578 | } | |
579 | ||
1e77d55a | 580 | static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev, |
a5462e5b | 581 | enum pxa_camera_active_dma act_dma) |
3bc43840 | 582 | { |
1e77d55a | 583 | struct pxa_buffer *buf, *last_buf; |
3bc43840 | 584 | unsigned long flags; |
1e77d55a RJ |
585 | u32 camera_status, overrun; |
586 | int chan; | |
1e77d55a RJ |
587 | enum dma_status last_status; |
588 | dma_cookie_t last_issued; | |
3bc43840 GL |
589 | |
590 | spin_lock_irqsave(&pcdev->lock, flags); | |
591 | ||
256b0233 | 592 | camera_status = __raw_readl(pcdev->base + CISR); |
295ab497 | 593 | dev_dbg(pcdev_to_dev(pcdev), "camera dma irq, cisr=0x%x dma=%d\n", |
1e77d55a | 594 | camera_status, act_dma); |
256b0233 RJ |
595 | overrun = CISR_IFO_0; |
596 | if (pcdev->channels == 3) | |
597 | overrun |= CISR_IFO_1 | CISR_IFO_2; | |
7102b773 | 598 | |
8c62e221 RJ |
599 | /* |
600 | * pcdev->active should not be NULL in DMA irq handler. | |
601 | * | |
602 | * But there is one corner case : if capture was stopped due to an | |
603 | * overrun of channel 1, and at that same channel 2 was completed. | |
604 | * | |
605 | * When handling the overrun in DMA irq for channel 1, we'll stop the | |
606 | * capture and restart it (and thus set pcdev->active to NULL). But the | |
607 | * DMA irq handler will already be pending for channel 2. So on entering | |
608 | * the DMA irq handler for channel 2 there will be no active buffer, yet | |
609 | * that is normal. | |
610 | */ | |
611 | if (!pcdev->active) | |
3bc43840 | 612 | goto out; |
3bc43840 | 613 | |
e009ebd5 RJ |
614 | buf = pcdev->active; |
615 | WARN_ON(buf->inwork || list_empty(&buf->queue)); | |
3bc43840 | 616 | |
1e77d55a RJ |
617 | /* |
618 | * It's normal if the last frame creates an overrun, as there | |
619 | * are no more DMA descriptors to fetch from QCI fifos | |
620 | */ | |
621 | switch (act_dma) { | |
622 | case DMA_U: | |
623 | chan = 1; | |
624 | break; | |
625 | case DMA_V: | |
626 | chan = 2; | |
627 | break; | |
628 | default: | |
629 | chan = 0; | |
630 | break; | |
631 | } | |
632 | last_buf = list_entry(pcdev->capture.prev, | |
e009ebd5 | 633 | struct pxa_buffer, queue); |
1e77d55a RJ |
634 | last_status = dma_async_is_tx_complete(pcdev->dma_chans[chan], |
635 | last_buf->cookie[chan], | |
636 | NULL, &last_issued); | |
637 | if (camera_status & overrun && | |
638 | last_status != DMA_COMPLETE) { | |
295ab497 | 639 | dev_dbg(pcdev_to_dev(pcdev), "FIFO overrun! CISR: %x\n", |
1e77d55a RJ |
640 | camera_status); |
641 | pxa_camera_stop_capture(pcdev); | |
e009ebd5 | 642 | list_for_each_entry(buf, &pcdev->capture, queue) |
1e77d55a RJ |
643 | pxa_dma_add_tail_buf(pcdev, buf); |
644 | pxa_camera_start_capture(pcdev); | |
645 | goto out; | |
646 | } | |
647 | buf->active_dma &= ~act_dma; | |
648 | if (!buf->active_dma) { | |
fcdf9bbc | 649 | pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_DONE); |
1e77d55a RJ |
650 | pxa_camera_check_link_miss(pcdev, last_buf->cookie[chan], |
651 | last_issued); | |
256b0233 | 652 | } |
3bc43840 GL |
653 | |
654 | out: | |
655 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
656 | } | |
657 | ||
40e2e092 GL |
658 | static u32 mclk_get_divisor(struct platform_device *pdev, |
659 | struct pxa_camera_dev *pcdev) | |
3bc43840 | 660 | { |
cf34cba7 GL |
661 | unsigned long mclk = pcdev->mclk; |
662 | u32 div; | |
3bc43840 GL |
663 | unsigned long lcdclk; |
664 | ||
cf34cba7 GL |
665 | lcdclk = clk_get_rate(pcdev->clk); |
666 | pcdev->ciclk = lcdclk; | |
3bc43840 | 667 | |
cf34cba7 GL |
668 | /* mclk <= ciclk / 4 (27.4.2) */ |
669 | if (mclk > lcdclk / 4) { | |
670 | mclk = lcdclk / 4; | |
295ab497 RJ |
671 | dev_warn(pcdev_to_dev(pcdev), |
672 | "Limiting master clock to %lu\n", mclk); | |
cf34cba7 GL |
673 | } |
674 | ||
675 | /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */ | |
676 | div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1; | |
3bc43840 | 677 | |
cf34cba7 GL |
678 | /* If we're not supplying MCLK, leave it at 0 */ |
679 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
680 | pcdev->mclk = lcdclk / (2 * (div + 1)); | |
3bc43840 | 681 | |
295ab497 | 682 | dev_dbg(pcdev_to_dev(pcdev), "LCD clock %luHz, target freq %luHz, divisor %u\n", |
40e2e092 | 683 | lcdclk, mclk, div); |
3bc43840 GL |
684 | |
685 | return div; | |
686 | } | |
687 | ||
cf34cba7 GL |
688 | static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev, |
689 | unsigned long pclk) | |
690 | { | |
691 | /* We want a timeout > 1 pixel time, not ">=" */ | |
692 | u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1; | |
693 | ||
694 | __raw_writel(ciclk_per_pixel, pcdev->base + CITOR); | |
695 | } | |
696 | ||
7102b773 | 697 | static void pxa_camera_activate(struct pxa_camera_dev *pcdev) |
3bc43840 | 698 | { |
3bc43840 GL |
699 | u32 cicr4 = 0; |
700 | ||
5ca11fa3 EM |
701 | /* disable all interrupts */ |
702 | __raw_writel(0x3ff, pcdev->base + CICR0); | |
3bc43840 GL |
703 | |
704 | if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN) | |
705 | cicr4 |= CICR4_PCLK_EN; | |
706 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
707 | cicr4 |= CICR4_MCLK_EN; | |
708 | if (pcdev->platform_flags & PXA_CAMERA_PCP) | |
709 | cicr4 |= CICR4_PCP; | |
710 | if (pcdev->platform_flags & PXA_CAMERA_HSP) | |
711 | cicr4 |= CICR4_HSP; | |
712 | if (pcdev->platform_flags & PXA_CAMERA_VSP) | |
713 | cicr4 |= CICR4_VSP; | |
714 | ||
cf34cba7 GL |
715 | __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4); |
716 | ||
717 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
718 | /* Initialise the timeout under the assumption pclk = mclk */ | |
719 | recalculate_fifo_timeout(pcdev, pcdev->mclk); | |
720 | else | |
721 | /* "Safe default" - 13MHz */ | |
722 | recalculate_fifo_timeout(pcdev, 13000000); | |
3bc43840 | 723 | |
91acd962 | 724 | clk_prepare_enable(pcdev->clk); |
3bc43840 GL |
725 | } |
726 | ||
7102b773 | 727 | static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev) |
3bc43840 | 728 | { |
91acd962 | 729 | clk_disable_unprepare(pcdev->clk); |
3bc43840 GL |
730 | } |
731 | ||
e623ebe6 | 732 | static void pxa_camera_eof(unsigned long arg) |
3bc43840 | 733 | { |
e623ebe6 RJ |
734 | struct pxa_camera_dev *pcdev = (struct pxa_camera_dev *)arg; |
735 | unsigned long cifr; | |
256b0233 | 736 | struct pxa_buffer *buf; |
3bc43840 | 737 | |
e009ebd5 | 738 | dev_dbg(pcdev_to_dev(pcdev), |
e623ebe6 RJ |
739 | "Camera interrupt status 0x%x\n", |
740 | __raw_readl(pcdev->base + CISR)); | |
741 | ||
742 | /* Reset the FIFOs */ | |
743 | cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F; | |
744 | __raw_writel(cifr, pcdev->base + CIFR); | |
745 | ||
746 | pcdev->active = list_first_entry(&pcdev->capture, | |
e009ebd5 RJ |
747 | struct pxa_buffer, queue); |
748 | buf = pcdev->active; | |
e623ebe6 RJ |
749 | pxa_videobuf_set_actdma(pcdev, buf); |
750 | ||
751 | pxa_dma_start_channels(pcdev); | |
752 | } | |
753 | ||
754 | static irqreturn_t pxa_camera_irq(int irq, void *data) | |
755 | { | |
756 | struct pxa_camera_dev *pcdev = data; | |
757 | unsigned long status, cicr0; | |
758 | ||
5ca11fa3 | 759 | status = __raw_readl(pcdev->base + CISR); |
e009ebd5 | 760 | dev_dbg(pcdev_to_dev(pcdev), |
0166b743 | 761 | "Camera interrupt status 0x%lx\n", status); |
3bc43840 | 762 | |
e7c50688 GL |
763 | if (!status) |
764 | return IRQ_NONE; | |
765 | ||
5ca11fa3 | 766 | __raw_writel(status, pcdev->base + CISR); |
e7c50688 GL |
767 | |
768 | if (status & CISR_EOF) { | |
5ca11fa3 EM |
769 | cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM; |
770 | __raw_writel(cicr0, pcdev->base + CICR0); | |
e623ebe6 | 771 | tasklet_schedule(&pcdev->task_eof); |
e7c50688 GL |
772 | } |
773 | ||
3bc43840 GL |
774 | return IRQ_HANDLED; |
775 | } | |
776 | ||
ad5f2e85 GL |
777 | static int test_platform_param(struct pxa_camera_dev *pcdev, |
778 | unsigned char buswidth, unsigned long *flags) | |
3bc43840 | 779 | { |
ad5f2e85 GL |
780 | /* |
781 | * Platform specified synchronization and pixel clock polarities are | |
782 | * only a recommendation and are only used during probing. The PXA270 | |
783 | * quick capture interface supports both. | |
784 | */ | |
785 | *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ? | |
679419aa GL |
786 | V4L2_MBUS_MASTER : V4L2_MBUS_SLAVE) | |
787 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | | |
788 | V4L2_MBUS_HSYNC_ACTIVE_LOW | | |
789 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | | |
790 | V4L2_MBUS_VSYNC_ACTIVE_LOW | | |
791 | V4L2_MBUS_DATA_ACTIVE_HIGH | | |
792 | V4L2_MBUS_PCLK_SAMPLE_RISING | | |
793 | V4L2_MBUS_PCLK_SAMPLE_FALLING; | |
3bc43840 GL |
794 | |
795 | /* If requested data width is supported by the platform, use it */ | |
679419aa GL |
796 | if ((1 << (buswidth - 1)) & pcdev->width_flags) |
797 | return 0; | |
ad5f2e85 | 798 | |
679419aa | 799 | return -EINVAL; |
ad5f2e85 GL |
800 | } |
801 | ||
283e4a82 | 802 | static void pxa_camera_setup_cicr(struct pxa_camera_dev *pcdev, |
6a6c8786 | 803 | unsigned long flags, __u32 pixfmt) |
ad5f2e85 | 804 | { |
6a6c8786 | 805 | unsigned long dw, bpp; |
32536108 | 806 | u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0, y_skip_top; |
b36bcbd6 | 807 | int ret = sensor_call(pcdev, sensor, g_skip_top_lines, &y_skip_top); |
32536108 GL |
808 | |
809 | if (ret < 0) | |
810 | y_skip_top = 0; | |
3bc43840 | 811 | |
5d28d525 GL |
812 | /* |
813 | * Datawidth is now guaranteed to be equal to one of the three values. | |
814 | * We fix bit-per-pixel equal to data-width... | |
815 | */ | |
283e4a82 | 816 | switch (pcdev->current_fmt->host_fmt->bits_per_sample) { |
679419aa | 817 | case 10: |
3bc43840 GL |
818 | dw = 4; |
819 | bpp = 0x40; | |
820 | break; | |
679419aa | 821 | case 9: |
3bc43840 GL |
822 | dw = 3; |
823 | bpp = 0x20; | |
824 | break; | |
825 | default: | |
5d28d525 GL |
826 | /* |
827 | * Actually it can only be 8 now, | |
828 | * default is just to silence compiler warnings | |
829 | */ | |
679419aa | 830 | case 8: |
3bc43840 GL |
831 | dw = 2; |
832 | bpp = 0; | |
833 | } | |
834 | ||
835 | if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN) | |
836 | cicr4 |= CICR4_PCLK_EN; | |
837 | if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN) | |
838 | cicr4 |= CICR4_MCLK_EN; | |
679419aa | 839 | if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING) |
3bc43840 | 840 | cicr4 |= CICR4_PCP; |
679419aa | 841 | if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW) |
3bc43840 | 842 | cicr4 |= CICR4_HSP; |
679419aa | 843 | if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW) |
3bc43840 GL |
844 | cicr4 |= CICR4_VSP; |
845 | ||
5ca11fa3 | 846 | cicr0 = __raw_readl(pcdev->base + CICR0); |
3bc43840 | 847 | if (cicr0 & CICR0_ENB) |
5ca11fa3 | 848 | __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0); |
a5462e5b | 849 | |
283e4a82 | 850 | cicr1 = CICR1_PPL_VAL(pcdev->current_pix.width - 1) | bpp | dw; |
a5462e5b MR |
851 | |
852 | switch (pixfmt) { | |
853 | case V4L2_PIX_FMT_YUV422P: | |
e7c50688 | 854 | pcdev->channels = 3; |
a5462e5b | 855 | cicr1 |= CICR1_YCBCR_F; |
2a48fc73 RJ |
856 | /* |
857 | * Normally, pxa bus wants as input UYVY format. We allow all | |
858 | * reorderings of the YUV422 format, as no processing is done, | |
859 | * and the YUV stream is just passed through without any | |
860 | * transformation. Note that UYVY is the only format that | |
861 | * should be used if pxa framebuffer Overlay2 is used. | |
862 | */ | |
863 | case V4L2_PIX_FMT_UYVY: | |
864 | case V4L2_PIX_FMT_VYUY: | |
a5462e5b | 865 | case V4L2_PIX_FMT_YUYV: |
2a48fc73 | 866 | case V4L2_PIX_FMT_YVYU: |
a5462e5b MR |
867 | cicr1 |= CICR1_COLOR_SP_VAL(2); |
868 | break; | |
869 | case V4L2_PIX_FMT_RGB555: | |
870 | cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) | | |
871 | CICR1_TBIT | CICR1_COLOR_SP_VAL(1); | |
872 | break; | |
873 | case V4L2_PIX_FMT_RGB565: | |
874 | cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2); | |
875 | break; | |
876 | } | |
877 | ||
5ca11fa3 | 878 | cicr2 = 0; |
283e4a82 | 879 | cicr3 = CICR3_LPF_VAL(pcdev->current_pix.height - 1) | |
32536108 | 880 | CICR3_BFW_VAL(min((u32)255, y_skip_top)); |
cf34cba7 | 881 | cicr4 |= pcdev->mclk_divisor; |
5ca11fa3 EM |
882 | |
883 | __raw_writel(cicr1, pcdev->base + CICR1); | |
884 | __raw_writel(cicr2, pcdev->base + CICR2); | |
885 | __raw_writel(cicr3, pcdev->base + CICR3); | |
886 | __raw_writel(cicr4, pcdev->base + CICR4); | |
3bc43840 GL |
887 | |
888 | /* CIF interrupts are not used, only DMA */ | |
5ca11fa3 EM |
889 | cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ? |
890 | CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)); | |
891 | cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK; | |
892 | __raw_writel(cicr0, pcdev->base + CICR0); | |
6a6c8786 GL |
893 | } |
894 | ||
6f28435d RJ |
895 | /* |
896 | * Videobuf2 section | |
897 | */ | |
898 | static void pxa_buffer_cleanup(struct pxa_buffer *buf) | |
899 | { | |
900 | int i; | |
901 | ||
902 | for (i = 0; i < 3 && buf->descs[i]; i++) { | |
903 | dmaengine_desc_free(buf->descs[i]); | |
904 | kfree(buf->sg[i]); | |
905 | buf->descs[i] = NULL; | |
906 | buf->sg[i] = NULL; | |
907 | buf->sg_len[i] = 0; | |
908 | buf->plane_sizes[i] = 0; | |
909 | } | |
910 | buf->nb_planes = 0; | |
911 | } | |
912 | ||
913 | static int pxa_buffer_init(struct pxa_camera_dev *pcdev, | |
914 | struct pxa_buffer *buf) | |
915 | { | |
916 | struct vb2_buffer *vb = &buf->vbuf.vb2_buf; | |
917 | struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0); | |
918 | int nb_channels = pcdev->channels; | |
919 | int i, ret = 0; | |
920 | unsigned long size = vb2_plane_size(vb, 0); | |
921 | ||
922 | switch (nb_channels) { | |
923 | case 1: | |
924 | buf->plane_sizes[0] = size; | |
925 | break; | |
926 | case 3: | |
927 | buf->plane_sizes[0] = size / 2; | |
928 | buf->plane_sizes[1] = size / 4; | |
929 | buf->plane_sizes[2] = size / 4; | |
930 | break; | |
931 | default: | |
932 | return -EINVAL; | |
933 | }; | |
934 | buf->nb_planes = nb_channels; | |
935 | ||
936 | ret = sg_split(sgt->sgl, sgt->nents, 0, nb_channels, | |
937 | buf->plane_sizes, buf->sg, buf->sg_len, GFP_KERNEL); | |
938 | if (ret < 0) { | |
939 | dev_err(pcdev_to_dev(pcdev), | |
940 | "sg_split failed: %d\n", ret); | |
941 | return ret; | |
942 | } | |
943 | for (i = 0; i < nb_channels; i++) { | |
944 | ret = pxa_init_dma_channel(pcdev, buf, i, | |
945 | buf->sg[i], buf->sg_len[i]); | |
946 | if (ret) { | |
947 | pxa_buffer_cleanup(buf); | |
948 | return ret; | |
949 | } | |
950 | } | |
951 | INIT_LIST_HEAD(&buf->queue); | |
952 | ||
953 | return ret; | |
954 | } | |
955 | ||
956 | static void pxac_vb2_cleanup(struct vb2_buffer *vb) | |
957 | { | |
958 | struct pxa_buffer *buf = vb2_to_pxa_buffer(vb); | |
959 | struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue); | |
960 | ||
961 | dev_dbg(pcdev_to_dev(pcdev), | |
962 | "%s(vb=%p)\n", __func__, vb); | |
963 | pxa_buffer_cleanup(buf); | |
964 | } | |
965 | ||
966 | static void pxac_vb2_queue(struct vb2_buffer *vb) | |
967 | { | |
968 | struct pxa_buffer *buf = vb2_to_pxa_buffer(vb); | |
969 | struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue); | |
970 | ||
971 | dev_dbg(pcdev_to_dev(pcdev), | |
972 | "%s(vb=%p) nb_channels=%d size=%lu active=%p\n", | |
973 | __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0), | |
974 | pcdev->active); | |
975 | ||
976 | list_add_tail(&buf->queue, &pcdev->capture); | |
977 | ||
978 | pxa_dma_add_tail_buf(pcdev, buf); | |
979 | } | |
980 | ||
981 | /* | |
982 | * Please check the DMA prepared buffer structure in : | |
983 | * Documentation/video4linux/pxa_camera.txt | |
984 | * Please check also in pxa_camera_check_link_miss() to understand why DMA chain | |
985 | * modification while DMA chain is running will work anyway. | |
986 | */ | |
987 | static int pxac_vb2_prepare(struct vb2_buffer *vb) | |
988 | { | |
989 | struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue); | |
990 | struct pxa_buffer *buf = vb2_to_pxa_buffer(vb); | |
6f28435d RJ |
991 | int ret = 0; |
992 | ||
993 | switch (pcdev->channels) { | |
994 | case 1: | |
995 | case 3: | |
283e4a82 | 996 | vb2_set_plane_payload(vb, 0, pcdev->current_pix.sizeimage); |
6f28435d RJ |
997 | break; |
998 | default: | |
999 | return -EINVAL; | |
1000 | } | |
1001 | ||
1002 | dev_dbg(pcdev_to_dev(pcdev), | |
1003 | "%s (vb=%p) nb_channels=%d size=%lu\n", | |
1004 | __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0)); | |
1005 | ||
283e4a82 | 1006 | WARN_ON(!pcdev->current_fmt); |
6f28435d RJ |
1007 | |
1008 | #ifdef DEBUG | |
1009 | /* | |
1010 | * This can be useful if you want to see if we actually fill | |
1011 | * the buffer with something | |
1012 | */ | |
1013 | for (i = 0; i < vb->num_planes; i++) | |
1014 | memset((void *)vb2_plane_vaddr(vb, i), | |
1015 | 0xaa, vb2_get_plane_payload(vb, i)); | |
1016 | #endif | |
1017 | ||
1018 | /* | |
1019 | * I think, in buf_prepare you only have to protect global data, | |
1020 | * the actual buffer is yours | |
1021 | */ | |
1022 | buf->inwork = 0; | |
1023 | pxa_videobuf_set_actdma(pcdev, buf); | |
1024 | ||
1025 | return ret; | |
1026 | } | |
1027 | ||
1028 | static int pxac_vb2_init(struct vb2_buffer *vb) | |
1029 | { | |
1030 | struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue); | |
1031 | struct pxa_buffer *buf = vb2_to_pxa_buffer(vb); | |
1032 | ||
1033 | dev_dbg(pcdev_to_dev(pcdev), | |
1034 | "%s(nb_channels=%d)\n", | |
1035 | __func__, pcdev->channels); | |
1036 | ||
1037 | return pxa_buffer_init(pcdev, buf); | |
1038 | } | |
1039 | ||
1040 | static int pxac_vb2_queue_setup(struct vb2_queue *vq, | |
1041 | unsigned int *nbufs, | |
1042 | unsigned int *num_planes, unsigned int sizes[], | |
1043 | struct device *alloc_devs[]) | |
1044 | { | |
1045 | struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq); | |
283e4a82 | 1046 | int size = pcdev->current_pix.sizeimage; |
6f28435d RJ |
1047 | |
1048 | dev_dbg(pcdev_to_dev(pcdev), | |
1049 | "%s(vq=%p nbufs=%d num_planes=%d size=%d)\n", | |
1050 | __func__, vq, *nbufs, *num_planes, size); | |
1051 | /* | |
1052 | * Called from VIDIOC_REQBUFS or in compatibility mode For YUV422P | |
1053 | * format, even if there are 3 planes Y, U and V, we reply there is only | |
1054 | * one plane, containing Y, U and V data, one after the other. | |
1055 | */ | |
1056 | if (*num_planes) | |
1057 | return sizes[0] < size ? -EINVAL : 0; | |
1058 | ||
1059 | *num_planes = 1; | |
1060 | switch (pcdev->channels) { | |
1061 | case 1: | |
1062 | case 3: | |
1063 | sizes[0] = size; | |
1064 | break; | |
1065 | default: | |
1066 | return -EINVAL; | |
1067 | } | |
1068 | ||
1069 | if (!*nbufs) | |
1070 | *nbufs = 1; | |
1071 | ||
1072 | return 0; | |
1073 | } | |
1074 | ||
1075 | static int pxac_vb2_start_streaming(struct vb2_queue *vq, unsigned int count) | |
1076 | { | |
1077 | struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq); | |
1078 | ||
1079 | dev_dbg(pcdev_to_dev(pcdev), "%s(count=%d) active=%p\n", | |
1080 | __func__, count, pcdev->active); | |
1081 | ||
61634976 | 1082 | pcdev->buf_sequence = 0; |
6f28435d RJ |
1083 | if (!pcdev->active) |
1084 | pxa_camera_start_capture(pcdev); | |
1085 | ||
1086 | return 0; | |
1087 | } | |
1088 | ||
1089 | static void pxac_vb2_stop_streaming(struct vb2_queue *vq) | |
1090 | { | |
fcdf9bbc RJ |
1091 | struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq); |
1092 | struct pxa_buffer *buf, *tmp; | |
1093 | ||
1094 | dev_dbg(pcdev_to_dev(pcdev), "%s active=%p\n", | |
1095 | __func__, pcdev->active); | |
1096 | pxa_camera_stop_capture(pcdev); | |
1097 | ||
1098 | list_for_each_entry_safe(buf, tmp, &pcdev->capture, queue) | |
1099 | pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_ERROR); | |
6f28435d RJ |
1100 | } |
1101 | ||
1102 | static struct vb2_ops pxac_vb2_ops = { | |
1103 | .queue_setup = pxac_vb2_queue_setup, | |
1104 | .buf_init = pxac_vb2_init, | |
1105 | .buf_prepare = pxac_vb2_prepare, | |
1106 | .buf_queue = pxac_vb2_queue, | |
1107 | .buf_cleanup = pxac_vb2_cleanup, | |
1108 | .start_streaming = pxac_vb2_start_streaming, | |
1109 | .stop_streaming = pxac_vb2_stop_streaming, | |
1110 | .wait_prepare = vb2_ops_wait_prepare, | |
1111 | .wait_finish = vb2_ops_wait_finish, | |
1112 | }; | |
1113 | ||
283e4a82 | 1114 | static int pxa_camera_init_videobuf2(struct pxa_camera_dev *pcdev) |
6f28435d | 1115 | { |
6f28435d | 1116 | int ret; |
283e4a82 | 1117 | struct vb2_queue *vq = &pcdev->vb2_vq; |
6f28435d | 1118 | |
283e4a82 | 1119 | memset(vq, 0, sizeof(*vq)); |
6f28435d RJ |
1120 | vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
1121 | vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; | |
1122 | vq->drv_priv = pcdev; | |
1123 | vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
1124 | vq->buf_struct_size = sizeof(struct pxa_buffer); | |
283e4a82 | 1125 | vq->dev = pcdev->v4l2_dev.dev; |
6f28435d RJ |
1126 | |
1127 | vq->ops = &pxac_vb2_ops; | |
1128 | vq->mem_ops = &vb2_dma_sg_memops; | |
283e4a82 | 1129 | vq->lock = &pcdev->mlock; |
6f28435d RJ |
1130 | |
1131 | ret = vb2_queue_init(vq); | |
1132 | dev_dbg(pcdev_to_dev(pcdev), | |
1133 | "vb2_queue_init(vq=%p): %d\n", vq, ret); | |
1134 | ||
1135 | return ret; | |
1136 | } | |
1137 | ||
1138 | /* | |
1139 | * Video ioctls section | |
1140 | */ | |
283e4a82 | 1141 | static int pxa_camera_set_bus_param(struct pxa_camera_dev *pcdev) |
6a6c8786 | 1142 | { |
679419aa | 1143 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
283e4a82 | 1144 | u32 pixfmt = pcdev->current_fmt->host_fmt->fourcc; |
679419aa | 1145 | unsigned long bus_flags, common_flags; |
760697be | 1146 | int ret; |
6a6c8786 | 1147 | |
283e4a82 RJ |
1148 | ret = test_platform_param(pcdev, |
1149 | pcdev->current_fmt->host_fmt->bits_per_sample, | |
d2dcad49 | 1150 | &bus_flags); |
6a6c8786 GL |
1151 | if (ret < 0) |
1152 | return ret; | |
1153 | ||
b36bcbd6 | 1154 | ret = sensor_call(pcdev, video, g_mbus_config, &cfg); |
679419aa GL |
1155 | if (!ret) { |
1156 | common_flags = soc_mbus_config_compatible(&cfg, | |
1157 | bus_flags); | |
1158 | if (!common_flags) { | |
295ab497 | 1159 | dev_warn(pcdev_to_dev(pcdev), |
679419aa GL |
1160 | "Flags incompatible: camera 0x%x, host 0x%lx\n", |
1161 | cfg.flags, bus_flags); | |
1162 | return -EINVAL; | |
1163 | } | |
1164 | } else if (ret != -ENOIOCTLCMD) { | |
1165 | return ret; | |
1166 | } else { | |
1167 | common_flags = bus_flags; | |
1168 | } | |
6a6c8786 GL |
1169 | |
1170 | pcdev->channels = 1; | |
1171 | ||
1172 | /* Make choises, based on platform preferences */ | |
679419aa GL |
1173 | if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && |
1174 | (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { | |
6a6c8786 | 1175 | if (pcdev->platform_flags & PXA_CAMERA_HSP) |
679419aa | 1176 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; |
6a6c8786 | 1177 | else |
679419aa | 1178 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; |
6a6c8786 GL |
1179 | } |
1180 | ||
679419aa GL |
1181 | if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) && |
1182 | (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) { | |
6a6c8786 | 1183 | if (pcdev->platform_flags & PXA_CAMERA_VSP) |
679419aa | 1184 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH; |
6a6c8786 | 1185 | else |
679419aa | 1186 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW; |
6a6c8786 GL |
1187 | } |
1188 | ||
679419aa GL |
1189 | if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) && |
1190 | (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) { | |
6a6c8786 | 1191 | if (pcdev->platform_flags & PXA_CAMERA_PCP) |
679419aa | 1192 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING; |
6a6c8786 | 1193 | else |
679419aa | 1194 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING; |
6a6c8786 GL |
1195 | } |
1196 | ||
679419aa | 1197 | cfg.flags = common_flags; |
b36bcbd6 | 1198 | ret = sensor_call(pcdev, video, s_mbus_config, &cfg); |
679419aa | 1199 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
283e4a82 RJ |
1200 | dev_dbg(pcdev_to_dev(pcdev), |
1201 | "camera s_mbus_config(0x%lx) returned %d\n", | |
679419aa | 1202 | common_flags, ret); |
6a6c8786 | 1203 | return ret; |
679419aa GL |
1204 | } |
1205 | ||
283e4a82 | 1206 | pxa_camera_setup_cicr(pcdev, common_flags, pixfmt); |
3bc43840 GL |
1207 | |
1208 | return 0; | |
1209 | } | |
1210 | ||
283e4a82 | 1211 | static int pxa_camera_try_bus_param(struct pxa_camera_dev *pcdev, |
2a48fc73 | 1212 | unsigned char buswidth) |
ad5f2e85 | 1213 | { |
679419aa GL |
1214 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
1215 | unsigned long bus_flags, common_flags; | |
2a48fc73 | 1216 | int ret = test_platform_param(pcdev, buswidth, &bus_flags); |
ad5f2e85 GL |
1217 | |
1218 | if (ret < 0) | |
1219 | return ret; | |
1220 | ||
b36bcbd6 | 1221 | ret = sensor_call(pcdev, video, g_mbus_config, &cfg); |
679419aa GL |
1222 | if (!ret) { |
1223 | common_flags = soc_mbus_config_compatible(&cfg, | |
1224 | bus_flags); | |
1225 | if (!common_flags) { | |
295ab497 | 1226 | dev_warn(pcdev_to_dev(pcdev), |
679419aa GL |
1227 | "Flags incompatible: camera 0x%x, host 0x%lx\n", |
1228 | cfg.flags, bus_flags); | |
1229 | return -EINVAL; | |
1230 | } | |
1231 | } else if (ret == -ENOIOCTLCMD) { | |
1232 | ret = 0; | |
1233 | } | |
ad5f2e85 | 1234 | |
679419aa | 1235 | return ret; |
ad5f2e85 GL |
1236 | } |
1237 | ||
760697be | 1238 | static const struct soc_mbus_pixelfmt pxa_camera_formats[] = { |
2a48fc73 | 1239 | { |
760697be GL |
1240 | .fourcc = V4L2_PIX_FMT_YUV422P, |
1241 | .name = "Planar YUV422 16 bit", | |
1242 | .bits_per_sample = 8, | |
1243 | .packing = SOC_MBUS_PACKING_2X8_PADHI, | |
1244 | .order = SOC_MBUS_ORDER_LE, | |
ad3b81fa | 1245 | .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_U_V, |
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 | ||
283e4a82 RJ |
1259 | static int pxa_camera_get_formats(struct v4l2_device *v4l2_dev, |
1260 | unsigned int idx, | |
2a48fc73 RJ |
1261 | struct soc_camera_format_xlate *xlate) |
1262 | { | |
283e4a82 | 1263 | struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev); |
760697be | 1264 | int formats = 0, ret; |
ebcff5fc HV |
1265 | struct v4l2_subdev_mbus_code_enum code = { |
1266 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
1267 | .index = idx, | |
1268 | }; | |
760697be | 1269 | const struct soc_mbus_pixelfmt *fmt; |
2a48fc73 | 1270 | |
b36bcbd6 | 1271 | ret = sensor_call(pcdev, pad, enum_mbus_code, NULL, &code); |
760697be GL |
1272 | if (ret < 0) |
1273 | /* No more formats */ | |
1274 | return 0; | |
2a48fc73 | 1275 | |
ebcff5fc | 1276 | fmt = soc_mbus_get_fmtdesc(code.code); |
760697be | 1277 | if (!fmt) { |
283e4a82 RJ |
1278 | dev_err(pcdev_to_dev(pcdev), |
1279 | "Invalid format code #%u: %d\n", idx, code.code); | |
2a48fc73 | 1280 | return 0; |
760697be | 1281 | } |
3bc43840 | 1282 | |
760697be | 1283 | /* This also checks support for the requested bits-per-sample */ |
283e4a82 | 1284 | ret = pxa_camera_try_bus_param(pcdev, fmt->bits_per_sample); |
2a48fc73 RJ |
1285 | if (ret < 0) |
1286 | return 0; | |
1287 | ||
ebcff5fc | 1288 | switch (code.code) { |
27ffaeb0 | 1289 | case MEDIA_BUS_FMT_UYVY8_2X8: |
2a48fc73 RJ |
1290 | formats++; |
1291 | if (xlate) { | |
760697be | 1292 | xlate->host_fmt = &pxa_camera_formats[0]; |
ebcff5fc | 1293 | xlate->code = code.code; |
2a48fc73 | 1294 | xlate++; |
295ab497 RJ |
1295 | dev_dbg(pcdev_to_dev(pcdev), |
1296 | "Providing format %s using code %d\n", | |
ebcff5fc | 1297 | pxa_camera_formats[0].name, code.code); |
2a48fc73 | 1298 | } |
27ffaeb0 BB |
1299 | case MEDIA_BUS_FMT_VYUY8_2X8: |
1300 | case MEDIA_BUS_FMT_YUYV8_2X8: | |
1301 | case MEDIA_BUS_FMT_YVYU8_2X8: | |
1302 | case MEDIA_BUS_FMT_RGB565_2X8_LE: | |
1303 | case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE: | |
760697be | 1304 | if (xlate) |
295ab497 RJ |
1305 | dev_dbg(pcdev_to_dev(pcdev), |
1306 | "Providing format %s packed\n", | |
760697be | 1307 | fmt->name); |
2a48fc73 RJ |
1308 | break; |
1309 | default: | |
760697be GL |
1310 | if (!pxa_camera_packing_supported(fmt)) |
1311 | return 0; | |
1312 | if (xlate) | |
295ab497 | 1313 | dev_dbg(pcdev_to_dev(pcdev), |
2a48fc73 | 1314 | "Providing format %s in pass-through mode\n", |
760697be GL |
1315 | fmt->name); |
1316 | } | |
1317 | ||
1318 | /* Generic pass-through */ | |
1319 | formats++; | |
1320 | if (xlate) { | |
1321 | xlate->host_fmt = fmt; | |
ebcff5fc | 1322 | xlate->code = code.code; |
760697be | 1323 | xlate++; |
2a48fc73 RJ |
1324 | } |
1325 | ||
1326 | return formats; | |
1327 | } | |
1328 | ||
283e4a82 RJ |
1329 | static int pxa_camera_build_formats(struct pxa_camera_dev *pcdev) |
1330 | { | |
1331 | struct soc_camera_format_xlate *xlate; | |
1332 | ||
1333 | xlate = soc_mbus_build_fmts_xlate(&pcdev->v4l2_dev, pcdev->sensor, | |
1334 | pxa_camera_get_formats); | |
1335 | if (IS_ERR(xlate)) | |
1336 | return PTR_ERR(xlate); | |
1337 | ||
1338 | pcdev->user_formats = xlate; | |
1339 | return 0; | |
1340 | } | |
1341 | ||
1342 | static void pxa_camera_destroy_formats(struct pxa_camera_dev *pcdev) | |
6a6c8786 | 1343 | { |
283e4a82 | 1344 | kfree(pcdev->user_formats); |
6a6c8786 GL |
1345 | } |
1346 | ||
760697be | 1347 | static int pxa_camera_check_frame(u32 width, u32 height) |
6a6c8786 GL |
1348 | { |
1349 | /* limit to pxa hardware capabilities */ | |
760697be GL |
1350 | return height < 32 || height > 2048 || width < 48 || width > 2048 || |
1351 | (width & 0x01); | |
6a6c8786 GL |
1352 | } |
1353 | ||
cdd657eb RJ |
1354 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
1355 | static int pxac_vidioc_g_register(struct file *file, void *priv, | |
1356 | struct v4l2_dbg_register *reg) | |
1357 | { | |
1358 | struct pxa_camera_dev *pcdev = video_drvdata(file); | |
1359 | ||
1360 | if (reg->reg > CIBR2) | |
1361 | return -ERANGE; | |
1362 | ||
1363 | reg->val = __raw_readl(pcdev->base + reg->reg); | |
1364 | reg->size = sizeof(__u32); | |
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | static int pxac_vidioc_s_register(struct file *file, void *priv, | |
1369 | const struct v4l2_dbg_register *reg) | |
1370 | { | |
1371 | struct pxa_camera_dev *pcdev = video_drvdata(file); | |
1372 | ||
1373 | if (reg->reg > CIBR2) | |
1374 | return -ERANGE; | |
1375 | if (reg->size != sizeof(__u32)) | |
1376 | return -EINVAL; | |
1377 | __raw_writel(reg->val, pcdev->base + reg->reg); | |
1378 | return 0; | |
1379 | } | |
1380 | #endif | |
1381 | ||
283e4a82 RJ |
1382 | static int pxac_vidioc_enum_fmt_vid_cap(struct file *filp, void *priv, |
1383 | struct v4l2_fmtdesc *f) | |
ad5f2e85 | 1384 | { |
283e4a82 RJ |
1385 | struct pxa_camera_dev *pcdev = video_drvdata(filp); |
1386 | const struct soc_mbus_pixelfmt *format; | |
1387 | unsigned int idx; | |
760697be | 1388 | |
283e4a82 RJ |
1389 | for (idx = 0; pcdev->user_formats[idx].code; idx++); |
1390 | if (f->index >= idx) | |
760697be | 1391 | return -EINVAL; |
2a48fc73 | 1392 | |
283e4a82 RJ |
1393 | format = pcdev->user_formats[f->index].host_fmt; |
1394 | f->pixelformat = format->fourcc; | |
1395 | return 0; | |
1396 | } | |
760697be | 1397 | |
283e4a82 RJ |
1398 | static int pxac_vidioc_g_fmt_vid_cap(struct file *filp, void *priv, |
1399 | struct v4l2_format *f) | |
1400 | { | |
1401 | struct pxa_camera_dev *pcdev = video_drvdata(filp); | |
1402 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
25c4d74e | 1403 | |
283e4a82 RJ |
1404 | pix->width = pcdev->current_pix.width; |
1405 | pix->height = pcdev->current_pix.height; | |
1406 | pix->bytesperline = pcdev->current_pix.bytesperline; | |
1407 | pix->sizeimage = pcdev->current_pix.sizeimage; | |
1408 | pix->field = pcdev->current_pix.field; | |
1409 | pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc; | |
1410 | pix->colorspace = pcdev->current_pix.colorspace; | |
1411 | dev_dbg(pcdev_to_dev(pcdev), "current_fmt->fourcc: 0x%08x\n", | |
1412 | pcdev->current_fmt->host_fmt->fourcc); | |
1413 | return 0; | |
ad5f2e85 GL |
1414 | } |
1415 | ||
283e4a82 RJ |
1416 | static int pxac_vidioc_try_fmt_vid_cap(struct file *filp, void *priv, |
1417 | struct v4l2_format *f) | |
3bc43840 | 1418 | { |
283e4a82 | 1419 | struct pxa_camera_dev *pcdev = video_drvdata(filp); |
2a48fc73 RJ |
1420 | const struct soc_camera_format_xlate *xlate; |
1421 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
5eab4983 HV |
1422 | struct v4l2_subdev_pad_config pad_cfg; |
1423 | struct v4l2_subdev_format format = { | |
1424 | .which = V4L2_SUBDEV_FORMAT_TRY, | |
1425 | }; | |
1426 | struct v4l2_mbus_framefmt *mf = &format.format; | |
2a48fc73 | 1427 | __u32 pixfmt = pix->pixelformat; |
bf507158 | 1428 | int ret; |
a2c8c68c | 1429 | |
283e4a82 | 1430 | xlate = soc_mbus_xlate_by_fourcc(pcdev->user_formats, pixfmt); |
2a48fc73 | 1431 | if (!xlate) { |
295ab497 | 1432 | dev_warn(pcdev_to_dev(pcdev), "Format %x not found\n", pixfmt); |
25c4d74e | 1433 | return -EINVAL; |
2a48fc73 | 1434 | } |
25c4d74e | 1435 | |
92a8337b | 1436 | /* |
4a6b8df2 TP |
1437 | * Limit to pxa hardware capabilities. YUV422P planar format requires |
1438 | * images size to be a multiple of 16 bytes. If not, zeros will be | |
1439 | * inserted between Y and U planes, and U and V planes, which violates | |
1440 | * the YUV422P standard. | |
92a8337b | 1441 | */ |
4a6b8df2 TP |
1442 | v4l_bound_align_image(&pix->width, 48, 2048, 1, |
1443 | &pix->height, 32, 2048, 0, | |
6a6c8786 | 1444 | pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0); |
92a8337b | 1445 | |
283e4a82 | 1446 | v4l2_fill_mbus_format(mf, pix, xlate->code); |
b36bcbd6 | 1447 | ret = sensor_call(pcdev, pad, set_fmt, &pad_cfg, &format); |
760697be GL |
1448 | if (ret < 0) |
1449 | return ret; | |
06daa1af | 1450 | |
283e4a82 | 1451 | v4l2_fill_pix_format(pix, mf); |
760697be | 1452 | |
283e4a82 | 1453 | /* Only progressive video supported so far */ |
5eab4983 | 1454 | switch (mf->field) { |
760697be GL |
1455 | case V4L2_FIELD_ANY: |
1456 | case V4L2_FIELD_NONE: | |
283e4a82 | 1457 | pix->field = V4L2_FIELD_NONE; |
760697be GL |
1458 | break; |
1459 | default: | |
1460 | /* TODO: support interlaced at least in pass-through mode */ | |
295ab497 | 1461 | dev_err(pcdev_to_dev(pcdev), "Field type %d unsupported.\n", |
5eab4983 | 1462 | mf->field); |
06daa1af GL |
1463 | return -EINVAL; |
1464 | } | |
1465 | ||
283e4a82 RJ |
1466 | ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt); |
1467 | if (ret < 0) | |
1468 | return ret; | |
1469 | ||
1470 | pix->bytesperline = ret; | |
1471 | ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline, | |
1472 | pix->height); | |
1473 | if (ret < 0) | |
1474 | return ret; | |
1475 | ||
1476 | pix->sizeimage = ret; | |
1477 | return 0; | |
3bc43840 GL |
1478 | } |
1479 | ||
283e4a82 RJ |
1480 | static int pxac_vidioc_s_fmt_vid_cap(struct file *filp, void *priv, |
1481 | struct v4l2_format *f) | |
3bc43840 | 1482 | { |
283e4a82 RJ |
1483 | struct pxa_camera_dev *pcdev = video_drvdata(filp); |
1484 | const struct soc_camera_format_xlate *xlate; | |
1485 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
1486 | struct v4l2_subdev_format format = { | |
1487 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
1488 | }; | |
1489 | unsigned long flags; | |
1490 | int ret, is_busy; | |
1491 | ||
1492 | dev_dbg(pcdev_to_dev(pcdev), | |
1493 | "s_fmt_vid_cap(pix=%dx%d:%x)\n", | |
1494 | pix->width, pix->height, pix->pixelformat); | |
1495 | ||
1496 | spin_lock_irqsave(&pcdev->lock, flags); | |
1497 | is_busy = pcdev->active || vb2_is_busy(&pcdev->vb2_vq); | |
1498 | spin_unlock_irqrestore(&pcdev->lock, flags); | |
1499 | ||
1500 | if (is_busy) | |
1501 | return -EBUSY; | |
1502 | ||
1503 | ret = pxac_vidioc_try_fmt_vid_cap(filp, priv, f); | |
1504 | if (ret) | |
1505 | return ret; | |
1506 | ||
1507 | xlate = soc_mbus_xlate_by_fourcc(pcdev->user_formats, | |
1508 | pix->pixelformat); | |
1509 | v4l2_fill_mbus_format(&format.format, pix, xlate->code); | |
1510 | ret = sensor_call(pcdev, pad, set_fmt, NULL, &format); | |
1511 | if (ret < 0) { | |
1512 | dev_warn(pcdev_to_dev(pcdev), | |
1513 | "Failed to configure for format %x\n", | |
1514 | pix->pixelformat); | |
1515 | } else if (pxa_camera_check_frame(pix->width, pix->height)) { | |
1516 | dev_warn(pcdev_to_dev(pcdev), | |
1517 | "Camera driver produced an unsupported frame %dx%d\n", | |
1518 | pix->width, pix->height); | |
1519 | return -EINVAL; | |
1520 | } | |
1521 | ||
1522 | pcdev->current_fmt = xlate; | |
1523 | pcdev->current_pix = *pix; | |
3bc43840 | 1524 | |
283e4a82 RJ |
1525 | ret = pxa_camera_set_bus_param(pcdev); |
1526 | return ret; | |
3bc43840 GL |
1527 | } |
1528 | ||
283e4a82 RJ |
1529 | static int pxac_vidioc_querycap(struct file *file, void *priv, |
1530 | struct v4l2_capability *cap) | |
3bc43840 | 1531 | { |
283e4a82 RJ |
1532 | strlcpy(cap->bus_info, "platform:pxa-camera", sizeof(cap->bus_info)); |
1533 | strlcpy(cap->driver, PXA_CAM_DRV_NAME, sizeof(cap->driver)); | |
3bc43840 | 1534 | strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card)); |
7d96c3e4 GL |
1535 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
1536 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
3bc43840 GL |
1537 | |
1538 | return 0; | |
1539 | } | |
1540 | ||
283e4a82 RJ |
1541 | static int pxac_vidioc_enum_input(struct file *file, void *priv, |
1542 | struct v4l2_input *i) | |
1543 | { | |
1544 | if (i->index > 0) | |
1545 | return -EINVAL; | |
1546 | ||
1547 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
1548 | strlcpy(i->name, "Camera", sizeof(i->name)); | |
1549 | ||
1550 | return 0; | |
1551 | } | |
1552 | ||
1553 | static int pxac_vidioc_g_input(struct file *file, void *priv, unsigned int *i) | |
1554 | { | |
1555 | *i = 0; | |
1556 | ||
1557 | return 0; | |
1558 | } | |
1559 | ||
1560 | static int pxac_vidioc_s_input(struct file *file, void *priv, unsigned int i) | |
1561 | { | |
1562 | if (i > 0) | |
1563 | return -EINVAL; | |
1564 | ||
1565 | return 0; | |
1566 | } | |
1567 | ||
1568 | static int pxac_fops_camera_open(struct file *filp) | |
1569 | { | |
1570 | struct pxa_camera_dev *pcdev = video_drvdata(filp); | |
1571 | int ret; | |
1572 | ||
1573 | mutex_lock(&pcdev->mlock); | |
1574 | ret = v4l2_fh_open(filp); | |
1575 | if (ret < 0) | |
1576 | goto out; | |
1577 | ||
1578 | ret = sensor_call(pcdev, core, s_power, 1); | |
1579 | if (ret) | |
1580 | v4l2_fh_release(filp); | |
1581 | out: | |
1582 | mutex_unlock(&pcdev->mlock); | |
1583 | return ret; | |
1584 | } | |
1585 | ||
1586 | static int pxac_fops_camera_release(struct file *filp) | |
1587 | { | |
1588 | struct pxa_camera_dev *pcdev = video_drvdata(filp); | |
1589 | int ret; | |
1590 | ||
1591 | ret = vb2_fop_release(filp); | |
1592 | if (ret < 0) | |
1593 | return ret; | |
1594 | ||
1595 | mutex_lock(&pcdev->mlock); | |
1596 | ret = sensor_call(pcdev, core, s_power, 0); | |
1597 | mutex_unlock(&pcdev->mlock); | |
1598 | ||
1599 | return ret; | |
1600 | } | |
1601 | ||
1602 | static const struct v4l2_file_operations pxa_camera_fops = { | |
1603 | .owner = THIS_MODULE, | |
1604 | .open = pxac_fops_camera_open, | |
1605 | .release = pxac_fops_camera_release, | |
1606 | .read = vb2_fop_read, | |
1607 | .poll = vb2_fop_poll, | |
1608 | .mmap = vb2_fop_mmap, | |
1609 | .unlocked_ioctl = video_ioctl2, | |
1610 | }; | |
1611 | ||
1612 | static const struct v4l2_ioctl_ops pxa_camera_ioctl_ops = { | |
1613 | .vidioc_querycap = pxac_vidioc_querycap, | |
1614 | ||
1615 | .vidioc_enum_input = pxac_vidioc_enum_input, | |
1616 | .vidioc_g_input = pxac_vidioc_g_input, | |
1617 | .vidioc_s_input = pxac_vidioc_s_input, | |
1618 | ||
1619 | .vidioc_enum_fmt_vid_cap = pxac_vidioc_enum_fmt_vid_cap, | |
1620 | .vidioc_g_fmt_vid_cap = pxac_vidioc_g_fmt_vid_cap, | |
1621 | .vidioc_s_fmt_vid_cap = pxac_vidioc_s_fmt_vid_cap, | |
1622 | .vidioc_try_fmt_vid_cap = pxac_vidioc_try_fmt_vid_cap, | |
1623 | ||
1624 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
1625 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
1626 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1627 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1628 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
1629 | .vidioc_expbuf = vb2_ioctl_expbuf, | |
1630 | .vidioc_streamon = vb2_ioctl_streamon, | |
1631 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
cdd657eb RJ |
1632 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
1633 | .vidioc_g_register = pxac_vidioc_g_register, | |
1634 | .vidioc_s_register = pxac_vidioc_s_register, | |
1635 | #endif | |
283e4a82 RJ |
1636 | }; |
1637 | ||
1638 | static struct v4l2_clk_ops pxa_camera_mclk_ops = { | |
1639 | }; | |
1640 | ||
1641 | static const struct video_device pxa_camera_videodev_template = { | |
1642 | .name = "pxa-camera", | |
1643 | .minor = -1, | |
1644 | .fops = &pxa_camera_fops, | |
1645 | .ioctl_ops = &pxa_camera_ioctl_ops, | |
1646 | .release = video_device_release_empty, | |
1647 | .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING, | |
1648 | }; | |
1649 | ||
1650 | static int pxa_camera_sensor_bound(struct v4l2_async_notifier *notifier, | |
1651 | struct v4l2_subdev *subdev, | |
1652 | struct v4l2_async_subdev *asd) | |
1653 | { | |
1654 | int err; | |
1655 | struct v4l2_device *v4l2_dev = notifier->v4l2_dev; | |
1656 | struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev); | |
1657 | struct video_device *vdev = &pcdev->vdev; | |
1658 | struct v4l2_pix_format *pix = &pcdev->current_pix; | |
1659 | struct v4l2_subdev_format format = { | |
1660 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
1661 | }; | |
1662 | struct v4l2_mbus_framefmt *mf = &format.format; | |
1663 | ||
1664 | dev_info(pcdev_to_dev(pcdev), "%s(): trying to bind a device\n", | |
1665 | __func__); | |
1666 | mutex_lock(&pcdev->mlock); | |
1667 | *vdev = pxa_camera_videodev_template; | |
1668 | vdev->v4l2_dev = v4l2_dev; | |
1669 | vdev->lock = &pcdev->mlock; | |
1670 | pcdev->sensor = subdev; | |
1671 | pcdev->vdev.queue = &pcdev->vb2_vq; | |
1672 | pcdev->vdev.v4l2_dev = &pcdev->v4l2_dev; | |
1673 | pcdev->vdev.ctrl_handler = subdev->ctrl_handler; | |
1674 | video_set_drvdata(&pcdev->vdev, pcdev); | |
1675 | ||
1676 | err = pxa_camera_build_formats(pcdev); | |
1677 | if (err) { | |
1678 | dev_err(pcdev_to_dev(pcdev), "building formats failed: %d\n", | |
1679 | err); | |
1680 | goto out; | |
1681 | } | |
1682 | ||
1683 | pcdev->current_fmt = pcdev->user_formats; | |
1684 | pix->field = V4L2_FIELD_NONE; | |
1685 | pix->width = DEFAULT_WIDTH; | |
1686 | pix->height = DEFAULT_HEIGHT; | |
1687 | pix->bytesperline = | |
1688 | soc_mbus_bytes_per_line(pix->width, | |
1689 | pcdev->current_fmt->host_fmt); | |
1690 | pix->sizeimage = | |
1691 | soc_mbus_image_size(pcdev->current_fmt->host_fmt, | |
1692 | pix->bytesperline, pix->height); | |
1693 | pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc; | |
1694 | v4l2_fill_mbus_format(mf, pix, pcdev->current_fmt->code); | |
1695 | err = sensor_call(pcdev, pad, set_fmt, NULL, &format); | |
1696 | if (err) | |
1697 | goto out; | |
1698 | ||
1699 | v4l2_fill_pix_format(pix, mf); | |
1700 | pr_info("%s(): colorspace=0x%x pixfmt=0x%x\n", | |
1701 | __func__, pix->colorspace, pix->pixelformat); | |
1702 | ||
1703 | err = pxa_camera_init_videobuf2(pcdev); | |
1704 | if (err) | |
1705 | goto out; | |
1706 | ||
1707 | err = video_register_device(&pcdev->vdev, VFL_TYPE_GRABBER, -1); | |
1708 | if (err) { | |
1709 | v4l2_err(v4l2_dev, "register video device failed: %d\n", err); | |
1710 | pcdev->sensor = NULL; | |
1711 | } else { | |
1712 | dev_info(pcdev_to_dev(pcdev), | |
1713 | "PXA Camera driver attached to camera %s\n", | |
1714 | subdev->name); | |
1715 | } | |
1716 | out: | |
1717 | mutex_unlock(&pcdev->mlock); | |
1718 | return err; | |
1719 | } | |
1720 | ||
1721 | static void pxa_camera_sensor_unbind(struct v4l2_async_notifier *notifier, | |
1722 | struct v4l2_subdev *subdev, | |
1723 | struct v4l2_async_subdev *asd) | |
1724 | { | |
1725 | struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(notifier->v4l2_dev); | |
1726 | ||
1727 | mutex_lock(&pcdev->mlock); | |
1728 | dev_info(pcdev_to_dev(pcdev), | |
1729 | "PXA Camera driver detached from camera %s\n", | |
1730 | subdev->name); | |
1731 | ||
1732 | /* disable capture, disable interrupts */ | |
1733 | __raw_writel(0x3ff, pcdev->base + CICR0); | |
1734 | ||
1735 | /* Stop DMA engine */ | |
1736 | pxa_dma_stop_channels(pcdev); | |
1737 | ||
1738 | pxa_camera_destroy_formats(pcdev); | |
1739 | video_unregister_device(&pcdev->vdev); | |
1740 | pcdev->sensor = NULL; | |
1741 | ||
1742 | mutex_unlock(&pcdev->mlock); | |
1743 | } | |
1744 | ||
6f28435d RJ |
1745 | /* |
1746 | * Driver probe, remove, suspend and resume operations | |
1747 | */ | |
7254026c | 1748 | static int pxa_camera_suspend(struct device *dev) |
3f6ac497 | 1749 | { |
283e4a82 | 1750 | struct pxa_camera_dev *pcdev = dev_get_drvdata(dev); |
3f6ac497 RJ |
1751 | int i = 0, ret = 0; |
1752 | ||
5ca11fa3 EM |
1753 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0); |
1754 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1); | |
1755 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2); | |
1756 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3); | |
1757 | pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4); | |
3f6ac497 | 1758 | |
283e4a82 | 1759 | if (pcdev->sensor) { |
b36bcbd6 | 1760 | ret = sensor_call(pcdev, core, s_power, 0); |
497833c6 GL |
1761 | if (ret == -ENOIOCTLCMD) |
1762 | ret = 0; | |
1763 | } | |
3f6ac497 RJ |
1764 | |
1765 | return ret; | |
1766 | } | |
1767 | ||
7254026c | 1768 | static int pxa_camera_resume(struct device *dev) |
3f6ac497 | 1769 | { |
283e4a82 | 1770 | struct pxa_camera_dev *pcdev = dev_get_drvdata(dev); |
3f6ac497 RJ |
1771 | int i = 0, ret = 0; |
1772 | ||
5ca11fa3 EM |
1773 | __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0); |
1774 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1); | |
1775 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2); | |
1776 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3); | |
1777 | __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4); | |
3f6ac497 | 1778 | |
283e4a82 | 1779 | if (pcdev->sensor) { |
b36bcbd6 | 1780 | ret = sensor_call(pcdev, core, s_power, 1); |
497833c6 GL |
1781 | if (ret == -ENOIOCTLCMD) |
1782 | ret = 0; | |
1783 | } | |
3f6ac497 RJ |
1784 | |
1785 | /* Restart frame capture if active buffer exists */ | |
256b0233 RJ |
1786 | if (!ret && pcdev->active) |
1787 | pxa_camera_start_capture(pcdev); | |
3f6ac497 RJ |
1788 | |
1789 | return ret; | |
1790 | } | |
1791 | ||
e9a1d94f | 1792 | static int pxa_camera_pdata_from_dt(struct device *dev, |
283e4a82 RJ |
1793 | struct pxa_camera_dev *pcdev, |
1794 | struct v4l2_async_subdev *asd) | |
e9a1d94f RJ |
1795 | { |
1796 | u32 mclk_rate; | |
283e4a82 | 1797 | struct device_node *remote, *np = dev->of_node; |
e9a1d94f RJ |
1798 | struct v4l2_of_endpoint ep; |
1799 | int err = of_property_read_u32(np, "clock-frequency", | |
1800 | &mclk_rate); | |
1801 | if (!err) { | |
1802 | pcdev->platform_flags |= PXA_CAMERA_MCLK_EN; | |
1803 | pcdev->mclk = mclk_rate; | |
1804 | } | |
1805 | ||
1806 | np = of_graph_get_next_endpoint(np, NULL); | |
1807 | if (!np) { | |
283e4a82 | 1808 | dev_err(dev, "could not find endpoint\n"); |
e9a1d94f RJ |
1809 | return -EINVAL; |
1810 | } | |
1811 | ||
1812 | err = v4l2_of_parse_endpoint(np, &ep); | |
1813 | if (err) { | |
283e4a82 | 1814 | dev_err(dev, "could not parse endpoint\n"); |
e9a1d94f RJ |
1815 | goto out; |
1816 | } | |
1817 | ||
1818 | switch (ep.bus.parallel.bus_width) { | |
1819 | case 4: | |
1820 | pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_4; | |
1821 | break; | |
1822 | case 5: | |
1823 | pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_5; | |
1824 | break; | |
1825 | case 8: | |
1826 | pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_8; | |
1827 | break; | |
1828 | case 9: | |
1829 | pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_9; | |
1830 | break; | |
1831 | case 10: | |
1832 | pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10; | |
1833 | break; | |
1834 | default: | |
1835 | break; | |
c611c908 | 1836 | } |
e9a1d94f RJ |
1837 | |
1838 | if (ep.bus.parallel.flags & V4L2_MBUS_MASTER) | |
1839 | pcdev->platform_flags |= PXA_CAMERA_MASTER; | |
1840 | if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) | |
1841 | pcdev->platform_flags |= PXA_CAMERA_HSP; | |
1842 | if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) | |
1843 | pcdev->platform_flags |= PXA_CAMERA_VSP; | |
1844 | if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING) | |
1845 | pcdev->platform_flags |= PXA_CAMERA_PCLK_EN | PXA_CAMERA_PCP; | |
1846 | if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING) | |
1847 | pcdev->platform_flags |= PXA_CAMERA_PCLK_EN; | |
1848 | ||
283e4a82 RJ |
1849 | asd->match_type = V4L2_ASYNC_MATCH_OF; |
1850 | remote = of_graph_get_remote_port(np); | |
1851 | if (remote) { | |
1852 | asd->match.of.node = remote; | |
1853 | of_node_put(remote); | |
1854 | } else { | |
1855 | dev_notice(dev, "no remote for %s\n", of_node_full_name(np)); | |
1856 | } | |
1857 | ||
e9a1d94f RJ |
1858 | out: |
1859 | of_node_put(np); | |
1860 | ||
1861 | return err; | |
1862 | } | |
1863 | ||
4c62e976 | 1864 | static int pxa_camera_probe(struct platform_device *pdev) |
3bc43840 GL |
1865 | { |
1866 | struct pxa_camera_dev *pcdev; | |
1867 | struct resource *res; | |
1868 | void __iomem *base; | |
1e77d55a RJ |
1869 | struct dma_slave_config config = { |
1870 | .src_addr_width = 0, | |
1871 | .src_maxburst = 8, | |
1872 | .direction = DMA_DEV_TO_MEM, | |
1873 | }; | |
1874 | dma_cap_mask_t mask; | |
1875 | struct pxad_param params; | |
283e4a82 | 1876 | char clk_name[V4L2_CLK_NAME_SIZE]; |
02da4659 | 1877 | int irq; |
1e77d55a | 1878 | int err = 0, i; |
3bc43840 GL |
1879 | |
1880 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1881 | irq = platform_get_irq(pdev, 0); | |
47de201c JL |
1882 | if (!res || irq < 0) |
1883 | return -ENODEV; | |
3bc43840 | 1884 | |
47de201c | 1885 | pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL); |
3bc43840 | 1886 | if (!pcdev) { |
7102b773 | 1887 | dev_err(&pdev->dev, "Could not allocate pcdev\n"); |
47de201c | 1888 | return -ENOMEM; |
3bc43840 GL |
1889 | } |
1890 | ||
47de201c JL |
1891 | pcdev->clk = devm_clk_get(&pdev->dev, NULL); |
1892 | if (IS_ERR(pcdev->clk)) | |
1893 | return PTR_ERR(pcdev->clk); | |
3bc43840 | 1894 | |
3bc43840 GL |
1895 | pcdev->res = res; |
1896 | ||
1897 | pcdev->pdata = pdev->dev.platform_data; | |
e9a1d94f | 1898 | if (&pdev->dev.of_node && !pcdev->pdata) { |
283e4a82 | 1899 | err = pxa_camera_pdata_from_dt(&pdev->dev, pcdev, &pcdev->asd); |
e9a1d94f RJ |
1900 | } else { |
1901 | pcdev->platform_flags = pcdev->pdata->flags; | |
1902 | pcdev->mclk = pcdev->pdata->mclk_10khz * 10000; | |
283e4a82 RJ |
1903 | pcdev->asd.match_type = V4L2_ASYNC_MATCH_I2C; |
1904 | pcdev->asd.match.i2c.adapter_id = | |
1905 | pcdev->pdata->sensor_i2c_adapter_id; | |
1906 | pcdev->asd.match.i2c.address = pcdev->pdata->sensor_i2c_address; | |
e9a1d94f RJ |
1907 | } |
1908 | if (err < 0) | |
1909 | return err; | |
1910 | ||
ad5f2e85 GL |
1911 | if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 | |
1912 | PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) { | |
5d28d525 GL |
1913 | /* |
1914 | * Platform hasn't set available data widths. This is bad. | |
1915 | * Warn and use a default. | |
1916 | */ | |
3bc43840 GL |
1917 | dev_warn(&pdev->dev, "WARNING! Platform hasn't set available " |
1918 | "data widths, using default 10 bit\n"); | |
1919 | pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10; | |
1920 | } | |
679419aa GL |
1921 | if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8) |
1922 | pcdev->width_flags = 1 << 7; | |
1923 | if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9) | |
1924 | pcdev->width_flags |= 1 << 8; | |
1925 | if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10) | |
1926 | pcdev->width_flags |= 1 << 9; | |
cf34cba7 | 1927 | if (!pcdev->mclk) { |
3bc43840 | 1928 | dev_warn(&pdev->dev, |
cf34cba7 | 1929 | "mclk == 0! Please, fix your platform data. " |
3bc43840 | 1930 | "Using default 20MHz\n"); |
cf34cba7 | 1931 | pcdev->mclk = 20000000; |
3bc43840 GL |
1932 | } |
1933 | ||
40e2e092 | 1934 | pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev); |
cf34cba7 | 1935 | |
3bc43840 GL |
1936 | INIT_LIST_HEAD(&pcdev->capture); |
1937 | spin_lock_init(&pcdev->lock); | |
283e4a82 | 1938 | mutex_init(&pcdev->mlock); |
3bc43840 GL |
1939 | |
1940 | /* | |
1941 | * Request the regions. | |
1942 | */ | |
8efdb135 SK |
1943 | base = devm_ioremap_resource(&pdev->dev, res); |
1944 | if (IS_ERR(base)) | |
1945 | return PTR_ERR(base); | |
1946 | ||
3bc43840 GL |
1947 | pcdev->irq = irq; |
1948 | pcdev->base = base; | |
3bc43840 GL |
1949 | |
1950 | /* request dma */ | |
1e77d55a RJ |
1951 | dma_cap_zero(mask); |
1952 | dma_cap_set(DMA_SLAVE, mask); | |
1953 | dma_cap_set(DMA_PRIVATE, mask); | |
1954 | ||
1955 | params.prio = 0; | |
1956 | params.drcmr = 68; | |
1957 | pcdev->dma_chans[0] = | |
1958 | dma_request_slave_channel_compat(mask, pxad_filter_fn, | |
1959 | ¶ms, &pdev->dev, "CI_Y"); | |
1960 | if (!pcdev->dma_chans[0]) { | |
eff505fa | 1961 | dev_err(&pdev->dev, "Can't request DMA for Y\n"); |
1e77d55a | 1962 | return -ENODEV; |
3bc43840 | 1963 | } |
a5462e5b | 1964 | |
1e77d55a RJ |
1965 | params.drcmr = 69; |
1966 | pcdev->dma_chans[1] = | |
1967 | dma_request_slave_channel_compat(mask, pxad_filter_fn, | |
1968 | ¶ms, &pdev->dev, "CI_U"); | |
1969 | if (!pcdev->dma_chans[1]) { | |
1970 | dev_err(&pdev->dev, "Can't request DMA for Y\n"); | |
a5462e5b MR |
1971 | goto exit_free_dma_y; |
1972 | } | |
a5462e5b | 1973 | |
1e77d55a RJ |
1974 | params.drcmr = 70; |
1975 | pcdev->dma_chans[2] = | |
1976 | dma_request_slave_channel_compat(mask, pxad_filter_fn, | |
1977 | ¶ms, &pdev->dev, "CI_V"); | |
1978 | if (!pcdev->dma_chans[2]) { | |
eff505fa | 1979 | dev_err(&pdev->dev, "Can't request DMA for V\n"); |
a5462e5b MR |
1980 | goto exit_free_dma_u; |
1981 | } | |
3bc43840 | 1982 | |
1e77d55a RJ |
1983 | for (i = 0; i < 3; i++) { |
1984 | config.src_addr = pcdev->res->start + CIBR0 + i * 8; | |
1985 | err = dmaengine_slave_config(pcdev->dma_chans[i], &config); | |
1986 | if (err < 0) { | |
1987 | dev_err(&pdev->dev, "dma slave config failed: %d\n", | |
1988 | err); | |
1989 | goto exit_free_dma; | |
1990 | } | |
1991 | } | |
3bc43840 GL |
1992 | |
1993 | /* request irq */ | |
47de201c JL |
1994 | err = devm_request_irq(&pdev->dev, pcdev->irq, pxa_camera_irq, 0, |
1995 | PXA_CAM_DRV_NAME, pcdev); | |
3bc43840 | 1996 | if (err) { |
47de201c | 1997 | dev_err(&pdev->dev, "Camera interrupt register failed\n"); |
3bc43840 GL |
1998 | goto exit_free_dma; |
1999 | } | |
2000 | ||
e623ebe6 | 2001 | tasklet_init(&pcdev->task_eof, pxa_camera_eof, (unsigned long)pcdev); |
eff505fa | 2002 | |
283e4a82 RJ |
2003 | pxa_camera_activate(pcdev); |
2004 | ||
2005 | dev_set_drvdata(&pdev->dev, pcdev); | |
2006 | err = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev); | |
3bc43840 | 2007 | if (err) |
47de201c | 2008 | goto exit_free_dma; |
3bc43840 | 2009 | |
283e4a82 RJ |
2010 | pcdev->asds[0] = &pcdev->asd; |
2011 | pcdev->notifier.subdevs = pcdev->asds; | |
2012 | pcdev->notifier.num_subdevs = 1; | |
2013 | pcdev->notifier.bound = pxa_camera_sensor_bound; | |
2014 | pcdev->notifier.unbind = pxa_camera_sensor_unbind; | |
2015 | ||
2016 | if (!of_have_populated_dt()) | |
2017 | pcdev->asd.match_type = V4L2_ASYNC_MATCH_I2C; | |
2018 | ||
2019 | err = pxa_camera_init_videobuf2(pcdev); | |
2020 | if (err) | |
2021 | goto exit_free_v4l2dev; | |
3bc43840 | 2022 | |
283e4a82 RJ |
2023 | if (pcdev->mclk) { |
2024 | v4l2_clk_name_i2c(clk_name, sizeof(clk_name), | |
2025 | pcdev->asd.match.i2c.adapter_id, | |
2026 | pcdev->asd.match.i2c.address); | |
2027 | ||
2028 | pcdev->mclk_clk = v4l2_clk_register(&pxa_camera_mclk_ops, | |
2029 | clk_name, NULL); | |
2030 | if (IS_ERR(pcdev->mclk_clk)) | |
2031 | return PTR_ERR(pcdev->mclk_clk); | |
2032 | } | |
2033 | ||
2034 | err = v4l2_async_notifier_register(&pcdev->v4l2_dev, &pcdev->notifier); | |
2035 | if (err) | |
2036 | goto exit_free_clk; | |
2037 | ||
2038 | return 0; | |
2039 | exit_free_clk: | |
2040 | v4l2_clk_unregister(pcdev->mclk_clk); | |
2041 | exit_free_v4l2dev: | |
2042 | v4l2_device_unregister(&pcdev->v4l2_dev); | |
3bc43840 | 2043 | exit_free_dma: |
1e77d55a | 2044 | dma_release_channel(pcdev->dma_chans[2]); |
a5462e5b | 2045 | exit_free_dma_u: |
1e77d55a | 2046 | dma_release_channel(pcdev->dma_chans[1]); |
a5462e5b | 2047 | exit_free_dma_y: |
1e77d55a | 2048 | dma_release_channel(pcdev->dma_chans[0]); |
3bc43840 GL |
2049 | return err; |
2050 | } | |
2051 | ||
4c62e976 | 2052 | static int pxa_camera_remove(struct platform_device *pdev) |
3bc43840 | 2053 | { |
283e4a82 | 2054 | struct pxa_camera_dev *pcdev = dev_get_drvdata(&pdev->dev); |
3bc43840 | 2055 | |
283e4a82 | 2056 | pxa_camera_deactivate(pcdev); |
1e77d55a RJ |
2057 | dma_release_channel(pcdev->dma_chans[0]); |
2058 | dma_release_channel(pcdev->dma_chans[1]); | |
2059 | dma_release_channel(pcdev->dma_chans[2]); | |
3bc43840 | 2060 | |
283e4a82 RJ |
2061 | v4l2_clk_unregister(pcdev->mclk_clk); |
2062 | v4l2_device_unregister(&pcdev->v4l2_dev); | |
3bc43840 | 2063 | |
7102b773 | 2064 | dev_info(&pdev->dev, "PXA Camera driver unloaded\n"); |
3bc43840 | 2065 | |
3bc43840 GL |
2066 | return 0; |
2067 | } | |
2068 | ||
56a49194 | 2069 | static const struct dev_pm_ops pxa_camera_pm = { |
7254026c GL |
2070 | .suspend = pxa_camera_suspend, |
2071 | .resume = pxa_camera_resume, | |
2072 | }; | |
2073 | ||
e9a1d94f RJ |
2074 | static const struct of_device_id pxa_camera_of_match[] = { |
2075 | { .compatible = "marvell,pxa270-qci", }, | |
2076 | {}, | |
2077 | }; | |
2078 | MODULE_DEVICE_TABLE(of, pxa_camera_of_match); | |
2079 | ||
3bc43840 | 2080 | static struct platform_driver pxa_camera_driver = { |
6003b2ad | 2081 | .driver = { |
3bc43840 | 2082 | .name = PXA_CAM_DRV_NAME, |
7254026c | 2083 | .pm = &pxa_camera_pm, |
e9a1d94f | 2084 | .of_match_table = of_match_ptr(pxa_camera_of_match), |
3bc43840 GL |
2085 | }, |
2086 | .probe = pxa_camera_probe, | |
4c62e976 | 2087 | .remove = pxa_camera_remove, |
3bc43840 GL |
2088 | }; |
2089 | ||
1d6629b1 | 2090 | module_platform_driver(pxa_camera_driver); |
3bc43840 GL |
2091 | |
2092 | MODULE_DESCRIPTION("PXA27x SoC Camera Host driver"); | |
2093 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
2094 | MODULE_LICENSE("GPL"); | |
64dc3c1a | 2095 | MODULE_VERSION(PXA_CAM_VERSION); |
40e2e092 | 2096 | MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME); |