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