1 // SPDX-License-Identifier: GPL-2.0+
3 * i.MX Pixel Pipeline (PXP) mem-to-mem scaler/CSC/rotator driver
5 * Copyright (c) 2018 Pengutronix, Philipp Zabel
9 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
10 * Pawel Osciak, <pawel@osciak.com>
11 * Marek Szyprowski, <m.szyprowski@samsung.com>
13 #include <linux/bitfield.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/interrupt.h>
19 #include <linux/iopoll.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
27 #include <media/media-device.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-event.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-mem2mem.h>
33 #include <media/videobuf2-dma-contig.h>
37 static unsigned int debug
;
38 module_param(debug
, uint
, 0644);
39 MODULE_PARM_DESC(debug
, "activates debug info");
45 #define ALIGN_W 3 /* 8x8 pixel blocks */
48 /* Flags that indicate a format can be used for capture/output */
49 #define MEM2MEM_CAPTURE (1 << 0)
50 #define MEM2MEM_OUTPUT (1 << 1)
52 #define MEM2MEM_NAME "pxp"
54 /* Flags that indicate processing mode */
55 #define MEM2MEM_HFLIP (1 << 0)
56 #define MEM2MEM_VFLIP (1 << 1)
58 #define PXP_VERSION_MAJOR(version) \
59 FIELD_GET(BM_PXP_VERSION_MAJOR, version)
60 #define PXP_VERSION_MINOR(version) \
61 FIELD_GET(BM_PXP_VERSION_MINOR, version)
63 #define dprintk(dev, fmt, arg...) \
64 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
69 /* Types the format can be used for */
73 static struct pxp_fmt formats
[] = {
75 .fourcc
= V4L2_PIX_FMT_XBGR32
,
77 /* Both capture and output format */
78 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
80 .fourcc
= V4L2_PIX_FMT_ABGR32
,
82 /* Capture-only format */
83 .types
= MEM2MEM_CAPTURE
,
85 .fourcc
= V4L2_PIX_FMT_BGR24
,
87 .types
= MEM2MEM_CAPTURE
,
89 .fourcc
= V4L2_PIX_FMT_RGB565
,
91 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
93 .fourcc
= V4L2_PIX_FMT_RGB555
,
95 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
97 .fourcc
= V4L2_PIX_FMT_RGB444
,
99 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
101 .fourcc
= V4L2_PIX_FMT_VUYA32
,
103 .types
= MEM2MEM_CAPTURE
,
105 .fourcc
= V4L2_PIX_FMT_VUYX32
,
107 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
109 .fourcc
= V4L2_PIX_FMT_UYVY
,
111 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
113 .fourcc
= V4L2_PIX_FMT_YUYV
,
115 /* Output-only format */
116 .types
= MEM2MEM_OUTPUT
,
118 .fourcc
= V4L2_PIX_FMT_VYUY
,
120 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
122 .fourcc
= V4L2_PIX_FMT_YVYU
,
124 .types
= MEM2MEM_OUTPUT
,
126 .fourcc
= V4L2_PIX_FMT_GREY
,
128 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
130 .fourcc
= V4L2_PIX_FMT_Y4
,
132 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
134 .fourcc
= V4L2_PIX_FMT_NV16
,
136 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
138 .fourcc
= V4L2_PIX_FMT_NV12
,
140 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
142 .fourcc
= V4L2_PIX_FMT_NV21
,
144 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
146 .fourcc
= V4L2_PIX_FMT_NV61
,
148 .types
= MEM2MEM_CAPTURE
| MEM2MEM_OUTPUT
,
150 .fourcc
= V4L2_PIX_FMT_YUV422P
,
152 .types
= MEM2MEM_OUTPUT
,
154 .fourcc
= V4L2_PIX_FMT_YUV420
,
156 .types
= MEM2MEM_OUTPUT
,
160 #define NUM_FORMATS ARRAY_SIZE(formats)
162 /* Per-queue, driver-specific private data */
166 unsigned int bytesperline
;
167 unsigned int sizeimage
;
168 unsigned int sequence
;
170 enum v4l2_ycbcr_encoding ycbcr_enc
;
171 enum v4l2_quantization quant
;
179 static const struct regmap_config pxp_regmap_config
= {
183 .max_register
= HW_PXP_VERSION
,
186 static struct pxp_fmt
*find_format(unsigned int pixelformat
)
191 for (k
= 0; k
< NUM_FORMATS
; k
++) {
193 if (fmt
->fourcc
== pixelformat
)
197 if (k
== NUM_FORMATS
)
206 u32 (*data_path_ctrl0
)(struct pxp_ctx
*ctx
);
210 struct v4l2_device v4l2_dev
;
211 struct video_device vfd
;
212 #ifdef CONFIG_MEDIA_CONTROLLER
213 struct media_device mdev
;
217 struct regmap
*regmap
;
219 const struct pxp_pdata
*pdata
;
222 struct mutex dev_mutex
;
225 struct v4l2_m2m_dev
*m2m_dev
;
232 struct v4l2_ctrl_handler hdl
;
234 /* Abort requested by m2m */
237 /* Processing mode */
242 enum v4l2_colorspace colorspace
;
243 enum v4l2_xfer_func xfer_func
;
245 /* Source and destination queue data */
246 struct pxp_q_data q_data
[2];
249 static inline struct pxp_ctx
*file2ctx(struct file
*file
)
251 return container_of(file
->private_data
, struct pxp_ctx
, fh
);
254 static struct pxp_q_data
*get_q_data(struct pxp_ctx
*ctx
,
255 enum v4l2_buf_type type
)
257 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
258 return &ctx
->q_data
[V4L2_M2M_SRC
];
260 return &ctx
->q_data
[V4L2_M2M_DST
];
263 static inline u32
pxp_read(struct pxp_dev
*dev
, u32 reg
)
267 regmap_read(dev
->regmap
, reg
, &value
);
272 static inline void pxp_write(struct pxp_dev
*dev
, u32 reg
, u32 value
)
274 regmap_write(dev
->regmap
, reg
, value
);
277 static u32
pxp_v4l2_pix_fmt_to_ps_format(u32 v4l2_pix_fmt
)
279 switch (v4l2_pix_fmt
) {
280 case V4L2_PIX_FMT_XBGR32
: return BV_PXP_PS_CTRL_FORMAT__RGB888
;
281 case V4L2_PIX_FMT_RGB555
: return BV_PXP_PS_CTRL_FORMAT__RGB555
;
282 case V4L2_PIX_FMT_RGB444
: return BV_PXP_PS_CTRL_FORMAT__RGB444
;
283 case V4L2_PIX_FMT_RGB565
: return BV_PXP_PS_CTRL_FORMAT__RGB565
;
284 case V4L2_PIX_FMT_VUYX32
: return BV_PXP_PS_CTRL_FORMAT__YUV1P444
;
285 case V4L2_PIX_FMT_UYVY
: return BV_PXP_PS_CTRL_FORMAT__UYVY1P422
;
286 case V4L2_PIX_FMT_YUYV
: return BM_PXP_PS_CTRL_WB_SWAP
|
287 BV_PXP_PS_CTRL_FORMAT__UYVY1P422
;
288 case V4L2_PIX_FMT_VYUY
: return BV_PXP_PS_CTRL_FORMAT__VYUY1P422
;
289 case V4L2_PIX_FMT_YVYU
: return BM_PXP_PS_CTRL_WB_SWAP
|
290 BV_PXP_PS_CTRL_FORMAT__VYUY1P422
;
291 case V4L2_PIX_FMT_GREY
: return BV_PXP_PS_CTRL_FORMAT__Y8
;
293 case V4L2_PIX_FMT_Y4
: return BV_PXP_PS_CTRL_FORMAT__Y4
;
294 case V4L2_PIX_FMT_NV16
: return BV_PXP_PS_CTRL_FORMAT__YUV2P422
;
295 case V4L2_PIX_FMT_NV12
: return BV_PXP_PS_CTRL_FORMAT__YUV2P420
;
296 case V4L2_PIX_FMT_NV21
: return BV_PXP_PS_CTRL_FORMAT__YVU2P420
;
297 case V4L2_PIX_FMT_NV61
: return BV_PXP_PS_CTRL_FORMAT__YVU2P422
;
298 case V4L2_PIX_FMT_YUV422P
: return BV_PXP_PS_CTRL_FORMAT__YUV422
;
299 case V4L2_PIX_FMT_YUV420
: return BV_PXP_PS_CTRL_FORMAT__YUV420
;
303 static u32
pxp_v4l2_pix_fmt_to_out_format(u32 v4l2_pix_fmt
)
305 switch (v4l2_pix_fmt
) {
306 case V4L2_PIX_FMT_XBGR32
: return BV_PXP_OUT_CTRL_FORMAT__RGB888
;
307 case V4L2_PIX_FMT_ABGR32
: return BV_PXP_OUT_CTRL_FORMAT__ARGB8888
;
308 case V4L2_PIX_FMT_BGR24
: return BV_PXP_OUT_CTRL_FORMAT__RGB888P
;
309 /* Missing V4L2 pixel formats for ARGB1555 and ARGB4444 */
310 case V4L2_PIX_FMT_RGB555
: return BV_PXP_OUT_CTRL_FORMAT__RGB555
;
311 case V4L2_PIX_FMT_RGB444
: return BV_PXP_OUT_CTRL_FORMAT__RGB444
;
312 case V4L2_PIX_FMT_RGB565
: return BV_PXP_OUT_CTRL_FORMAT__RGB565
;
313 case V4L2_PIX_FMT_VUYA32
:
314 case V4L2_PIX_FMT_VUYX32
: return BV_PXP_OUT_CTRL_FORMAT__YUV1P444
;
315 case V4L2_PIX_FMT_UYVY
: return BV_PXP_OUT_CTRL_FORMAT__UYVY1P422
;
316 case V4L2_PIX_FMT_VYUY
: return BV_PXP_OUT_CTRL_FORMAT__VYUY1P422
;
317 case V4L2_PIX_FMT_GREY
: return BV_PXP_OUT_CTRL_FORMAT__Y8
;
319 case V4L2_PIX_FMT_Y4
: return BV_PXP_OUT_CTRL_FORMAT__Y4
;
320 case V4L2_PIX_FMT_NV16
: return BV_PXP_OUT_CTRL_FORMAT__YUV2P422
;
321 case V4L2_PIX_FMT_NV12
: return BV_PXP_OUT_CTRL_FORMAT__YUV2P420
;
322 case V4L2_PIX_FMT_NV61
: return BV_PXP_OUT_CTRL_FORMAT__YVU2P422
;
323 case V4L2_PIX_FMT_NV21
: return BV_PXP_OUT_CTRL_FORMAT__YVU2P420
;
327 static bool pxp_v4l2_pix_fmt_is_yuv(u32 v4l2_pix_fmt
)
329 switch (v4l2_pix_fmt
) {
330 case V4L2_PIX_FMT_VUYA32
:
331 case V4L2_PIX_FMT_VUYX32
:
332 case V4L2_PIX_FMT_UYVY
:
333 case V4L2_PIX_FMT_YUYV
:
334 case V4L2_PIX_FMT_VYUY
:
335 case V4L2_PIX_FMT_YVYU
:
336 case V4L2_PIX_FMT_NV16
:
337 case V4L2_PIX_FMT_NV12
:
338 case V4L2_PIX_FMT_NV61
:
339 case V4L2_PIX_FMT_NV21
:
340 case V4L2_PIX_FMT_YUV420
:
341 case V4L2_PIX_FMT_YUV422P
:
342 case V4L2_PIX_FMT_GREY
:
343 case V4L2_PIX_FMT_Y4
:
350 static void pxp_setup_csc(struct pxp_ctx
*ctx
)
352 struct pxp_dev
*dev
= ctx
->dev
;
353 enum v4l2_ycbcr_encoding ycbcr_enc
;
354 enum v4l2_quantization quantization
;
356 if (pxp_v4l2_pix_fmt_is_yuv(ctx
->q_data
[V4L2_M2M_SRC
].fmt
->fourcc
) &&
357 !pxp_v4l2_pix_fmt_is_yuv(ctx
->q_data
[V4L2_M2M_DST
].fmt
->fourcc
)) {
359 * CSC1 YUV/YCbCr to RGB conversion is implemented as follows:
361 * |R| |C0 0 C1| |Y + Yoffset |
362 * |G| = |C0 C3 C2| * |Cb + UVoffset|
363 * |B| |C0 C4 0 | |Cr + UVoffset|
365 * Results are clamped to 0..255.
367 * BT.601 limited range:
369 * |R| |1.1644 0.0000 1.5960| |Y - 16 |
370 * |G| = |1.1644 -0.3917 -0.8129| * |Cb - 128|
371 * |B| |1.1644 2.0172 0.0000| |Cr - 128|
373 static const u32 csc1_coef_bt601_lim
[3] = {
374 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
375 BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
376 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
377 BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
378 BF_PXP_CSC1_COEF1_C1(0x198) | /* 1.5938 (-0.23 %) */
379 BF_PXP_CSC1_COEF1_C4(0x204), /* 2.0156 (-0.16 %) */
380 BF_PXP_CSC1_COEF2_C2(0x730) | /* -0.8125 (+0.04 %) */
381 BF_PXP_CSC1_COEF2_C3(0x79c), /* -0.3906 (+0.11 %) */
386 * |R| |1.0000 0.0000 1.4020| |Y + 0 |
387 * |G| = |1.0000 -0.3441 -0.7141| * |Cb - 128|
388 * |B| |1.0000 1.7720 0.0000| |Cr - 128|
390 static const u32 csc1_coef_bt601_full
[3] = {
391 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
392 BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
393 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
394 BF_PXP_CSC1_COEF0_Y_OFFSET(0),
395 BF_PXP_CSC1_COEF1_C1(0x166) | /* 1.3984 (-0.36 %) */
396 BF_PXP_CSC1_COEF1_C4(0x1c5), /* 1.7695 (-0.25 %) */
397 BF_PXP_CSC1_COEF2_C2(0x74a) | /* -0.7109 (+0.32 %) */
398 BF_PXP_CSC1_COEF2_C3(0x7a8), /* -0.3438 (+0.04 %) */
401 * Rec.709 limited range:
403 * |R| |1.1644 0.0000 1.7927| |Y - 16 |
404 * |G| = |1.1644 -0.2132 -0.5329| * |Cb - 128|
405 * |B| |1.1644 2.1124 0.0000| |Cr - 128|
407 static const u32 csc1_coef_rec709_lim
[3] = {
408 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
409 BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
410 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
411 BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
412 BF_PXP_CSC1_COEF1_C1(0x1ca) | /* 1.7891 (-0.37 %) */
413 BF_PXP_CSC1_COEF1_C4(0x21c), /* 2.1094 (-0.30 %) */
414 BF_PXP_CSC1_COEF2_C2(0x778) | /* -0.5312 (+0.16 %) */
415 BF_PXP_CSC1_COEF2_C3(0x7ca), /* -0.2109 (+0.23 %) */
418 * Rec.709 full range:
420 * |R| |1.0000 0.0000 1.5748| |Y + 0 |
421 * |G| = |1.0000 -0.1873 -0.4681| * |Cb - 128|
422 * |B| |1.0000 1.8556 0.0000| |Cr - 128|
424 static const u32 csc1_coef_rec709_full
[3] = {
425 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
426 BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
427 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
428 BF_PXP_CSC1_COEF0_Y_OFFSET(0),
429 BF_PXP_CSC1_COEF1_C1(0x193) | /* 1.5742 (-0.06 %) */
430 BF_PXP_CSC1_COEF1_C4(0x1db), /* 1.8555 (-0.01 %) */
431 BF_PXP_CSC1_COEF2_C2(0x789) | /* -0.4648 (+0.33 %) */
432 BF_PXP_CSC1_COEF2_C3(0x7d1), /* -0.1836 (+0.37 %) */
435 * BT.2020 limited range:
437 * |R| |1.1644 0.0000 1.6787| |Y - 16 |
438 * |G| = |1.1644 -0.1874 -0.6505| * |Cb - 128|
439 * |B| |1.1644 2.1418 0.0000| |Cr - 128|
441 static const u32 csc1_coef_bt2020_lim
[3] = {
442 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
443 BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
444 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
445 BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
446 BF_PXP_CSC1_COEF1_C1(0x1ad) | /* 1.6758 (-0.29 %) */
447 BF_PXP_CSC1_COEF1_C4(0x224), /* 2.1406 (-0.11 %) */
448 BF_PXP_CSC1_COEF2_C2(0x75a) | /* -0.6484 (+0.20 %) */
449 BF_PXP_CSC1_COEF2_C3(0x7d1), /* -0.1836 (+0.38 %) */
452 * BT.2020 full range:
454 * |R| |1.0000 0.0000 1.4746| |Y + 0 |
455 * |G| = |1.0000 -0.1646 -0.5714| * |Cb - 128|
456 * |B| |1.0000 1.8814 0.0000| |Cr - 128|
458 static const u32 csc1_coef_bt2020_full
[3] = {
459 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
460 BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
461 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
462 BF_PXP_CSC1_COEF0_Y_OFFSET(0),
463 BF_PXP_CSC1_COEF1_C1(0x179) | /* 1.4727 (-0.19 %) */
464 BF_PXP_CSC1_COEF1_C4(0x1e1), /* 1.8789 (-0.25 %) */
465 BF_PXP_CSC1_COEF2_C2(0x76e) | /* -0.5703 (+0.11 %) */
466 BF_PXP_CSC1_COEF2_C3(0x7d6), /* -0.1641 (+0.05 %) */
469 * SMPTE 240m limited range:
471 * |R| |1.1644 0.0000 1.7937| |Y - 16 |
472 * |G| = |1.1644 -0.2565 -0.5427| * |Cb - 128|
473 * |B| |1.1644 2.0798 0.0000| |Cr - 128|
475 static const u32 csc1_coef_smpte240m_lim
[3] = {
476 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
477 BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
478 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
479 BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
480 BF_PXP_CSC1_COEF1_C1(0x1cb) | /* 1.7930 (-0.07 %) */
481 BF_PXP_CSC1_COEF1_C4(0x214), /* 2.0781 (-0.17 %) */
482 BF_PXP_CSC1_COEF2_C2(0x776) | /* -0.5391 (+0.36 %) */
483 BF_PXP_CSC1_COEF2_C3(0x7bf), /* -0.2539 (+0.26 %) */
486 * SMPTE 240m full range:
488 * |R| |1.0000 0.0000 1.5756| |Y + 0 |
489 * |G| = |1.0000 -0.2253 -0.4767| * |Cb - 128|
490 * |B| |1.0000 1.8270 0.0000| |Cr - 128|
492 static const u32 csc1_coef_smpte240m_full
[3] = {
493 BM_PXP_CSC1_COEF0_YCBCR_MODE
|
494 BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
495 BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
496 BF_PXP_CSC1_COEF0_Y_OFFSET(0),
497 BF_PXP_CSC1_COEF1_C1(0x193) | /* 1.5742 (-0.14 %) */
498 BF_PXP_CSC1_COEF1_C4(0x1d3), /* 1.8242 (-0.28 %) */
499 BF_PXP_CSC1_COEF2_C2(0x786) | /* -0.4766 (+0.01 %) */
500 BF_PXP_CSC1_COEF2_C3(0x7c7), /* -0.2227 (+0.26 %) */
502 const u32
*csc1_coef
;
504 ycbcr_enc
= ctx
->q_data
[V4L2_M2M_SRC
].ycbcr_enc
;
505 quantization
= ctx
->q_data
[V4L2_M2M_SRC
].quant
;
507 if (ycbcr_enc
== V4L2_YCBCR_ENC_601
) {
508 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
509 csc1_coef
= csc1_coef_bt601_full
;
511 csc1_coef
= csc1_coef_bt601_lim
;
512 } else if (ycbcr_enc
== V4L2_YCBCR_ENC_709
) {
513 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
514 csc1_coef
= csc1_coef_rec709_full
;
516 csc1_coef
= csc1_coef_rec709_lim
;
517 } else if (ycbcr_enc
== V4L2_YCBCR_ENC_BT2020
) {
518 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
519 csc1_coef
= csc1_coef_bt2020_full
;
521 csc1_coef
= csc1_coef_bt2020_lim
;
523 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
524 csc1_coef
= csc1_coef_smpte240m_full
;
526 csc1_coef
= csc1_coef_smpte240m_lim
;
529 pxp_write(dev
, HW_PXP_CSC1_COEF0
, csc1_coef
[0]);
530 pxp_write(dev
, HW_PXP_CSC1_COEF1
, csc1_coef
[1]);
531 pxp_write(dev
, HW_PXP_CSC1_COEF2
, csc1_coef
[2]);
533 pxp_write(dev
, HW_PXP_CSC1_COEF0
, BM_PXP_CSC1_COEF0_BYPASS
);
536 if (!pxp_v4l2_pix_fmt_is_yuv(ctx
->q_data
[V4L2_M2M_SRC
].fmt
->fourcc
) &&
537 pxp_v4l2_pix_fmt_is_yuv(ctx
->q_data
[V4L2_M2M_DST
].fmt
->fourcc
)) {
539 * CSC2 RGB to YUV/YCbCr conversion is implemented as follows:
541 * |Y | |A1 A2 A3| |R| |D1|
542 * |Cb| = |B1 B2 B3| * |G| + |D2|
543 * |Cr| |C1 C2 C3| |B| |D3|
545 * Results are clamped to 0..255.
547 * BT.601 limited range:
549 * |Y | | 0.2568 0.5041 0.0979| |R| |16 |
550 * |Cb| = |-0.1482 -0.2910 0.4392| * |G| + |128|
551 * |Cr| | 0.4392 0.4392 -0.3678| |B| |128|
553 static const u32 csc2_coef_bt601_lim
[6] = {
554 BF_PXP_CSC2_COEF0_A2(0x081) | /* 0.5039 (-0.02 %) */
555 BF_PXP_CSC2_COEF0_A1(0x041), /* 0.2539 (-0.29 %) */
556 BF_PXP_CSC2_COEF1_B1(0x7db) | /* -0.1445 (+0.37 %) */
557 BF_PXP_CSC2_COEF1_A3(0x019), /* 0.0977 (-0.02 %) */
558 BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
559 BF_PXP_CSC2_COEF2_B2(0x7b6), /* -0.2891 (+0.20 %) */
560 BF_PXP_CSC2_COEF3_C2(0x7a2) | /* -0.3672 (+0.06 %) */
561 BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
562 BF_PXP_CSC2_COEF4_D1(16) |
563 BF_PXP_CSC2_COEF4_C3(0x7ee), /* -0.0703 (+0.11 %) */
564 BF_PXP_CSC2_COEF5_D3(128) |
565 BF_PXP_CSC2_COEF5_D2(128),
570 * |Y | | 0.2990 0.5870 0.1140| |R| |0 |
571 * |Cb| = |-0.1687 -0.3313 0.5000| * |G| + |128|
572 * |Cr| | 0.5000 0.5000 -0.4187| |B| |128|
574 static const u32 csc2_coef_bt601_full
[6] = {
575 BF_PXP_CSC2_COEF0_A2(0x096) | /* 0.5859 (-0.11 %) */
576 BF_PXP_CSC2_COEF0_A1(0x04c), /* 0.2969 (-0.21 %) */
577 BF_PXP_CSC2_COEF1_B1(0x7d5) | /* -0.1680 (+0.07 %) */
578 BF_PXP_CSC2_COEF1_A3(0x01d), /* 0.1133 (-0.07 %) */
579 BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
580 BF_PXP_CSC2_COEF2_B2(0x7ac), /* -0.3281 (+0.32 %) */
581 BF_PXP_CSC2_COEF3_C2(0x795) | /* -0.4180 (+0.07 %) */
582 BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
583 BF_PXP_CSC2_COEF4_D1(0) |
584 BF_PXP_CSC2_COEF4_C3(0x7ec), /* -0.0781 (+0.32 %) */
585 BF_PXP_CSC2_COEF5_D3(128) |
586 BF_PXP_CSC2_COEF5_D2(128),
589 * Rec.709 limited range:
591 * |Y | | 0.1826 0.6142 0.0620| |R| |16 |
592 * |Cb| = |-0.1007 -0.3385 0.4392| * |G| + |128|
593 * |Cr| | 0.4392 0.4392 -0.3990| |B| |128|
595 static const u32 csc2_coef_rec709_lim
[6] = {
596 BF_PXP_CSC2_COEF0_A2(0x09d) | /* 0.6133 (-0.09 %) */
597 BF_PXP_CSC2_COEF0_A1(0x02e), /* 0.1797 (-0.29 %) */
598 BF_PXP_CSC2_COEF1_B1(0x7e7) | /* -0.0977 (+0.30 %) */
599 BF_PXP_CSC2_COEF1_A3(0x00f), /* 0.0586 (-0.34 %) */
600 BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
601 BF_PXP_CSC2_COEF2_B2(0x7aa), /* -0.3359 (+0.26 %) */
602 BF_PXP_CSC2_COEF3_C2(0x79a) | /* -0.3984 (+0.05 %) */
603 BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
604 BF_PXP_CSC2_COEF4_D1(16) |
605 BF_PXP_CSC2_COEF4_C3(0x7f6), /* -0.0391 (+0.12 %) */
606 BF_PXP_CSC2_COEF5_D3(128) |
607 BF_PXP_CSC2_COEF5_D2(128),
610 * Rec.709 full range:
612 * |Y | | 0.2126 0.7152 0.0722| |R| |0 |
613 * |Cb| = |-0.1146 -0.3854 0.5000| * |G| + |128|
614 * |Cr| | 0.5000 0.5000 -0.4542| |B| |128|
616 static const u32 csc2_coef_rec709_full
[6] = {
617 BF_PXP_CSC2_COEF0_A2(0x0b7) | /* 0.7148 (-0.04 %) */
618 BF_PXP_CSC2_COEF0_A1(0x036), /* 0.2109 (-0.17 %) */
619 BF_PXP_CSC2_COEF1_B1(0x7e3) | /* -0.1133 (+0.13 %) */
620 BF_PXP_CSC2_COEF1_A3(0x012), /* 0.0703 (-0.19 %) */
621 BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
622 BF_PXP_CSC2_COEF2_B2(0x79e), /* -0.3828 (+0.26 %) */
623 BF_PXP_CSC2_COEF3_C2(0x78c) | /* -0.4531 (+0.11 %) */
624 BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
625 BF_PXP_CSC2_COEF4_D1(0) |
626 BF_PXP_CSC2_COEF4_C3(0x7f5), /* -0.0430 (+0.28 %) */
627 BF_PXP_CSC2_COEF5_D3(128) |
628 BF_PXP_CSC2_COEF5_D2(128),
631 * BT.2020 limited range:
633 * |Y | | 0.2256 0.5823 0.0509| |R| |16 |
634 * |Cb| = |-0.1226 -0.3166 0.4392| * |G| + |128|
635 * |Cr| | 0.4392 0.4392 -0.4039| |B| |128|
637 static const u32 csc2_coef_bt2020_lim
[6] = {
638 BF_PXP_CSC2_COEF0_A2(0x095) | /* 0.5820 (-0.03 %) */
639 BF_PXP_CSC2_COEF0_A1(0x039), /* 0.2227 (-0.30 %) */
640 BF_PXP_CSC2_COEF1_B1(0x7e1) | /* -0.1211 (+0.15 %) */
641 BF_PXP_CSC2_COEF1_A3(0x00d), /* 0.0508 (-0.01 %) */
642 BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
643 BF_PXP_CSC2_COEF2_B2(0x7af), /* -0.3164 (+0.02 %) */
644 BF_PXP_CSC2_COEF3_C2(0x799) | /* -0.4023 (+0.16 %) */
645 BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
646 BF_PXP_CSC2_COEF4_D1(16) |
647 BF_PXP_CSC2_COEF4_C3(0x7f7), /* -0.0352 (+0.02 %) */
648 BF_PXP_CSC2_COEF5_D3(128) |
649 BF_PXP_CSC2_COEF5_D2(128),
652 * BT.2020 full range:
654 * |Y | | 0.2627 0.6780 0.0593| |R| |0 |
655 * |Cb| = |-0.1396 -0.3604 0.5000| * |G| + |128|
656 * |Cr| | 0.5000 0.5000 -0.4598| |B| |128|
658 static const u32 csc2_coef_bt2020_full
[6] = {
659 BF_PXP_CSC2_COEF0_A2(0x0ad) | /* 0.6758 (-0.22 %) */
660 BF_PXP_CSC2_COEF0_A1(0x043), /* 0.2617 (-0.10 %) */
661 BF_PXP_CSC2_COEF1_B1(0x7dd) | /* -0.1367 (+0.29 %) */
662 BF_PXP_CSC2_COEF1_A3(0x00f), /* 0.0586 (-0.07 %) */
663 BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
664 BF_PXP_CSC2_COEF2_B2(0x7a4), /* -0.3594 (+0.10 %) */
665 BF_PXP_CSC2_COEF3_C2(0x78b) | /* -0.4570 (+0.28 %) */
666 BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
667 BF_PXP_CSC2_COEF4_D1(0) |
668 BF_PXP_CSC2_COEF4_C3(0x7f6), /* -0.0391 (+0.11 %) */
669 BF_PXP_CSC2_COEF5_D3(128) |
670 BF_PXP_CSC2_COEF5_D2(128),
673 * SMPTE 240m limited range:
675 * |Y | | 0.1821 0.6020 0.0747| |R| |16 |
676 * |Cb| = |-0.1019 -0.3373 0.4392| * |G| + |128|
677 * |Cr| | 0.4392 0.4392 -0.3909| |B| |128|
679 static const u32 csc2_coef_smpte240m_lim
[6] = {
680 BF_PXP_CSC2_COEF0_A2(0x09a) | /* 0.6016 (-0.05 %) */
681 BF_PXP_CSC2_COEF0_A1(0x02e), /* 0.1797 (-0.24 %) */
682 BF_PXP_CSC2_COEF1_B1(0x7e6) | /* -0.1016 (+0.03 %) */
683 BF_PXP_CSC2_COEF1_A3(0x013), /* 0.0742 (-0.05 %) */
684 BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
685 BF_PXP_CSC2_COEF2_B2(0x7aa), /* -0.3359 (+0.14 %) */
686 BF_PXP_CSC2_COEF3_C2(0x79c) | /* -0.3906 (+0.03 %) */
687 BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
688 BF_PXP_CSC2_COEF4_D1(16) |
689 BF_PXP_CSC2_COEF4_C3(0x7f4), /* -0.0469 (+0.14 %) */
690 BF_PXP_CSC2_COEF5_D3(128) |
691 BF_PXP_CSC2_COEF5_D2(128),
694 * SMPTE 240m full range:
696 * |Y | | 0.2120 0.7010 0.0870| |R| |0 |
697 * |Cb| = |-0.1160 -0.3840 0.5000| * |G| + |128|
698 * |Cr| | 0.5000 0.5000 -0.4450| |B| |128|
700 static const u32 csc2_coef_smpte240m_full
[6] = {
701 BF_PXP_CSC2_COEF0_A2(0x0b3) | /* 0.6992 (-0.18 %) */
702 BF_PXP_CSC2_COEF0_A1(0x036), /* 0.2109 (-0.11 %) */
703 BF_PXP_CSC2_COEF1_B1(0x7e3) | /* -0.1133 (+0.27 %) */
704 BF_PXP_CSC2_COEF1_A3(0x016), /* 0.0859 (-0.11 %) */
705 BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
706 BF_PXP_CSC2_COEF2_B2(0x79e), /* -0.3828 (+0.12 %) */
707 BF_PXP_CSC2_COEF3_C2(0x78f) | /* -0.4414 (+0.36 %) */
708 BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
709 BF_PXP_CSC2_COEF4_D1(0) |
710 BF_PXP_CSC2_COEF4_C3(0x7f2), /* -0.0547 (+0.03 %) */
711 BF_PXP_CSC2_COEF5_D3(128) |
712 BF_PXP_CSC2_COEF5_D2(128),
714 const u32
*csc2_coef
;
717 ycbcr_enc
= ctx
->q_data
[V4L2_M2M_DST
].ycbcr_enc
;
718 quantization
= ctx
->q_data
[V4L2_M2M_DST
].quant
;
720 if (ycbcr_enc
== V4L2_YCBCR_ENC_601
) {
721 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
722 csc2_coef
= csc2_coef_bt601_full
;
724 csc2_coef
= csc2_coef_bt601_lim
;
725 } else if (ycbcr_enc
== V4L2_YCBCR_ENC_709
) {
726 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
727 csc2_coef
= csc2_coef_rec709_full
;
729 csc2_coef
= csc2_coef_rec709_lim
;
730 } else if (ycbcr_enc
== V4L2_YCBCR_ENC_BT2020
) {
731 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
732 csc2_coef
= csc2_coef_bt2020_full
;
734 csc2_coef
= csc2_coef_bt2020_lim
;
736 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
)
737 csc2_coef
= csc2_coef_smpte240m_full
;
739 csc2_coef
= csc2_coef_smpte240m_lim
;
741 if (quantization
== V4L2_QUANTIZATION_FULL_RANGE
) {
742 csc2_ctrl
= BV_PXP_CSC2_CTRL_CSC_MODE__RGB2YUV
<<
743 BP_PXP_CSC2_CTRL_CSC_MODE
;
745 csc2_ctrl
= BV_PXP_CSC2_CTRL_CSC_MODE__RGB2YCbCr
<<
746 BP_PXP_CSC2_CTRL_CSC_MODE
;
749 pxp_write(dev
, HW_PXP_CSC2_CTRL
, csc2_ctrl
);
750 pxp_write(dev
, HW_PXP_CSC2_COEF0
, csc2_coef
[0]);
751 pxp_write(dev
, HW_PXP_CSC2_COEF1
, csc2_coef
[1]);
752 pxp_write(dev
, HW_PXP_CSC2_COEF2
, csc2_coef
[2]);
753 pxp_write(dev
, HW_PXP_CSC2_COEF3
, csc2_coef
[3]);
754 pxp_write(dev
, HW_PXP_CSC2_COEF4
, csc2_coef
[4]);
755 pxp_write(dev
, HW_PXP_CSC2_COEF5
, csc2_coef
[5]);
757 pxp_write(dev
, HW_PXP_CSC2_CTRL
, BM_PXP_CSC2_CTRL_BYPASS
);
761 static u32
pxp_imx6ull_data_path_ctrl0(struct pxp_ctx
*ctx
)
766 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX15_SEL(3);
767 /* Bypass Dithering x3CH */
768 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX14_SEL(1);
769 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX13_SEL(3);
770 /* Select Rotation */
771 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX12_SEL(0);
773 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX11_SEL(1);
774 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX10_SEL(3);
775 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX9_SEL(3);
777 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX8_SEL(0);
778 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX7_SEL(3);
779 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX6_SEL(3);
780 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX5_SEL(3);
781 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX4_SEL(3);
782 /* Bypass Rotation 2 */
783 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX3_SEL(0);
784 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX2_SEL(3);
785 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX1_SEL(3);
786 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX0_SEL(3);
791 static u32
pxp_imx7d_data_path_ctrl0(struct pxp_ctx
*ctx
)
796 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX15_SEL(3);
797 /* Select Rotation 0 */
798 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX14_SEL(0);
799 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX13_SEL(3);
800 /* Select MUX11 for Rotation 0 */
801 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX12_SEL(1);
803 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX11_SEL(1);
804 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX10_SEL(3);
805 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX9_SEL(3);
807 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX8_SEL(0);
808 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX7_SEL(3);
809 /* Select Composite Alpha Blending/Color Key 0 for CSC 2 */
810 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX6_SEL(1);
811 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX5_SEL(3);
812 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX4_SEL(3);
813 /* Bypass Rotation 1 */
814 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX3_SEL(0);
815 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX2_SEL(3);
816 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX1_SEL(3);
817 ctrl0
|= BF_PXP_DATA_PATH_CTRL0_MUX0_SEL(3);
822 static void pxp_set_data_path(struct pxp_ctx
*ctx
)
824 struct pxp_dev
*dev
= ctx
->dev
;
828 ctrl0
= dev
->pdata
->data_path_ctrl0(ctx
);
831 ctrl1
|= BF_PXP_DATA_PATH_CTRL1_MUX17_SEL(3);
832 ctrl1
|= BF_PXP_DATA_PATH_CTRL1_MUX16_SEL(3);
834 pxp_write(dev
, HW_PXP_DATA_PATH_CTRL0
, ctrl0
);
835 pxp_write(dev
, HW_PXP_DATA_PATH_CTRL1
, ctrl1
);
838 static int pxp_start(struct pxp_ctx
*ctx
, struct vb2_v4l2_buffer
*in_vb
,
839 struct vb2_v4l2_buffer
*out_vb
)
841 struct pxp_dev
*dev
= ctx
->dev
;
842 struct pxp_q_data
*q_data
;
843 u32 src_width
, src_height
, src_stride
, src_fourcc
;
844 u32 dst_width
, dst_height
, dst_stride
, dst_fourcc
;
845 dma_addr_t p_in
, p_out
;
846 u32 ctrl
, out_ctrl
, out_buf
, out_buf2
, out_pitch
, out_lrc
, out_ps_ulc
;
848 u32 ps_ctrl
, ps_buf
, ps_ubuf
, ps_vbuf
, ps_pitch
, ps_scale
, ps_offset
;
851 u32 decx
, decy
, xscale
, yscale
;
853 q_data
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
855 src_width
= ctx
->q_data
[V4L2_M2M_SRC
].width
;
856 dst_width
= ctx
->q_data
[V4L2_M2M_DST
].width
;
857 src_height
= ctx
->q_data
[V4L2_M2M_SRC
].height
;
858 dst_height
= ctx
->q_data
[V4L2_M2M_DST
].height
;
859 src_stride
= ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
;
860 dst_stride
= ctx
->q_data
[V4L2_M2M_DST
].bytesperline
;
861 src_fourcc
= ctx
->q_data
[V4L2_M2M_SRC
].fmt
->fourcc
;
862 dst_fourcc
= ctx
->q_data
[V4L2_M2M_DST
].fmt
->fourcc
;
864 p_in
= vb2_dma_contig_plane_dma_addr(&in_vb
->vb2_buf
, 0);
865 p_out
= vb2_dma_contig_plane_dma_addr(&out_vb
->vb2_buf
, 0);
867 if (!p_in
|| !p_out
) {
868 v4l2_err(&dev
->v4l2_dev
,
869 "Acquiring DMA addresses of buffers failed\n");
874 get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
)->sequence
++;
875 in_vb
->sequence
= q_data
->sequence
++;
876 out_vb
->vb2_buf
.timestamp
= in_vb
->vb2_buf
.timestamp
;
878 if (in_vb
->flags
& V4L2_BUF_FLAG_TIMECODE
)
879 out_vb
->timecode
= in_vb
->timecode
;
880 out_vb
->field
= in_vb
->field
;
881 out_vb
->flags
= in_vb
->flags
&
882 (V4L2_BUF_FLAG_TIMECODE
|
883 V4L2_BUF_FLAG_KEYFRAME
|
884 V4L2_BUF_FLAG_PFRAME
|
885 V4L2_BUF_FLAG_BFRAME
|
886 V4L2_BUF_FLAG_TSTAMP_SRC_MASK
);
889 ctrl
= BF_PXP_CTRL_VFLIP0(!!(ctx
->mode
& MEM2MEM_VFLIP
)) |
890 BF_PXP_CTRL_HFLIP0(!!(ctx
->mode
& MEM2MEM_HFLIP
)) |
891 BF_PXP_CTRL_ROTATE0(ctx
->rotation
);
892 /* Always write alpha value as V4L2_CID_ALPHA_COMPONENT */
893 out_ctrl
= BF_PXP_OUT_CTRL_ALPHA(ctx
->alpha_component
) |
894 BF_PXP_OUT_CTRL_ALPHA_OUTPUT(1) |
895 pxp_v4l2_pix_fmt_to_out_format(dst_fourcc
);
898 if (ctx
->rotation
== BV_PXP_CTRL_ROTATE0__ROT_90
||
899 ctx
->rotation
== BV_PXP_CTRL_ROTATE0__ROT_270
)
900 swap(dst_width
, dst_height
);
902 switch (dst_fourcc
) {
903 case V4L2_PIX_FMT_NV12
:
904 case V4L2_PIX_FMT_NV21
:
905 case V4L2_PIX_FMT_NV16
:
906 case V4L2_PIX_FMT_NV61
:
907 out_buf2
= out_buf
+ dst_stride
* dst_height
;
913 out_pitch
= BF_PXP_OUT_PITCH_PITCH(dst_stride
);
914 out_lrc
= BF_PXP_OUT_LRC_X(dst_width
- 1) |
915 BF_PXP_OUT_LRC_Y(dst_height
- 1);
916 /* PS covers whole output */
917 out_ps_ulc
= BF_PXP_OUT_PS_ULC_X(0) | BF_PXP_OUT_PS_ULC_Y(0);
918 out_ps_lrc
= BF_PXP_OUT_PS_LRC_X(dst_width
- 1) |
919 BF_PXP_OUT_PS_LRC_Y(dst_height
- 1);
921 as_ulc
= BF_PXP_OUT_AS_ULC_X(1) | BF_PXP_OUT_AS_ULC_Y(1);
922 as_lrc
= BF_PXP_OUT_AS_LRC_X(0) | BF_PXP_OUT_AS_LRC_Y(0);
924 decx
= (src_width
<= dst_width
) ? 0 : ilog2(src_width
/ dst_width
);
925 decy
= (src_height
<= dst_height
) ? 0 : ilog2(src_height
/ dst_height
);
926 ps_ctrl
= BF_PXP_PS_CTRL_DECX(decx
) | BF_PXP_PS_CTRL_DECY(decy
) |
927 pxp_v4l2_pix_fmt_to_ps_format(src_fourcc
);
929 y_size
= src_stride
* src_height
;
930 switch (src_fourcc
) {
931 case V4L2_PIX_FMT_YUV420
:
932 ps_ubuf
= ps_buf
+ y_size
;
933 ps_vbuf
= ps_ubuf
+ y_size
/ 4;
935 case V4L2_PIX_FMT_YUV422P
:
936 ps_ubuf
= ps_buf
+ y_size
;
937 ps_vbuf
= ps_ubuf
+ y_size
/ 2;
939 case V4L2_PIX_FMT_NV12
:
940 case V4L2_PIX_FMT_NV21
:
941 case V4L2_PIX_FMT_NV16
:
942 case V4L2_PIX_FMT_NV61
:
943 ps_ubuf
= ps_buf
+ y_size
;
946 case V4L2_PIX_FMT_GREY
:
947 case V4L2_PIX_FMT_Y4
:
949 /* In grayscale mode, ps_vbuf contents are reused as CbCr */
957 ps_pitch
= BF_PXP_PS_PITCH_PITCH(src_stride
);
959 xscale
= (src_width
>> decx
) * 0x1000 / dst_width
;
961 switch (src_fourcc
) {
962 case V4L2_PIX_FMT_UYVY
:
963 case V4L2_PIX_FMT_YUYV
:
964 case V4L2_PIX_FMT_VYUY
:
965 case V4L2_PIX_FMT_YVYU
:
966 case V4L2_PIX_FMT_NV16
:
967 case V4L2_PIX_FMT_NV12
:
968 case V4L2_PIX_FMT_NV21
:
969 case V4L2_PIX_FMT_NV61
:
970 case V4L2_PIX_FMT_YUV422P
:
971 case V4L2_PIX_FMT_YUV420
:
973 * This avoids sampling past the right edge for
974 * horizontally chroma subsampled formats.
976 xscale
= (src_width
- 2) * 0x1000 / (dst_width
- 1);
979 xscale
= (src_width
- 1) * 0x1000 / (dst_width
- 1);
984 yscale
= (src_height
>> decy
) * 0x1000 / dst_height
;
986 yscale
= (src_height
- 1) * 0x1000 / (dst_height
- 1);
987 ps_scale
= BF_PXP_PS_SCALE_YSCALE(yscale
) |
988 BF_PXP_PS_SCALE_XSCALE(xscale
);
989 ps_offset
= BF_PXP_PS_OFFSET_YOFFSET(0) | BF_PXP_PS_OFFSET_XOFFSET(0);
991 pxp_write(dev
, HW_PXP_CTRL
, ctrl
);
993 pxp_write(dev
, HW_PXP_OUT_CTRL
, out_ctrl
);
994 pxp_write(dev
, HW_PXP_OUT_BUF
, out_buf
);
995 pxp_write(dev
, HW_PXP_OUT_BUF2
, out_buf2
);
996 pxp_write(dev
, HW_PXP_OUT_PITCH
, out_pitch
);
997 pxp_write(dev
, HW_PXP_OUT_LRC
, out_lrc
);
998 pxp_write(dev
, HW_PXP_OUT_PS_ULC
, out_ps_ulc
);
999 pxp_write(dev
, HW_PXP_OUT_PS_LRC
, out_ps_lrc
);
1000 pxp_write(dev
, HW_PXP_OUT_AS_ULC
, as_ulc
);
1001 pxp_write(dev
, HW_PXP_OUT_AS_LRC
, as_lrc
);
1002 pxp_write(dev
, HW_PXP_PS_CTRL
, ps_ctrl
);
1003 pxp_write(dev
, HW_PXP_PS_BUF
, ps_buf
);
1004 pxp_write(dev
, HW_PXP_PS_UBUF
, ps_ubuf
);
1005 pxp_write(dev
, HW_PXP_PS_VBUF
, ps_vbuf
);
1006 pxp_write(dev
, HW_PXP_PS_PITCH
, ps_pitch
);
1007 pxp_write(dev
, HW_PXP_PS_BACKGROUND_0
, 0x00ffffff);
1008 pxp_write(dev
, HW_PXP_PS_SCALE
, ps_scale
);
1009 pxp_write(dev
, HW_PXP_PS_OFFSET
, ps_offset
);
1010 /* disable processed surface color keying */
1011 pxp_write(dev
, HW_PXP_PS_CLRKEYLOW_0
, 0x00ffffff);
1012 pxp_write(dev
, HW_PXP_PS_CLRKEYHIGH_0
, 0x00000000);
1014 /* disable alpha surface color keying */
1015 pxp_write(dev
, HW_PXP_AS_CLRKEYLOW_0
, 0x00ffffff);
1016 pxp_write(dev
, HW_PXP_AS_CLRKEYHIGH_0
, 0x00000000);
1022 pxp_write(dev
, HW_PXP_LUT_CTRL
, BM_PXP_LUT_CTRL_BYPASS
);
1024 pxp_set_data_path(ctx
);
1026 pxp_write(dev
, HW_PXP_IRQ_MASK
, 0xffff);
1028 /* ungate, enable PS/AS/OUT and PXP operation */
1029 pxp_write(dev
, HW_PXP_CTRL_SET
, BM_PXP_CTRL_IRQ_ENABLE
);
1030 pxp_write(dev
, HW_PXP_CTRL_SET
,
1031 BM_PXP_CTRL_ENABLE
| BM_PXP_CTRL_ENABLE_CSC2
|
1032 BM_PXP_CTRL_ENABLE_ROTATE0
| BM_PXP_CTRL_ENABLE_PS_AS_OUT
);
1037 static void pxp_job_finish(struct pxp_dev
*dev
)
1039 struct pxp_ctx
*curr_ctx
;
1040 struct vb2_v4l2_buffer
*src_vb
, *dst_vb
;
1041 unsigned long flags
;
1043 curr_ctx
= v4l2_m2m_get_curr_priv(dev
->m2m_dev
);
1045 if (curr_ctx
== NULL
) {
1046 pr_err("Instance released before the end of transaction\n");
1050 src_vb
= v4l2_m2m_src_buf_remove(curr_ctx
->fh
.m2m_ctx
);
1051 dst_vb
= v4l2_m2m_dst_buf_remove(curr_ctx
->fh
.m2m_ctx
);
1053 spin_lock_irqsave(&dev
->irqlock
, flags
);
1054 v4l2_m2m_buf_done(src_vb
, VB2_BUF_STATE_DONE
);
1055 v4l2_m2m_buf_done(dst_vb
, VB2_BUF_STATE_DONE
);
1056 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1058 dprintk(curr_ctx
->dev
, "Finishing transaction\n");
1059 v4l2_m2m_job_finish(dev
->m2m_dev
, curr_ctx
->fh
.m2m_ctx
);
1065 static void pxp_device_run(void *priv
)
1067 struct pxp_ctx
*ctx
= priv
;
1068 struct vb2_v4l2_buffer
*src_buf
, *dst_buf
;
1070 src_buf
= v4l2_m2m_next_src_buf(ctx
->fh
.m2m_ctx
);
1071 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
1073 pxp_start(ctx
, src_buf
, dst_buf
);
1076 static int pxp_job_ready(void *priv
)
1078 struct pxp_ctx
*ctx
= priv
;
1080 if (v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
) < 1 ||
1081 v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
) < 1) {
1082 dprintk(ctx
->dev
, "Not enough buffers available\n");
1089 static void pxp_job_abort(void *priv
)
1091 struct pxp_ctx
*ctx
= priv
;
1093 /* Will cancel the transaction in the next interrupt handler */
1100 static irqreturn_t
pxp_irq_handler(int irq
, void *dev_id
)
1102 struct pxp_dev
*dev
= dev_id
;
1105 stat
= pxp_read(dev
, HW_PXP_STAT
);
1107 if (stat
& BM_PXP_STAT_IRQ0
) {
1108 /* we expect x = 0, y = height, irq0 = 1 */
1109 if (stat
& ~(BM_PXP_STAT_BLOCKX
| BM_PXP_STAT_BLOCKY
|
1111 dprintk(dev
, "%s: stat = 0x%08x\n", __func__
, stat
);
1112 pxp_write(dev
, HW_PXP_STAT_CLR
, BM_PXP_STAT_IRQ0
);
1114 pxp_job_finish(dev
);
1116 u32 irq
= pxp_read(dev
, HW_PXP_IRQ
);
1118 dprintk(dev
, "%s: stat = 0x%08x\n", __func__
, stat
);
1119 dprintk(dev
, "%s: irq = 0x%08x\n", __func__
, irq
);
1121 pxp_write(dev
, HW_PXP_IRQ_CLR
, irq
);
1130 static int pxp_querycap(struct file
*file
, void *priv
,
1131 struct v4l2_capability
*cap
)
1133 strscpy(cap
->driver
, MEM2MEM_NAME
, sizeof(cap
->driver
));
1134 strscpy(cap
->card
, MEM2MEM_NAME
, sizeof(cap
->card
));
1138 static int pxp_enum_fmt(struct v4l2_fmtdesc
*f
, u32 type
)
1141 struct pxp_fmt
*fmt
;
1145 for (i
= 0; i
< NUM_FORMATS
; ++i
) {
1146 if (formats
[i
].types
& type
) {
1147 /* index-th format of type type found ? */
1148 if (num
== f
->index
)
1151 * Correct type but haven't reached our index yet,
1152 * just increment per-type index
1158 if (i
< NUM_FORMATS
) {
1161 f
->pixelformat
= fmt
->fourcc
;
1165 /* Format not found */
1169 static int pxp_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1170 struct v4l2_fmtdesc
*f
)
1172 return pxp_enum_fmt(f
, MEM2MEM_CAPTURE
);
1175 static int pxp_enum_fmt_vid_out(struct file
*file
, void *priv
,
1176 struct v4l2_fmtdesc
*f
)
1178 return pxp_enum_fmt(f
, MEM2MEM_OUTPUT
);
1181 static int pxp_g_fmt(struct pxp_ctx
*ctx
, struct v4l2_format
*f
)
1183 struct vb2_queue
*vq
;
1184 struct pxp_q_data
*q_data
;
1186 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
1190 q_data
= get_q_data(ctx
, f
->type
);
1192 f
->fmt
.pix
.width
= q_data
->width
;
1193 f
->fmt
.pix
.height
= q_data
->height
;
1194 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1195 f
->fmt
.pix
.pixelformat
= q_data
->fmt
->fourcc
;
1196 f
->fmt
.pix
.bytesperline
= q_data
->bytesperline
;
1197 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
1198 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
1199 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
1200 f
->fmt
.pix
.ycbcr_enc
= q_data
->ycbcr_enc
;
1201 f
->fmt
.pix
.quantization
= q_data
->quant
;
1206 static int pxp_g_fmt_vid_out(struct file
*file
, void *priv
,
1207 struct v4l2_format
*f
)
1209 return pxp_g_fmt(file2ctx(file
), f
);
1212 static int pxp_g_fmt_vid_cap(struct file
*file
, void *priv
,
1213 struct v4l2_format
*f
)
1215 return pxp_g_fmt(file2ctx(file
), f
);
1218 static inline u32
pxp_bytesperline(struct pxp_fmt
*fmt
, u32 width
)
1220 switch (fmt
->fourcc
) {
1221 case V4L2_PIX_FMT_YUV420
:
1222 case V4L2_PIX_FMT_NV12
:
1223 case V4L2_PIX_FMT_NV21
:
1224 case V4L2_PIX_FMT_YUV422P
:
1225 case V4L2_PIX_FMT_NV16
:
1226 case V4L2_PIX_FMT_NV61
:
1229 return (width
* fmt
->depth
) >> 3;
1233 static inline u32
pxp_sizeimage(struct pxp_fmt
*fmt
, u32 width
, u32 height
)
1235 return (fmt
->depth
* width
* height
) >> 3;
1238 static int pxp_try_fmt(struct v4l2_format
*f
, struct pxp_fmt
*fmt
)
1240 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, MAX_W
, ALIGN_W
,
1241 &f
->fmt
.pix
.height
, MIN_H
, MAX_H
, ALIGN_H
, 0);
1243 f
->fmt
.pix
.bytesperline
= pxp_bytesperline(fmt
, f
->fmt
.pix
.width
);
1244 f
->fmt
.pix
.sizeimage
= pxp_sizeimage(fmt
, f
->fmt
.pix
.width
,
1246 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1252 pxp_fixup_colorimetry_cap(struct pxp_ctx
*ctx
, u32 dst_fourcc
,
1253 enum v4l2_ycbcr_encoding
*ycbcr_enc
,
1254 enum v4l2_quantization
*quantization
)
1256 bool dst_is_yuv
= pxp_v4l2_pix_fmt_is_yuv(dst_fourcc
);
1258 if (pxp_v4l2_pix_fmt_is_yuv(ctx
->q_data
[V4L2_M2M_SRC
].fmt
->fourcc
) ==
1261 * There is no support for conversion between different YCbCr
1262 * encodings or between RGB limited and full range.
1264 *ycbcr_enc
= ctx
->q_data
[V4L2_M2M_SRC
].ycbcr_enc
;
1265 *quantization
= ctx
->q_data
[V4L2_M2M_SRC
].quant
;
1267 *ycbcr_enc
= V4L2_MAP_YCBCR_ENC_DEFAULT(ctx
->colorspace
);
1268 *quantization
= V4L2_MAP_QUANTIZATION_DEFAULT(!dst_is_yuv
,
1274 static int pxp_try_fmt_vid_cap(struct file
*file
, void *priv
,
1275 struct v4l2_format
*f
)
1277 struct pxp_fmt
*fmt
;
1278 struct pxp_ctx
*ctx
= file2ctx(file
);
1280 fmt
= find_format(f
->fmt
.pix
.pixelformat
);
1282 f
->fmt
.pix
.pixelformat
= formats
[0].fourcc
;
1283 fmt
= find_format(f
->fmt
.pix
.pixelformat
);
1285 if (!(fmt
->types
& MEM2MEM_CAPTURE
)) {
1286 v4l2_err(&ctx
->dev
->v4l2_dev
,
1287 "Fourcc format (0x%08x) invalid.\n",
1288 f
->fmt
.pix
.pixelformat
);
1292 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
1293 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
1295 pxp_fixup_colorimetry_cap(ctx
, fmt
->fourcc
,
1296 &f
->fmt
.pix
.ycbcr_enc
,
1297 &f
->fmt
.pix
.quantization
);
1299 return pxp_try_fmt(f
, fmt
);
1302 static int pxp_try_fmt_vid_out(struct file
*file
, void *priv
,
1303 struct v4l2_format
*f
)
1305 struct pxp_fmt
*fmt
;
1306 struct pxp_ctx
*ctx
= file2ctx(file
);
1308 fmt
= find_format(f
->fmt
.pix
.pixelformat
);
1310 f
->fmt
.pix
.pixelformat
= formats
[0].fourcc
;
1311 fmt
= find_format(f
->fmt
.pix
.pixelformat
);
1313 if (!(fmt
->types
& MEM2MEM_OUTPUT
)) {
1314 v4l2_err(&ctx
->dev
->v4l2_dev
,
1315 "Fourcc format (0x%08x) invalid.\n",
1316 f
->fmt
.pix
.pixelformat
);
1320 if (!f
->fmt
.pix
.colorspace
)
1321 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
1323 return pxp_try_fmt(f
, fmt
);
1326 static int pxp_s_fmt(struct pxp_ctx
*ctx
, struct v4l2_format
*f
)
1328 struct pxp_q_data
*q_data
;
1329 struct vb2_queue
*vq
;
1331 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
1335 q_data
= get_q_data(ctx
, f
->type
);
1339 if (vb2_is_busy(vq
)) {
1340 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
1344 q_data
->fmt
= find_format(f
->fmt
.pix
.pixelformat
);
1345 q_data
->width
= f
->fmt
.pix
.width
;
1346 q_data
->height
= f
->fmt
.pix
.height
;
1347 q_data
->bytesperline
= f
->fmt
.pix
.bytesperline
;
1348 q_data
->sizeimage
= f
->fmt
.pix
.sizeimage
;
1351 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
1352 f
->type
, q_data
->width
, q_data
->height
, q_data
->fmt
->fourcc
);
1357 static int pxp_s_fmt_vid_cap(struct file
*file
, void *priv
,
1358 struct v4l2_format
*f
)
1360 struct pxp_ctx
*ctx
= file2ctx(file
);
1363 ret
= pxp_try_fmt_vid_cap(file
, priv
, f
);
1367 ret
= pxp_s_fmt(file2ctx(file
), f
);
1371 ctx
->q_data
[V4L2_M2M_DST
].ycbcr_enc
= f
->fmt
.pix
.ycbcr_enc
;
1372 ctx
->q_data
[V4L2_M2M_DST
].quant
= f
->fmt
.pix
.quantization
;
1377 static int pxp_s_fmt_vid_out(struct file
*file
, void *priv
,
1378 struct v4l2_format
*f
)
1380 struct pxp_ctx
*ctx
= file2ctx(file
);
1383 ret
= pxp_try_fmt_vid_out(file
, priv
, f
);
1387 ret
= pxp_s_fmt(file2ctx(file
), f
);
1391 ctx
->colorspace
= f
->fmt
.pix
.colorspace
;
1392 ctx
->xfer_func
= f
->fmt
.pix
.xfer_func
;
1393 ctx
->q_data
[V4L2_M2M_SRC
].ycbcr_enc
= f
->fmt
.pix
.ycbcr_enc
;
1394 ctx
->q_data
[V4L2_M2M_SRC
].quant
= f
->fmt
.pix
.quantization
;
1396 pxp_fixup_colorimetry_cap(ctx
, ctx
->q_data
[V4L2_M2M_DST
].fmt
->fourcc
,
1397 &ctx
->q_data
[V4L2_M2M_DST
].ycbcr_enc
,
1398 &ctx
->q_data
[V4L2_M2M_DST
].quant
);
1403 static int pxp_enum_framesizes(struct file
*file
, void *fh
,
1404 struct v4l2_frmsizeenum
*fsize
)
1406 if (fsize
->index
> 0)
1409 if (!find_format(fsize
->pixel_format
))
1412 fsize
->type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
1413 fsize
->stepwise
.min_width
= MIN_W
;
1414 fsize
->stepwise
.max_width
= MAX_W
;
1415 fsize
->stepwise
.step_width
= 1 << ALIGN_W
;
1416 fsize
->stepwise
.min_height
= MIN_H
;
1417 fsize
->stepwise
.max_height
= MAX_H
;
1418 fsize
->stepwise
.step_height
= 1 << ALIGN_H
;
1423 static u8
pxp_degrees_to_rot_mode(u32 degrees
)
1427 return BV_PXP_CTRL_ROTATE0__ROT_90
;
1429 return BV_PXP_CTRL_ROTATE0__ROT_180
;
1431 return BV_PXP_CTRL_ROTATE0__ROT_270
;
1434 return BV_PXP_CTRL_ROTATE0__ROT_0
;
1438 static int pxp_s_ctrl(struct v4l2_ctrl
*ctrl
)
1440 struct pxp_ctx
*ctx
=
1441 container_of(ctrl
->handler
, struct pxp_ctx
, hdl
);
1444 case V4L2_CID_HFLIP
:
1446 ctx
->mode
|= MEM2MEM_HFLIP
;
1448 ctx
->mode
&= ~MEM2MEM_HFLIP
;
1451 case V4L2_CID_VFLIP
:
1453 ctx
->mode
|= MEM2MEM_VFLIP
;
1455 ctx
->mode
&= ~MEM2MEM_VFLIP
;
1458 case V4L2_CID_ROTATE
:
1459 ctx
->rotation
= pxp_degrees_to_rot_mode(ctrl
->val
);
1462 case V4L2_CID_ALPHA_COMPONENT
:
1463 ctx
->alpha_component
= ctrl
->val
;
1467 v4l2_err(&ctx
->dev
->v4l2_dev
, "Invalid control\n");
1474 static const struct v4l2_ctrl_ops pxp_ctrl_ops
= {
1475 .s_ctrl
= pxp_s_ctrl
,
1478 static const struct v4l2_ioctl_ops pxp_ioctl_ops
= {
1479 .vidioc_querycap
= pxp_querycap
,
1481 .vidioc_enum_fmt_vid_cap
= pxp_enum_fmt_vid_cap
,
1482 .vidioc_g_fmt_vid_cap
= pxp_g_fmt_vid_cap
,
1483 .vidioc_try_fmt_vid_cap
= pxp_try_fmt_vid_cap
,
1484 .vidioc_s_fmt_vid_cap
= pxp_s_fmt_vid_cap
,
1486 .vidioc_enum_fmt_vid_out
= pxp_enum_fmt_vid_out
,
1487 .vidioc_g_fmt_vid_out
= pxp_g_fmt_vid_out
,
1488 .vidioc_try_fmt_vid_out
= pxp_try_fmt_vid_out
,
1489 .vidioc_s_fmt_vid_out
= pxp_s_fmt_vid_out
,
1491 .vidioc_enum_framesizes
= pxp_enum_framesizes
,
1493 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
1494 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
1495 .vidioc_qbuf
= v4l2_m2m_ioctl_qbuf
,
1496 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
1497 .vidioc_prepare_buf
= v4l2_m2m_ioctl_prepare_buf
,
1498 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
1499 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
1501 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
1502 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
1504 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1505 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1511 static int pxp_queue_setup(struct vb2_queue
*vq
,
1512 unsigned int *nbuffers
, unsigned int *nplanes
,
1513 unsigned int sizes
[], struct device
*alloc_devs
[])
1515 struct pxp_ctx
*ctx
= vb2_get_drv_priv(vq
);
1516 struct pxp_q_data
*q_data
;
1517 unsigned int size
, count
= *nbuffers
;
1519 q_data
= get_q_data(ctx
, vq
->type
);
1521 size
= q_data
->sizeimage
;
1526 return sizes
[0] < size
? -EINVAL
: 0;
1531 dprintk(ctx
->dev
, "get %d buffer(s) of size %d each.\n", count
, size
);
1536 static int pxp_buf_prepare(struct vb2_buffer
*vb
)
1538 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1539 struct pxp_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1540 struct pxp_dev
*dev
= ctx
->dev
;
1541 struct pxp_q_data
*q_data
;
1543 dprintk(ctx
->dev
, "type: %d\n", vb
->vb2_queue
->type
);
1545 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
1546 if (V4L2_TYPE_IS_OUTPUT(vb
->vb2_queue
->type
)) {
1547 if (vbuf
->field
== V4L2_FIELD_ANY
)
1548 vbuf
->field
= V4L2_FIELD_NONE
;
1549 if (vbuf
->field
!= V4L2_FIELD_NONE
) {
1550 dprintk(dev
, "%s field isn't supported\n", __func__
);
1555 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
1556 dprintk(dev
, "%s data will not fit into plane (%lu < %lu)\n",
1557 __func__
, vb2_plane_size(vb
, 0),
1558 (long)q_data
->sizeimage
);
1562 vb2_set_plane_payload(vb
, 0, q_data
->sizeimage
);
1567 static void pxp_buf_queue(struct vb2_buffer
*vb
)
1569 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1570 struct pxp_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
1572 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
1575 static int pxp_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1577 struct pxp_ctx
*ctx
= vb2_get_drv_priv(q
);
1578 struct pxp_q_data
*q_data
= get_q_data(ctx
, q
->type
);
1580 q_data
->sequence
= 0;
1584 static void pxp_stop_streaming(struct vb2_queue
*q
)
1586 struct pxp_ctx
*ctx
= vb2_get_drv_priv(q
);
1587 struct vb2_v4l2_buffer
*vbuf
;
1588 unsigned long flags
;
1591 if (V4L2_TYPE_IS_OUTPUT(q
->type
))
1592 vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
1594 vbuf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
1597 spin_lock_irqsave(&ctx
->dev
->irqlock
, flags
);
1598 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
1599 spin_unlock_irqrestore(&ctx
->dev
->irqlock
, flags
);
1603 static const struct vb2_ops pxp_qops
= {
1604 .queue_setup
= pxp_queue_setup
,
1605 .buf_prepare
= pxp_buf_prepare
,
1606 .buf_queue
= pxp_buf_queue
,
1607 .start_streaming
= pxp_start_streaming
,
1608 .stop_streaming
= pxp_stop_streaming
,
1609 .wait_prepare
= vb2_ops_wait_prepare
,
1610 .wait_finish
= vb2_ops_wait_finish
,
1613 static int queue_init(void *priv
, struct vb2_queue
*src_vq
,
1614 struct vb2_queue
*dst_vq
)
1616 struct pxp_ctx
*ctx
= priv
;
1619 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1620 src_vq
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
1621 src_vq
->drv_priv
= ctx
;
1622 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1623 src_vq
->ops
= &pxp_qops
;
1624 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1625 src_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1626 src_vq
->lock
= &ctx
->dev
->dev_mutex
;
1627 src_vq
->dev
= ctx
->dev
->v4l2_dev
.dev
;
1629 ret
= vb2_queue_init(src_vq
);
1633 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1634 dst_vq
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
1635 dst_vq
->drv_priv
= ctx
;
1636 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1637 dst_vq
->ops
= &pxp_qops
;
1638 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1639 dst_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1640 dst_vq
->lock
= &ctx
->dev
->dev_mutex
;
1641 dst_vq
->dev
= ctx
->dev
->v4l2_dev
.dev
;
1643 return vb2_queue_init(dst_vq
);
1649 static int pxp_open(struct file
*file
)
1651 struct pxp_dev
*dev
= video_drvdata(file
);
1652 struct pxp_ctx
*ctx
= NULL
;
1653 struct v4l2_ctrl_handler
*hdl
;
1656 if (mutex_lock_interruptible(&dev
->dev_mutex
))
1657 return -ERESTARTSYS
;
1658 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1664 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
1665 file
->private_data
= &ctx
->fh
;
1668 v4l2_ctrl_handler_init(hdl
, 4);
1669 v4l2_ctrl_new_std(hdl
, &pxp_ctrl_ops
, V4L2_CID_HFLIP
, 0, 1, 1, 0);
1670 v4l2_ctrl_new_std(hdl
, &pxp_ctrl_ops
, V4L2_CID_VFLIP
, 0, 1, 1, 0);
1671 v4l2_ctrl_new_std(hdl
, &pxp_ctrl_ops
, V4L2_CID_ROTATE
, 0, 270, 90, 0);
1672 v4l2_ctrl_new_std(hdl
, &pxp_ctrl_ops
, V4L2_CID_ALPHA_COMPONENT
,
1676 v4l2_ctrl_handler_free(hdl
);
1680 ctx
->fh
.ctrl_handler
= hdl
;
1681 v4l2_ctrl_handler_setup(hdl
);
1683 ctx
->q_data
[V4L2_M2M_SRC
].fmt
= &formats
[0];
1684 ctx
->q_data
[V4L2_M2M_SRC
].width
= 640;
1685 ctx
->q_data
[V4L2_M2M_SRC
].height
= 480;
1686 ctx
->q_data
[V4L2_M2M_SRC
].bytesperline
=
1687 pxp_bytesperline(&formats
[0], 640);
1688 ctx
->q_data
[V4L2_M2M_SRC
].sizeimage
=
1689 pxp_sizeimage(&formats
[0], 640, 480);
1690 ctx
->q_data
[V4L2_M2M_DST
] = ctx
->q_data
[V4L2_M2M_SRC
];
1691 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
1693 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev
, ctx
, &queue_init
);
1695 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
1696 rc
= PTR_ERR(ctx
->fh
.m2m_ctx
);
1698 v4l2_ctrl_handler_free(hdl
);
1699 v4l2_fh_exit(&ctx
->fh
);
1704 v4l2_fh_add(&ctx
->fh
);
1705 atomic_inc(&dev
->num_inst
);
1707 dprintk(dev
, "Created instance: %p, m2m_ctx: %p\n",
1708 ctx
, ctx
->fh
.m2m_ctx
);
1711 mutex_unlock(&dev
->dev_mutex
);
1715 static int pxp_release(struct file
*file
)
1717 struct pxp_dev
*dev
= video_drvdata(file
);
1718 struct pxp_ctx
*ctx
= file2ctx(file
);
1720 dprintk(dev
, "Releasing instance %p\n", ctx
);
1722 v4l2_fh_del(&ctx
->fh
);
1723 v4l2_fh_exit(&ctx
->fh
);
1724 v4l2_ctrl_handler_free(&ctx
->hdl
);
1725 mutex_lock(&dev
->dev_mutex
);
1726 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1727 mutex_unlock(&dev
->dev_mutex
);
1730 atomic_dec(&dev
->num_inst
);
1735 static const struct v4l2_file_operations pxp_fops
= {
1736 .owner
= THIS_MODULE
,
1738 .release
= pxp_release
,
1739 .poll
= v4l2_m2m_fop_poll
,
1740 .unlocked_ioctl
= video_ioctl2
,
1741 .mmap
= v4l2_m2m_fop_mmap
,
1744 static const struct video_device pxp_videodev
= {
1745 .name
= MEM2MEM_NAME
,
1746 .vfl_dir
= VFL_DIR_M2M
,
1748 .device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
,
1749 .ioctl_ops
= &pxp_ioctl_ops
,
1751 .release
= video_device_release_empty
,
1754 static const struct v4l2_m2m_ops m2m_ops
= {
1755 .device_run
= pxp_device_run
,
1756 .job_ready
= pxp_job_ready
,
1757 .job_abort
= pxp_job_abort
,
1760 static int pxp_soft_reset(struct pxp_dev
*dev
)
1765 pxp_write(dev
, HW_PXP_CTRL_CLR
, BM_PXP_CTRL_SFTRST
);
1766 pxp_write(dev
, HW_PXP_CTRL_CLR
, BM_PXP_CTRL_CLKGATE
);
1768 pxp_write(dev
, HW_PXP_CTRL_SET
, BM_PXP_CTRL_SFTRST
);
1770 ret
= regmap_read_poll_timeout(dev
->regmap
, HW_PXP_CTRL
, val
,
1771 val
& BM_PXP_CTRL_CLKGATE
, 0, 100);
1775 pxp_write(dev
, HW_PXP_CTRL_CLR
, BM_PXP_CTRL_SFTRST
);
1776 pxp_write(dev
, HW_PXP_CTRL_CLR
, BM_PXP_CTRL_CLKGATE
);
1781 static int pxp_probe(struct platform_device
*pdev
)
1783 struct pxp_dev
*dev
;
1784 struct video_device
*vfd
;
1790 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1794 dev
->pdata
= of_device_get_match_data(&pdev
->dev
);
1796 dev
->clk
= devm_clk_get(&pdev
->dev
, "axi");
1797 if (IS_ERR(dev
->clk
)) {
1798 ret
= PTR_ERR(dev
->clk
);
1799 dev_err(&pdev
->dev
, "Failed to get clk: %d\n", ret
);
1803 mmio
= devm_platform_ioremap_resource(pdev
, 0);
1805 return PTR_ERR(mmio
);
1806 dev
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, mmio
,
1807 &pxp_regmap_config
);
1808 if (IS_ERR(dev
->regmap
))
1809 return dev_err_probe(&pdev
->dev
, PTR_ERR(dev
->regmap
),
1810 "Failed to init regmap\n");
1812 irq
= platform_get_irq(pdev
, 0);
1816 spin_lock_init(&dev
->irqlock
);
1818 ret
= devm_request_irq(&pdev
->dev
, irq
, pxp_irq_handler
, 0,
1819 dev_name(&pdev
->dev
), dev
);
1821 dev_err(&pdev
->dev
, "Failed to request irq: %d\n", ret
);
1825 ret
= clk_prepare_enable(dev
->clk
);
1829 ret
= pxp_soft_reset(dev
);
1831 dev_err(&pdev
->dev
, "PXP reset timeout: %d\n", ret
);
1835 hw_version
= pxp_read(dev
, HW_PXP_VERSION
);
1836 dev_dbg(&pdev
->dev
, "PXP Version %u.%u\n",
1837 PXP_VERSION_MAJOR(hw_version
), PXP_VERSION_MINOR(hw_version
));
1839 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
1843 atomic_set(&dev
->num_inst
, 0);
1844 mutex_init(&dev
->dev_mutex
);
1846 dev
->vfd
= pxp_videodev
;
1848 vfd
->lock
= &dev
->dev_mutex
;
1849 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1851 video_set_drvdata(vfd
, dev
);
1852 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s", pxp_videodev
.name
);
1853 v4l2_info(&dev
->v4l2_dev
,
1854 "Device registered as /dev/video%d\n", vfd
->num
);
1856 platform_set_drvdata(pdev
, dev
);
1858 dev
->m2m_dev
= v4l2_m2m_init(&m2m_ops
);
1859 if (IS_ERR(dev
->m2m_dev
)) {
1860 v4l2_err(&dev
->v4l2_dev
, "Failed to init mem2mem device\n");
1861 ret
= PTR_ERR(dev
->m2m_dev
);
1865 ret
= video_register_device(vfd
, VFL_TYPE_VIDEO
, 0);
1867 v4l2_err(&dev
->v4l2_dev
, "Failed to register video device\n");
1871 #ifdef CONFIG_MEDIA_CONTROLLER
1872 dev
->mdev
.dev
= &pdev
->dev
;
1873 strscpy(dev
->mdev
.model
, MEM2MEM_NAME
, sizeof(dev
->mdev
.model
));
1874 media_device_init(&dev
->mdev
);
1875 dev
->v4l2_dev
.mdev
= &dev
->mdev
;
1877 ret
= v4l2_m2m_register_media_controller(dev
->m2m_dev
, vfd
,
1878 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER
);
1880 dev_err(&pdev
->dev
, "Failed to initialize media device\n");
1884 ret
= media_device_register(&dev
->mdev
);
1886 dev_err(&pdev
->dev
, "Failed to register media device\n");
1893 #ifdef CONFIG_MEDIA_CONTROLLER
1895 v4l2_m2m_unregister_media_controller(dev
->m2m_dev
);
1897 video_unregister_device(vfd
);
1900 v4l2_m2m_release(dev
->m2m_dev
);
1902 v4l2_device_unregister(&dev
->v4l2_dev
);
1904 clk_disable_unprepare(dev
->clk
);
1909 static void pxp_remove(struct platform_device
*pdev
)
1911 struct pxp_dev
*dev
= platform_get_drvdata(pdev
);
1913 pxp_write(dev
, HW_PXP_CTRL_SET
, BM_PXP_CTRL_CLKGATE
);
1914 pxp_write(dev
, HW_PXP_CTRL_SET
, BM_PXP_CTRL_SFTRST
);
1916 clk_disable_unprepare(dev
->clk
);
1918 v4l2_info(&dev
->v4l2_dev
, "Removing " MEM2MEM_NAME
);
1920 #ifdef CONFIG_MEDIA_CONTROLLER
1921 media_device_unregister(&dev
->mdev
);
1922 v4l2_m2m_unregister_media_controller(dev
->m2m_dev
);
1924 video_unregister_device(&dev
->vfd
);
1925 v4l2_m2m_release(dev
->m2m_dev
);
1926 v4l2_device_unregister(&dev
->v4l2_dev
);
1929 static const struct pxp_pdata pxp_imx6ull_pdata
= {
1930 .data_path_ctrl0
= pxp_imx6ull_data_path_ctrl0
,
1933 static const struct pxp_pdata pxp_imx7d_pdata
= {
1934 .data_path_ctrl0
= pxp_imx7d_data_path_ctrl0
,
1937 static const struct of_device_id pxp_dt_ids
[] = {
1938 { .compatible
= "fsl,imx6ull-pxp", .data
= &pxp_imx6ull_pdata
},
1939 { .compatible
= "fsl,imx7d-pxp", .data
= &pxp_imx7d_pdata
},
1942 MODULE_DEVICE_TABLE(of
, pxp_dt_ids
);
1944 static struct platform_driver pxp_driver
= {
1946 .remove_new
= pxp_remove
,
1948 .name
= MEM2MEM_NAME
,
1949 .of_match_table
= pxp_dt_ids
,
1953 module_platform_driver(pxp_driver
);
1955 MODULE_DESCRIPTION("i.MX PXP mem2mem scaler/CSC/rotator");
1956 MODULE_AUTHOR("Philipp Zabel <kernel@pengutronix.de>");
1957 MODULE_LICENSE("GPL");