1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012 Mentor Graphics Inc.
4 * Copyright 2005-2012 Freescale Semiconductor, Inc. All Rights Reserved.
6 #include <linux/types.h>
7 #include <linux/bitrev.h>
9 #include <linux/sizes.h>
10 #include <drm/drm_fourcc.h>
13 struct ipu_cpmem_word
{
19 struct ipu_cpmem_word word
[2];
23 struct ipu_ch_param __iomem
*base
;
30 #define IPU_CPMEM_WORD(word, ofs, size) ((((word) * 160 + (ofs)) << 8) | (size))
32 #define IPU_FIELD_UBO IPU_CPMEM_WORD(0, 46, 22)
33 #define IPU_FIELD_VBO IPU_CPMEM_WORD(0, 68, 22)
34 #define IPU_FIELD_IOX IPU_CPMEM_WORD(0, 90, 4)
35 #define IPU_FIELD_RDRW IPU_CPMEM_WORD(0, 94, 1)
36 #define IPU_FIELD_SO IPU_CPMEM_WORD(0, 113, 1)
37 #define IPU_FIELD_SLY IPU_CPMEM_WORD(1, 102, 14)
38 #define IPU_FIELD_SLUV IPU_CPMEM_WORD(1, 128, 14)
40 #define IPU_FIELD_XV IPU_CPMEM_WORD(0, 0, 10)
41 #define IPU_FIELD_YV IPU_CPMEM_WORD(0, 10, 9)
42 #define IPU_FIELD_XB IPU_CPMEM_WORD(0, 19, 13)
43 #define IPU_FIELD_YB IPU_CPMEM_WORD(0, 32, 12)
44 #define IPU_FIELD_NSB_B IPU_CPMEM_WORD(0, 44, 1)
45 #define IPU_FIELD_CF IPU_CPMEM_WORD(0, 45, 1)
46 #define IPU_FIELD_SX IPU_CPMEM_WORD(0, 46, 12)
47 #define IPU_FIELD_SY IPU_CPMEM_WORD(0, 58, 11)
48 #define IPU_FIELD_NS IPU_CPMEM_WORD(0, 69, 10)
49 #define IPU_FIELD_SDX IPU_CPMEM_WORD(0, 79, 7)
50 #define IPU_FIELD_SM IPU_CPMEM_WORD(0, 86, 10)
51 #define IPU_FIELD_SCC IPU_CPMEM_WORD(0, 96, 1)
52 #define IPU_FIELD_SCE IPU_CPMEM_WORD(0, 97, 1)
53 #define IPU_FIELD_SDY IPU_CPMEM_WORD(0, 98, 7)
54 #define IPU_FIELD_SDRX IPU_CPMEM_WORD(0, 105, 1)
55 #define IPU_FIELD_SDRY IPU_CPMEM_WORD(0, 106, 1)
56 #define IPU_FIELD_BPP IPU_CPMEM_WORD(0, 107, 3)
57 #define IPU_FIELD_DEC_SEL IPU_CPMEM_WORD(0, 110, 2)
58 #define IPU_FIELD_DIM IPU_CPMEM_WORD(0, 112, 1)
59 #define IPU_FIELD_BNDM IPU_CPMEM_WORD(0, 114, 3)
60 #define IPU_FIELD_BM IPU_CPMEM_WORD(0, 117, 2)
61 #define IPU_FIELD_ROT IPU_CPMEM_WORD(0, 119, 1)
62 #define IPU_FIELD_ROT_HF_VF IPU_CPMEM_WORD(0, 119, 3)
63 #define IPU_FIELD_HF IPU_CPMEM_WORD(0, 120, 1)
64 #define IPU_FIELD_VF IPU_CPMEM_WORD(0, 121, 1)
65 #define IPU_FIELD_THE IPU_CPMEM_WORD(0, 122, 1)
66 #define IPU_FIELD_CAP IPU_CPMEM_WORD(0, 123, 1)
67 #define IPU_FIELD_CAE IPU_CPMEM_WORD(0, 124, 1)
68 #define IPU_FIELD_FW IPU_CPMEM_WORD(0, 125, 13)
69 #define IPU_FIELD_FH IPU_CPMEM_WORD(0, 138, 12)
70 #define IPU_FIELD_EBA0 IPU_CPMEM_WORD(1, 0, 29)
71 #define IPU_FIELD_EBA1 IPU_CPMEM_WORD(1, 29, 29)
72 #define IPU_FIELD_ILO IPU_CPMEM_WORD(1, 58, 20)
73 #define IPU_FIELD_NPB IPU_CPMEM_WORD(1, 78, 7)
74 #define IPU_FIELD_PFS IPU_CPMEM_WORD(1, 85, 4)
75 #define IPU_FIELD_ALU IPU_CPMEM_WORD(1, 89, 1)
76 #define IPU_FIELD_ALBM IPU_CPMEM_WORD(1, 90, 3)
77 #define IPU_FIELD_ID IPU_CPMEM_WORD(1, 93, 2)
78 #define IPU_FIELD_TH IPU_CPMEM_WORD(1, 95, 7)
79 #define IPU_FIELD_SL IPU_CPMEM_WORD(1, 102, 14)
80 #define IPU_FIELD_WID0 IPU_CPMEM_WORD(1, 116, 3)
81 #define IPU_FIELD_WID1 IPU_CPMEM_WORD(1, 119, 3)
82 #define IPU_FIELD_WID2 IPU_CPMEM_WORD(1, 122, 3)
83 #define IPU_FIELD_WID3 IPU_CPMEM_WORD(1, 125, 3)
84 #define IPU_FIELD_OFS0 IPU_CPMEM_WORD(1, 128, 5)
85 #define IPU_FIELD_OFS1 IPU_CPMEM_WORD(1, 133, 5)
86 #define IPU_FIELD_OFS2 IPU_CPMEM_WORD(1, 138, 5)
87 #define IPU_FIELD_OFS3 IPU_CPMEM_WORD(1, 143, 5)
88 #define IPU_FIELD_SXYS IPU_CPMEM_WORD(1, 148, 1)
89 #define IPU_FIELD_CRE IPU_CPMEM_WORD(1, 149, 1)
90 #define IPU_FIELD_DEC_SEL2 IPU_CPMEM_WORD(1, 150, 1)
92 static inline struct ipu_ch_param __iomem
*
93 ipu_get_cpmem(struct ipuv3_channel
*ch
)
95 struct ipu_cpmem
*cpmem
= ch
->ipu
->cpmem_priv
;
97 return cpmem
->base
+ ch
->num
;
100 static void ipu_ch_param_write_field(struct ipuv3_channel
*ch
, u32 wbs
, u32 v
)
102 struct ipu_ch_param __iomem
*base
= ipu_get_cpmem(ch
);
103 u32 bit
= (wbs
>> 8) % 160;
104 u32 size
= wbs
& 0xff;
105 u32 word
= (wbs
>> 8) / 160;
108 u32 mask
= (1 << size
) - 1;
111 pr_debug("%s %d %d %d\n", __func__
, word
, bit
, size
);
113 val
= readl(&base
->word
[word
].data
[i
]);
114 val
&= ~(mask
<< ofs
);
116 writel(val
, &base
->word
[word
].data
[i
]);
118 if ((bit
+ size
- 1) / 32 > i
) {
119 val
= readl(&base
->word
[word
].data
[i
+ 1]);
120 val
&= ~(mask
>> (ofs
? (32 - ofs
) : 0));
121 val
|= v
>> (ofs
? (32 - ofs
) : 0);
122 writel(val
, &base
->word
[word
].data
[i
+ 1]);
126 static u32
ipu_ch_param_read_field(struct ipuv3_channel
*ch
, u32 wbs
)
128 struct ipu_ch_param __iomem
*base
= ipu_get_cpmem(ch
);
129 u32 bit
= (wbs
>> 8) % 160;
130 u32 size
= wbs
& 0xff;
131 u32 word
= (wbs
>> 8) / 160;
134 u32 mask
= (1 << size
) - 1;
137 pr_debug("%s %d %d %d\n", __func__
, word
, bit
, size
);
139 val
= (readl(&base
->word
[word
].data
[i
]) >> ofs
) & mask
;
141 if ((bit
+ size
- 1) / 32 > i
) {
144 tmp
= readl(&base
->word
[word
].data
[i
+ 1]);
145 tmp
&= mask
>> (ofs
? (32 - ofs
) : 0);
146 val
|= tmp
<< (ofs
? (32 - ofs
) : 0);
153 * The V4L2 spec defines packed RGB formats in memory byte order, which from
154 * point of view of the IPU corresponds to little-endian words with the first
155 * component in the least significant bits.
156 * The DRM pixel formats and IPU internal representation are ordered the other
157 * way around, with the first named component ordered at the most significant
158 * bits. Further, V4L2 formats are not well defined:
159 * https://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html
160 * We choose the interpretation which matches GStreamer behavior.
162 static int v4l2_pix_fmt_to_drm_fourcc(u32 pixelformat
)
164 switch (pixelformat
) {
165 case V4L2_PIX_FMT_RGB565
:
167 * Here we choose the 'corrected' interpretation of RGBP, a
168 * little-endian 16-bit word with the red component at the most
170 * g[2:0]b[4:0] r[4:0]g[5:3] <=> [16:0] R:G:B
172 return DRM_FORMAT_RGB565
;
173 case V4L2_PIX_FMT_BGR24
:
174 /* B G R <=> [24:0] R:G:B */
175 return DRM_FORMAT_RGB888
;
176 case V4L2_PIX_FMT_RGB24
:
177 /* R G B <=> [24:0] B:G:R */
178 return DRM_FORMAT_BGR888
;
179 case V4L2_PIX_FMT_BGR32
:
180 /* B G R A <=> [32:0] A:B:G:R */
181 return DRM_FORMAT_XRGB8888
;
182 case V4L2_PIX_FMT_RGB32
:
183 /* R G B A <=> [32:0] A:B:G:R */
184 return DRM_FORMAT_XBGR8888
;
185 case V4L2_PIX_FMT_ABGR32
:
186 /* B G R A <=> [32:0] A:R:G:B */
187 return DRM_FORMAT_ARGB8888
;
188 case V4L2_PIX_FMT_XBGR32
:
189 /* B G R X <=> [32:0] X:R:G:B */
190 return DRM_FORMAT_XRGB8888
;
191 case V4L2_PIX_FMT_BGRA32
:
192 /* A B G R <=> [32:0] R:G:B:A */
193 return DRM_FORMAT_RGBA8888
;
194 case V4L2_PIX_FMT_BGRX32
:
195 /* X B G R <=> [32:0] R:G:B:X */
196 return DRM_FORMAT_RGBX8888
;
197 case V4L2_PIX_FMT_RGBA32
:
198 /* R G B A <=> [32:0] A:B:G:R */
199 return DRM_FORMAT_ABGR8888
;
200 case V4L2_PIX_FMT_RGBX32
:
201 /* R G B X <=> [32:0] X:B:G:R */
202 return DRM_FORMAT_XBGR8888
;
203 case V4L2_PIX_FMT_ARGB32
:
204 /* A R G B <=> [32:0] B:G:R:A */
205 return DRM_FORMAT_BGRA8888
;
206 case V4L2_PIX_FMT_XRGB32
:
207 /* X R G B <=> [32:0] B:G:R:X */
208 return DRM_FORMAT_BGRX8888
;
209 case V4L2_PIX_FMT_UYVY
:
210 return DRM_FORMAT_UYVY
;
211 case V4L2_PIX_FMT_YUYV
:
212 return DRM_FORMAT_YUYV
;
213 case V4L2_PIX_FMT_YUV420
:
214 return DRM_FORMAT_YUV420
;
215 case V4L2_PIX_FMT_YUV422P
:
216 return DRM_FORMAT_YUV422
;
217 case V4L2_PIX_FMT_YVU420
:
218 return DRM_FORMAT_YVU420
;
219 case V4L2_PIX_FMT_NV12
:
220 return DRM_FORMAT_NV12
;
221 case V4L2_PIX_FMT_NV16
:
222 return DRM_FORMAT_NV16
;
228 void ipu_cpmem_zero(struct ipuv3_channel
*ch
)
230 struct ipu_ch_param __iomem
*p
= ipu_get_cpmem(ch
);
231 void __iomem
*base
= p
;
234 for (i
= 0; i
< sizeof(*p
) / sizeof(u32
); i
++)
235 writel(0, base
+ i
* sizeof(u32
));
237 EXPORT_SYMBOL_GPL(ipu_cpmem_zero
);
239 void ipu_cpmem_set_resolution(struct ipuv3_channel
*ch
, int xres
, int yres
)
241 ipu_ch_param_write_field(ch
, IPU_FIELD_FW
, xres
- 1);
242 ipu_ch_param_write_field(ch
, IPU_FIELD_FH
, yres
- 1);
244 EXPORT_SYMBOL_GPL(ipu_cpmem_set_resolution
);
246 void ipu_cpmem_skip_odd_chroma_rows(struct ipuv3_channel
*ch
)
248 ipu_ch_param_write_field(ch
, IPU_FIELD_RDRW
, 1);
250 EXPORT_SYMBOL_GPL(ipu_cpmem_skip_odd_chroma_rows
);
252 void ipu_cpmem_set_stride(struct ipuv3_channel
*ch
, int stride
)
254 ipu_ch_param_write_field(ch
, IPU_FIELD_SLY
, stride
- 1);
256 EXPORT_SYMBOL_GPL(ipu_cpmem_set_stride
);
258 void ipu_cpmem_set_high_priority(struct ipuv3_channel
*ch
)
260 struct ipu_soc
*ipu
= ch
->ipu
;
263 if (ipu
->ipu_type
== IPUV3EX
)
264 ipu_ch_param_write_field(ch
, IPU_FIELD_ID
, 1);
266 val
= ipu_idmac_read(ipu
, IDMAC_CHA_PRI(ch
->num
));
267 val
|= 1 << (ch
->num
% 32);
268 ipu_idmac_write(ipu
, val
, IDMAC_CHA_PRI(ch
->num
));
270 EXPORT_SYMBOL_GPL(ipu_cpmem_set_high_priority
);
272 void ipu_cpmem_set_buffer(struct ipuv3_channel
*ch
, int bufnum
, dma_addr_t buf
)
274 WARN_ON_ONCE(buf
& 0x7);
277 ipu_ch_param_write_field(ch
, IPU_FIELD_EBA1
, buf
>> 3);
279 ipu_ch_param_write_field(ch
, IPU_FIELD_EBA0
, buf
>> 3);
281 EXPORT_SYMBOL_GPL(ipu_cpmem_set_buffer
);
283 void ipu_cpmem_set_uv_offset(struct ipuv3_channel
*ch
, u32 u_off
, u32 v_off
)
285 WARN_ON_ONCE((u_off
& 0x7) || (v_off
& 0x7));
287 ipu_ch_param_write_field(ch
, IPU_FIELD_UBO
, u_off
/ 8);
288 ipu_ch_param_write_field(ch
, IPU_FIELD_VBO
, v_off
/ 8);
290 EXPORT_SYMBOL_GPL(ipu_cpmem_set_uv_offset
);
292 void ipu_cpmem_interlaced_scan(struct ipuv3_channel
*ch
, int stride
,
299 ilo
= 0x100000 - (stride
/ 8);
304 sly
= (stride
* 2) - 1;
306 switch (pixelformat
) {
307 case V4L2_PIX_FMT_YUV420
:
308 case V4L2_PIX_FMT_YVU420
:
309 sluv
= stride
/ 2 - 1;
311 case V4L2_PIX_FMT_NV12
:
314 case V4L2_PIX_FMT_YUV422P
:
317 case V4L2_PIX_FMT_NV16
:
318 sluv
= stride
* 2 - 1;
325 ipu_ch_param_write_field(ch
, IPU_FIELD_SO
, 1);
326 ipu_ch_param_write_field(ch
, IPU_FIELD_ILO
, ilo
);
327 ipu_ch_param_write_field(ch
, IPU_FIELD_SLY
, sly
);
329 ipu_ch_param_write_field(ch
, IPU_FIELD_SLUV
, sluv
);
331 EXPORT_SYMBOL_GPL(ipu_cpmem_interlaced_scan
);
333 void ipu_cpmem_set_axi_id(struct ipuv3_channel
*ch
, u32 id
)
336 ipu_ch_param_write_field(ch
, IPU_FIELD_ID
, id
);
338 EXPORT_SYMBOL_GPL(ipu_cpmem_set_axi_id
);
340 int ipu_cpmem_get_burstsize(struct ipuv3_channel
*ch
)
342 return ipu_ch_param_read_field(ch
, IPU_FIELD_NPB
) + 1;
344 EXPORT_SYMBOL_GPL(ipu_cpmem_get_burstsize
);
346 void ipu_cpmem_set_burstsize(struct ipuv3_channel
*ch
, int burstsize
)
348 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, burstsize
- 1);
350 EXPORT_SYMBOL_GPL(ipu_cpmem_set_burstsize
);
352 void ipu_cpmem_set_block_mode(struct ipuv3_channel
*ch
)
354 ipu_ch_param_write_field(ch
, IPU_FIELD_BM
, 1);
356 EXPORT_SYMBOL_GPL(ipu_cpmem_set_block_mode
);
358 void ipu_cpmem_set_rotation(struct ipuv3_channel
*ch
,
359 enum ipu_rotate_mode rot
)
361 u32 temp_rot
= bitrev8(rot
) >> 5;
363 ipu_ch_param_write_field(ch
, IPU_FIELD_ROT_HF_VF
, temp_rot
);
365 EXPORT_SYMBOL_GPL(ipu_cpmem_set_rotation
);
367 int ipu_cpmem_set_format_rgb(struct ipuv3_channel
*ch
,
368 const struct ipu_rgb
*rgb
)
370 int bpp
= 0, npb
= 0, ro
, go
, bo
, to
;
372 ro
= rgb
->bits_per_pixel
- rgb
->red
.length
- rgb
->red
.offset
;
373 go
= rgb
->bits_per_pixel
- rgb
->green
.length
- rgb
->green
.offset
;
374 bo
= rgb
->bits_per_pixel
- rgb
->blue
.length
- rgb
->blue
.offset
;
375 to
= rgb
->bits_per_pixel
- rgb
->transp
.length
- rgb
->transp
.offset
;
377 ipu_ch_param_write_field(ch
, IPU_FIELD_WID0
, rgb
->red
.length
- 1);
378 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS0
, ro
);
379 ipu_ch_param_write_field(ch
, IPU_FIELD_WID1
, rgb
->green
.length
- 1);
380 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS1
, go
);
381 ipu_ch_param_write_field(ch
, IPU_FIELD_WID2
, rgb
->blue
.length
- 1);
382 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS2
, bo
);
384 if (rgb
->transp
.length
) {
385 ipu_ch_param_write_field(ch
, IPU_FIELD_WID3
,
386 rgb
->transp
.length
- 1);
387 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS3
, to
);
389 ipu_ch_param_write_field(ch
, IPU_FIELD_WID3
, 7);
390 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS3
,
391 rgb
->bits_per_pixel
);
394 switch (rgb
->bits_per_pixel
) {
414 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, bpp
);
415 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, npb
);
416 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 7); /* rgb mode */
420 EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_rgb
);
422 int ipu_cpmem_set_format_passthrough(struct ipuv3_channel
*ch
, int width
)
424 int bpp
= 0, npb
= 0;
447 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, bpp
);
448 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, npb
);
449 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 6); /* raw mode */
453 EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_passthrough
);
455 void ipu_cpmem_set_yuv_interleaved(struct ipuv3_channel
*ch
, u32 pixel_format
)
457 switch (pixel_format
) {
458 case V4L2_PIX_FMT_UYVY
:
459 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3); /* bits/pixel */
460 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0xA);/* pix fmt */
461 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);/* burst size */
463 case V4L2_PIX_FMT_YUYV
:
464 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3); /* bits/pixel */
465 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0x8);/* pix fmt */
466 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);/* burst size */
470 EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_interleaved
);
472 void ipu_cpmem_set_yuv_planar_full(struct ipuv3_channel
*ch
,
473 unsigned int uv_stride
,
474 unsigned int u_offset
, unsigned int v_offset
)
476 WARN_ON_ONCE((u_offset
& 0x7) || (v_offset
& 0x7));
478 ipu_ch_param_write_field(ch
, IPU_FIELD_SLUV
, uv_stride
- 1);
479 ipu_ch_param_write_field(ch
, IPU_FIELD_UBO
, u_offset
/ 8);
480 ipu_ch_param_write_field(ch
, IPU_FIELD_VBO
, v_offset
/ 8);
482 EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_planar_full
);
484 static const struct ipu_rgb def_xrgb_32
= {
485 .red
= { .offset
= 16, .length
= 8, },
486 .green
= { .offset
= 8, .length
= 8, },
487 .blue
= { .offset
= 0, .length
= 8, },
488 .transp
= { .offset
= 24, .length
= 8, },
489 .bits_per_pixel
= 32,
492 static const struct ipu_rgb def_xbgr_32
= {
493 .red
= { .offset
= 0, .length
= 8, },
494 .green
= { .offset
= 8, .length
= 8, },
495 .blue
= { .offset
= 16, .length
= 8, },
496 .transp
= { .offset
= 24, .length
= 8, },
497 .bits_per_pixel
= 32,
500 static const struct ipu_rgb def_rgbx_32
= {
501 .red
= { .offset
= 24, .length
= 8, },
502 .green
= { .offset
= 16, .length
= 8, },
503 .blue
= { .offset
= 8, .length
= 8, },
504 .transp
= { .offset
= 0, .length
= 8, },
505 .bits_per_pixel
= 32,
508 static const struct ipu_rgb def_bgrx_32
= {
509 .red
= { .offset
= 8, .length
= 8, },
510 .green
= { .offset
= 16, .length
= 8, },
511 .blue
= { .offset
= 24, .length
= 8, },
512 .transp
= { .offset
= 0, .length
= 8, },
513 .bits_per_pixel
= 32,
516 static const struct ipu_rgb def_rgb_24
= {
517 .red
= { .offset
= 16, .length
= 8, },
518 .green
= { .offset
= 8, .length
= 8, },
519 .blue
= { .offset
= 0, .length
= 8, },
520 .transp
= { .offset
= 0, .length
= 0, },
521 .bits_per_pixel
= 24,
524 static const struct ipu_rgb def_bgr_24
= {
525 .red
= { .offset
= 0, .length
= 8, },
526 .green
= { .offset
= 8, .length
= 8, },
527 .blue
= { .offset
= 16, .length
= 8, },
528 .transp
= { .offset
= 0, .length
= 0, },
529 .bits_per_pixel
= 24,
532 static const struct ipu_rgb def_rgb_16
= {
533 .red
= { .offset
= 11, .length
= 5, },
534 .green
= { .offset
= 5, .length
= 6, },
535 .blue
= { .offset
= 0, .length
= 5, },
536 .transp
= { .offset
= 0, .length
= 0, },
537 .bits_per_pixel
= 16,
540 static const struct ipu_rgb def_bgr_16
= {
541 .red
= { .offset
= 0, .length
= 5, },
542 .green
= { .offset
= 5, .length
= 6, },
543 .blue
= { .offset
= 11, .length
= 5, },
544 .transp
= { .offset
= 0, .length
= 0, },
545 .bits_per_pixel
= 16,
548 static const struct ipu_rgb def_argb_16
= {
549 .red
= { .offset
= 10, .length
= 5, },
550 .green
= { .offset
= 5, .length
= 5, },
551 .blue
= { .offset
= 0, .length
= 5, },
552 .transp
= { .offset
= 15, .length
= 1, },
553 .bits_per_pixel
= 16,
556 static const struct ipu_rgb def_argb_16_4444
= {
557 .red
= { .offset
= 8, .length
= 4, },
558 .green
= { .offset
= 4, .length
= 4, },
559 .blue
= { .offset
= 0, .length
= 4, },
560 .transp
= { .offset
= 12, .length
= 4, },
561 .bits_per_pixel
= 16,
564 static const struct ipu_rgb def_abgr_16
= {
565 .red
= { .offset
= 0, .length
= 5, },
566 .green
= { .offset
= 5, .length
= 5, },
567 .blue
= { .offset
= 10, .length
= 5, },
568 .transp
= { .offset
= 15, .length
= 1, },
569 .bits_per_pixel
= 16,
572 static const struct ipu_rgb def_rgba_16
= {
573 .red
= { .offset
= 11, .length
= 5, },
574 .green
= { .offset
= 6, .length
= 5, },
575 .blue
= { .offset
= 1, .length
= 5, },
576 .transp
= { .offset
= 0, .length
= 1, },
577 .bits_per_pixel
= 16,
580 static const struct ipu_rgb def_bgra_16
= {
581 .red
= { .offset
= 1, .length
= 5, },
582 .green
= { .offset
= 6, .length
= 5, },
583 .blue
= { .offset
= 11, .length
= 5, },
584 .transp
= { .offset
= 0, .length
= 1, },
585 .bits_per_pixel
= 16,
588 #define Y_OFFSET(pix, x, y) ((x) + pix->width * (y))
589 #define U_OFFSET(pix, x, y) ((pix->width * pix->height) + \
590 (pix->width * ((y) / 2) / 2) + (x) / 2)
591 #define V_OFFSET(pix, x, y) ((pix->width * pix->height) + \
592 (pix->width * pix->height / 4) + \
593 (pix->width * ((y) / 2) / 2) + (x) / 2)
594 #define U2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
595 (pix->width * (y) / 2) + (x) / 2)
596 #define V2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
597 (pix->width * pix->height / 2) + \
598 (pix->width * (y) / 2) + (x) / 2)
599 #define UV_OFFSET(pix, x, y) ((pix->width * pix->height) + \
600 (pix->width * ((y) / 2)) + (x))
601 #define UV2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
602 (pix->width * y) + (x))
604 #define NUM_ALPHA_CHANNELS 7
606 /* See Table 37-12. Alpha channels mapping. */
607 static int ipu_channel_albm(int ch_num
)
610 case IPUV3_CHANNEL_G_MEM_IC_PRP_VF
: return 0;
611 case IPUV3_CHANNEL_G_MEM_IC_PP
: return 1;
612 case IPUV3_CHANNEL_MEM_FG_SYNC
: return 2;
613 case IPUV3_CHANNEL_MEM_FG_ASYNC
: return 3;
614 case IPUV3_CHANNEL_MEM_BG_SYNC
: return 4;
615 case IPUV3_CHANNEL_MEM_BG_ASYNC
: return 5;
616 case IPUV3_CHANNEL_MEM_VDI_PLANE1_COMB
: return 6;
622 static void ipu_cpmem_set_separate_alpha(struct ipuv3_channel
*ch
)
624 struct ipu_soc
*ipu
= ch
->ipu
;
628 albm
= ipu_channel_albm(ch
->num
);
632 ipu_ch_param_write_field(ch
, IPU_FIELD_ALU
, 1);
633 ipu_ch_param_write_field(ch
, IPU_FIELD_ALBM
, albm
);
634 ipu_ch_param_write_field(ch
, IPU_FIELD_CRE
, 1);
636 val
= ipu_idmac_read(ipu
, IDMAC_SEP_ALPHA
);
638 ipu_idmac_write(ipu
, val
, IDMAC_SEP_ALPHA
);
641 int ipu_cpmem_set_fmt(struct ipuv3_channel
*ch
, u32 drm_fourcc
)
643 switch (drm_fourcc
) {
644 case DRM_FORMAT_YUV420
:
645 case DRM_FORMAT_YVU420
:
647 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 2);
649 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
651 case DRM_FORMAT_YUV422
:
652 case DRM_FORMAT_YVU422
:
654 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 1);
656 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
658 case DRM_FORMAT_YUV444
:
659 case DRM_FORMAT_YVU444
:
661 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0);
663 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
665 case DRM_FORMAT_NV12
:
667 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 4);
669 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
671 case DRM_FORMAT_NV16
:
673 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 3);
675 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
677 case DRM_FORMAT_UYVY
:
679 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3);
681 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0xA);
683 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
685 case DRM_FORMAT_YUYV
:
687 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3);
689 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0x8);
691 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
693 case DRM_FORMAT_ABGR8888
:
694 case DRM_FORMAT_XBGR8888
:
695 ipu_cpmem_set_format_rgb(ch
, &def_xbgr_32
);
697 case DRM_FORMAT_ARGB8888
:
698 case DRM_FORMAT_XRGB8888
:
699 ipu_cpmem_set_format_rgb(ch
, &def_xrgb_32
);
701 case DRM_FORMAT_RGBA8888
:
702 case DRM_FORMAT_RGBX8888
:
703 case DRM_FORMAT_RGBX8888_A8
:
704 ipu_cpmem_set_format_rgb(ch
, &def_rgbx_32
);
706 case DRM_FORMAT_BGRA8888
:
707 case DRM_FORMAT_BGRX8888
:
708 case DRM_FORMAT_BGRX8888_A8
:
709 ipu_cpmem_set_format_rgb(ch
, &def_bgrx_32
);
711 case DRM_FORMAT_BGR888
:
712 case DRM_FORMAT_BGR888_A8
:
713 ipu_cpmem_set_format_rgb(ch
, &def_bgr_24
);
715 case DRM_FORMAT_RGB888
:
716 case DRM_FORMAT_RGB888_A8
:
717 ipu_cpmem_set_format_rgb(ch
, &def_rgb_24
);
719 case DRM_FORMAT_RGB565
:
720 case DRM_FORMAT_RGB565_A8
:
721 ipu_cpmem_set_format_rgb(ch
, &def_rgb_16
);
723 case DRM_FORMAT_BGR565
:
724 case DRM_FORMAT_BGR565_A8
:
725 ipu_cpmem_set_format_rgb(ch
, &def_bgr_16
);
727 case DRM_FORMAT_ARGB1555
:
728 ipu_cpmem_set_format_rgb(ch
, &def_argb_16
);
730 case DRM_FORMAT_ABGR1555
:
731 ipu_cpmem_set_format_rgb(ch
, &def_abgr_16
);
733 case DRM_FORMAT_RGBA5551
:
734 ipu_cpmem_set_format_rgb(ch
, &def_rgba_16
);
736 case DRM_FORMAT_BGRA5551
:
737 ipu_cpmem_set_format_rgb(ch
, &def_bgra_16
);
739 case DRM_FORMAT_ARGB4444
:
740 ipu_cpmem_set_format_rgb(ch
, &def_argb_16_4444
);
746 switch (drm_fourcc
) {
747 case DRM_FORMAT_RGB565_A8
:
748 case DRM_FORMAT_BGR565_A8
:
749 case DRM_FORMAT_RGB888_A8
:
750 case DRM_FORMAT_BGR888_A8
:
751 case DRM_FORMAT_RGBX8888_A8
:
752 case DRM_FORMAT_BGRX8888_A8
:
753 ipu_ch_param_write_field(ch
, IPU_FIELD_WID3
, 7);
754 ipu_cpmem_set_separate_alpha(ch
);
762 EXPORT_SYMBOL_GPL(ipu_cpmem_set_fmt
);
764 int ipu_cpmem_set_image(struct ipuv3_channel
*ch
, struct ipu_image
*image
)
766 struct v4l2_pix_format
*pix
= &image
->pix
;
767 int offset
, u_offset
, v_offset
;
770 pr_debug("%s: resolution: %dx%d stride: %d\n",
771 __func__
, pix
->width
, pix
->height
,
774 ipu_cpmem_set_resolution(ch
, image
->rect
.width
, image
->rect
.height
);
775 ipu_cpmem_set_stride(ch
, pix
->bytesperline
);
777 ipu_cpmem_set_fmt(ch
, v4l2_pix_fmt_to_drm_fourcc(pix
->pixelformat
));
779 switch (pix
->pixelformat
) {
780 case V4L2_PIX_FMT_YUV420
:
781 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
782 u_offset
= image
->u_offset
?
783 image
->u_offset
: U_OFFSET(pix
, image
->rect
.left
,
784 image
->rect
.top
) - offset
;
785 v_offset
= image
->v_offset
?
786 image
->v_offset
: V_OFFSET(pix
, image
->rect
.left
,
787 image
->rect
.top
) - offset
;
789 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
/ 2,
792 case V4L2_PIX_FMT_YVU420
:
793 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
794 u_offset
= image
->u_offset
?
795 image
->u_offset
: V_OFFSET(pix
, image
->rect
.left
,
796 image
->rect
.top
) - offset
;
797 v_offset
= image
->v_offset
?
798 image
->v_offset
: U_OFFSET(pix
, image
->rect
.left
,
799 image
->rect
.top
) - offset
;
801 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
/ 2,
804 case V4L2_PIX_FMT_YUV422P
:
805 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
806 u_offset
= image
->u_offset
?
807 image
->u_offset
: U2_OFFSET(pix
, image
->rect
.left
,
808 image
->rect
.top
) - offset
;
809 v_offset
= image
->v_offset
?
810 image
->v_offset
: V2_OFFSET(pix
, image
->rect
.left
,
811 image
->rect
.top
) - offset
;
813 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
/ 2,
816 case V4L2_PIX_FMT_NV12
:
817 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
818 u_offset
= image
->u_offset
?
819 image
->u_offset
: UV_OFFSET(pix
, image
->rect
.left
,
820 image
->rect
.top
) - offset
;
821 v_offset
= image
->v_offset
? image
->v_offset
: 0;
823 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
,
826 case V4L2_PIX_FMT_NV16
:
827 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
828 u_offset
= image
->u_offset
?
829 image
->u_offset
: UV2_OFFSET(pix
, image
->rect
.left
,
830 image
->rect
.top
) - offset
;
831 v_offset
= image
->v_offset
? image
->v_offset
: 0;
833 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
,
836 case V4L2_PIX_FMT_UYVY
:
837 case V4L2_PIX_FMT_YUYV
:
838 case V4L2_PIX_FMT_RGB565
:
839 offset
= image
->rect
.left
* 2 +
840 image
->rect
.top
* pix
->bytesperline
;
842 case V4L2_PIX_FMT_RGB32
:
843 case V4L2_PIX_FMT_BGR32
:
844 case V4L2_PIX_FMT_ABGR32
:
845 case V4L2_PIX_FMT_XBGR32
:
846 case V4L2_PIX_FMT_BGRA32
:
847 case V4L2_PIX_FMT_BGRX32
:
848 case V4L2_PIX_FMT_RGBA32
:
849 case V4L2_PIX_FMT_RGBX32
:
850 case V4L2_PIX_FMT_ARGB32
:
851 case V4L2_PIX_FMT_XRGB32
:
852 offset
= image
->rect
.left
* 4 +
853 image
->rect
.top
* pix
->bytesperline
;
855 case V4L2_PIX_FMT_RGB24
:
856 case V4L2_PIX_FMT_BGR24
:
857 offset
= image
->rect
.left
* 3 +
858 image
->rect
.top
* pix
->bytesperline
;
860 case V4L2_PIX_FMT_SBGGR8
:
861 case V4L2_PIX_FMT_SGBRG8
:
862 case V4L2_PIX_FMT_SGRBG8
:
863 case V4L2_PIX_FMT_SRGGB8
:
864 case V4L2_PIX_FMT_GREY
:
865 offset
= image
->rect
.left
+ image
->rect
.top
* pix
->bytesperline
;
867 case V4L2_PIX_FMT_SBGGR16
:
868 case V4L2_PIX_FMT_SGBRG16
:
869 case V4L2_PIX_FMT_SGRBG16
:
870 case V4L2_PIX_FMT_SRGGB16
:
871 case V4L2_PIX_FMT_Y16
:
872 offset
= image
->rect
.left
* 2 +
873 image
->rect
.top
* pix
->bytesperline
;
876 /* This should not happen */
882 ipu_cpmem_set_buffer(ch
, 0, image
->phys0
+ offset
);
883 ipu_cpmem_set_buffer(ch
, 1, image
->phys1
+ offset
);
887 EXPORT_SYMBOL_GPL(ipu_cpmem_set_image
);
889 void ipu_cpmem_dump(struct ipuv3_channel
*ch
)
891 struct ipu_ch_param __iomem
*p
= ipu_get_cpmem(ch
);
892 struct ipu_soc
*ipu
= ch
->ipu
;
895 dev_dbg(ipu
->dev
, "ch %d word 0 - %08X %08X %08X %08X %08X\n", chno
,
896 readl(&p
->word
[0].data
[0]),
897 readl(&p
->word
[0].data
[1]),
898 readl(&p
->word
[0].data
[2]),
899 readl(&p
->word
[0].data
[3]),
900 readl(&p
->word
[0].data
[4]));
901 dev_dbg(ipu
->dev
, "ch %d word 1 - %08X %08X %08X %08X %08X\n", chno
,
902 readl(&p
->word
[1].data
[0]),
903 readl(&p
->word
[1].data
[1]),
904 readl(&p
->word
[1].data
[2]),
905 readl(&p
->word
[1].data
[3]),
906 readl(&p
->word
[1].data
[4]));
907 dev_dbg(ipu
->dev
, "PFS 0x%x, ",
908 ipu_ch_param_read_field(ch
, IPU_FIELD_PFS
));
909 dev_dbg(ipu
->dev
, "BPP 0x%x, ",
910 ipu_ch_param_read_field(ch
, IPU_FIELD_BPP
));
911 dev_dbg(ipu
->dev
, "NPB 0x%x\n",
912 ipu_ch_param_read_field(ch
, IPU_FIELD_NPB
));
914 dev_dbg(ipu
->dev
, "FW %d, ",
915 ipu_ch_param_read_field(ch
, IPU_FIELD_FW
));
916 dev_dbg(ipu
->dev
, "FH %d, ",
917 ipu_ch_param_read_field(ch
, IPU_FIELD_FH
));
918 dev_dbg(ipu
->dev
, "EBA0 0x%x\n",
919 ipu_ch_param_read_field(ch
, IPU_FIELD_EBA0
) << 3);
920 dev_dbg(ipu
->dev
, "EBA1 0x%x\n",
921 ipu_ch_param_read_field(ch
, IPU_FIELD_EBA1
) << 3);
922 dev_dbg(ipu
->dev
, "Stride %d\n",
923 ipu_ch_param_read_field(ch
, IPU_FIELD_SL
));
924 dev_dbg(ipu
->dev
, "scan_order %d\n",
925 ipu_ch_param_read_field(ch
, IPU_FIELD_SO
));
926 dev_dbg(ipu
->dev
, "uv_stride %d\n",
927 ipu_ch_param_read_field(ch
, IPU_FIELD_SLUV
));
928 dev_dbg(ipu
->dev
, "u_offset 0x%x\n",
929 ipu_ch_param_read_field(ch
, IPU_FIELD_UBO
) << 3);
930 dev_dbg(ipu
->dev
, "v_offset 0x%x\n",
931 ipu_ch_param_read_field(ch
, IPU_FIELD_VBO
) << 3);
933 dev_dbg(ipu
->dev
, "Width0 %d+1, ",
934 ipu_ch_param_read_field(ch
, IPU_FIELD_WID0
));
935 dev_dbg(ipu
->dev
, "Width1 %d+1, ",
936 ipu_ch_param_read_field(ch
, IPU_FIELD_WID1
));
937 dev_dbg(ipu
->dev
, "Width2 %d+1, ",
938 ipu_ch_param_read_field(ch
, IPU_FIELD_WID2
));
939 dev_dbg(ipu
->dev
, "Width3 %d+1, ",
940 ipu_ch_param_read_field(ch
, IPU_FIELD_WID3
));
941 dev_dbg(ipu
->dev
, "Offset0 %d, ",
942 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS0
));
943 dev_dbg(ipu
->dev
, "Offset1 %d, ",
944 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS1
));
945 dev_dbg(ipu
->dev
, "Offset2 %d, ",
946 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS2
));
947 dev_dbg(ipu
->dev
, "Offset3 %d\n",
948 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS3
));
950 EXPORT_SYMBOL_GPL(ipu_cpmem_dump
);
952 int ipu_cpmem_init(struct ipu_soc
*ipu
, struct device
*dev
, unsigned long base
)
954 struct ipu_cpmem
*cpmem
;
956 cpmem
= devm_kzalloc(dev
, sizeof(*cpmem
), GFP_KERNEL
);
960 ipu
->cpmem_priv
= cpmem
;
962 spin_lock_init(&cpmem
->lock
);
963 cpmem
->base
= devm_ioremap(dev
, base
, SZ_128K
);
967 dev_dbg(dev
, "CPMEM base: 0x%08lx remapped to %p\n",
974 void ipu_cpmem_exit(struct ipu_soc
*ipu
)