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