2 * Copyright (C) 2012 Mentor Graphics Inc.
3 * Copyright 2005-2012 Freescale Semiconductor, Inc. All Rights Reserved.
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
12 #include <linux/types.h>
13 #include <linux/bitrev.h>
15 #include <drm/drm_fourcc.h>
18 struct ipu_cpmem_word
{
24 struct ipu_cpmem_word word
[2];
28 struct ipu_ch_param __iomem
*base
;
35 #define IPU_CPMEM_WORD(word, ofs, size) ((((word) * 160 + (ofs)) << 8) | (size))
37 #define IPU_FIELD_UBO IPU_CPMEM_WORD(0, 46, 22)
38 #define IPU_FIELD_VBO IPU_CPMEM_WORD(0, 68, 22)
39 #define IPU_FIELD_IOX IPU_CPMEM_WORD(0, 90, 4)
40 #define IPU_FIELD_RDRW IPU_CPMEM_WORD(0, 94, 1)
41 #define IPU_FIELD_SO IPU_CPMEM_WORD(0, 113, 1)
42 #define IPU_FIELD_SLY IPU_CPMEM_WORD(1, 102, 14)
43 #define IPU_FIELD_SLUV IPU_CPMEM_WORD(1, 128, 14)
45 #define IPU_FIELD_XV IPU_CPMEM_WORD(0, 0, 10)
46 #define IPU_FIELD_YV IPU_CPMEM_WORD(0, 10, 9)
47 #define IPU_FIELD_XB IPU_CPMEM_WORD(0, 19, 13)
48 #define IPU_FIELD_YB IPU_CPMEM_WORD(0, 32, 12)
49 #define IPU_FIELD_NSB_B IPU_CPMEM_WORD(0, 44, 1)
50 #define IPU_FIELD_CF IPU_CPMEM_WORD(0, 45, 1)
51 #define IPU_FIELD_SX IPU_CPMEM_WORD(0, 46, 12)
52 #define IPU_FIELD_SY IPU_CPMEM_WORD(0, 58, 11)
53 #define IPU_FIELD_NS IPU_CPMEM_WORD(0, 69, 10)
54 #define IPU_FIELD_SDX IPU_CPMEM_WORD(0, 79, 7)
55 #define IPU_FIELD_SM IPU_CPMEM_WORD(0, 86, 10)
56 #define IPU_FIELD_SCC IPU_CPMEM_WORD(0, 96, 1)
57 #define IPU_FIELD_SCE IPU_CPMEM_WORD(0, 97, 1)
58 #define IPU_FIELD_SDY IPU_CPMEM_WORD(0, 98, 7)
59 #define IPU_FIELD_SDRX IPU_CPMEM_WORD(0, 105, 1)
60 #define IPU_FIELD_SDRY IPU_CPMEM_WORD(0, 106, 1)
61 #define IPU_FIELD_BPP IPU_CPMEM_WORD(0, 107, 3)
62 #define IPU_FIELD_DEC_SEL IPU_CPMEM_WORD(0, 110, 2)
63 #define IPU_FIELD_DIM IPU_CPMEM_WORD(0, 112, 1)
64 #define IPU_FIELD_BNDM IPU_CPMEM_WORD(0, 114, 3)
65 #define IPU_FIELD_BM IPU_CPMEM_WORD(0, 117, 2)
66 #define IPU_FIELD_ROT IPU_CPMEM_WORD(0, 119, 1)
67 #define IPU_FIELD_ROT_HF_VF IPU_CPMEM_WORD(0, 119, 3)
68 #define IPU_FIELD_HF IPU_CPMEM_WORD(0, 120, 1)
69 #define IPU_FIELD_VF IPU_CPMEM_WORD(0, 121, 1)
70 #define IPU_FIELD_THE IPU_CPMEM_WORD(0, 122, 1)
71 #define IPU_FIELD_CAP IPU_CPMEM_WORD(0, 123, 1)
72 #define IPU_FIELD_CAE IPU_CPMEM_WORD(0, 124, 1)
73 #define IPU_FIELD_FW IPU_CPMEM_WORD(0, 125, 13)
74 #define IPU_FIELD_FH IPU_CPMEM_WORD(0, 138, 12)
75 #define IPU_FIELD_EBA0 IPU_CPMEM_WORD(1, 0, 29)
76 #define IPU_FIELD_EBA1 IPU_CPMEM_WORD(1, 29, 29)
77 #define IPU_FIELD_ILO IPU_CPMEM_WORD(1, 58, 20)
78 #define IPU_FIELD_NPB IPU_CPMEM_WORD(1, 78, 7)
79 #define IPU_FIELD_PFS IPU_CPMEM_WORD(1, 85, 4)
80 #define IPU_FIELD_ALU IPU_CPMEM_WORD(1, 89, 1)
81 #define IPU_FIELD_ALBM IPU_CPMEM_WORD(1, 90, 3)
82 #define IPU_FIELD_ID IPU_CPMEM_WORD(1, 93, 2)
83 #define IPU_FIELD_TH IPU_CPMEM_WORD(1, 95, 7)
84 #define IPU_FIELD_SL IPU_CPMEM_WORD(1, 102, 14)
85 #define IPU_FIELD_WID0 IPU_CPMEM_WORD(1, 116, 3)
86 #define IPU_FIELD_WID1 IPU_CPMEM_WORD(1, 119, 3)
87 #define IPU_FIELD_WID2 IPU_CPMEM_WORD(1, 122, 3)
88 #define IPU_FIELD_WID3 IPU_CPMEM_WORD(1, 125, 3)
89 #define IPU_FIELD_OFS0 IPU_CPMEM_WORD(1, 128, 5)
90 #define IPU_FIELD_OFS1 IPU_CPMEM_WORD(1, 133, 5)
91 #define IPU_FIELD_OFS2 IPU_CPMEM_WORD(1, 138, 5)
92 #define IPU_FIELD_OFS3 IPU_CPMEM_WORD(1, 143, 5)
93 #define IPU_FIELD_SXYS IPU_CPMEM_WORD(1, 148, 1)
94 #define IPU_FIELD_CRE IPU_CPMEM_WORD(1, 149, 1)
95 #define IPU_FIELD_DEC_SEL2 IPU_CPMEM_WORD(1, 150, 1)
97 static inline struct ipu_ch_param __iomem
*
98 ipu_get_cpmem(struct ipuv3_channel
*ch
)
100 struct ipu_cpmem
*cpmem
= ch
->ipu
->cpmem_priv
;
102 return cpmem
->base
+ ch
->num
;
105 static void ipu_ch_param_write_field(struct ipuv3_channel
*ch
, u32 wbs
, u32 v
)
107 struct ipu_ch_param __iomem
*base
= ipu_get_cpmem(ch
);
108 u32 bit
= (wbs
>> 8) % 160;
109 u32 size
= wbs
& 0xff;
110 u32 word
= (wbs
>> 8) / 160;
113 u32 mask
= (1 << size
) - 1;
116 pr_debug("%s %d %d %d\n", __func__
, word
, bit
, size
);
118 val
= readl(&base
->word
[word
].data
[i
]);
119 val
&= ~(mask
<< ofs
);
121 writel(val
, &base
->word
[word
].data
[i
]);
123 if ((bit
+ size
- 1) / 32 > i
) {
124 val
= readl(&base
->word
[word
].data
[i
+ 1]);
125 val
&= ~(mask
>> (ofs
? (32 - ofs
) : 0));
126 val
|= v
>> (ofs
? (32 - ofs
) : 0);
127 writel(val
, &base
->word
[word
].data
[i
+ 1]);
131 static u32
ipu_ch_param_read_field(struct ipuv3_channel
*ch
, u32 wbs
)
133 struct ipu_ch_param __iomem
*base
= ipu_get_cpmem(ch
);
134 u32 bit
= (wbs
>> 8) % 160;
135 u32 size
= wbs
& 0xff;
136 u32 word
= (wbs
>> 8) / 160;
139 u32 mask
= (1 << size
) - 1;
142 pr_debug("%s %d %d %d\n", __func__
, word
, bit
, size
);
144 val
= (readl(&base
->word
[word
].data
[i
]) >> ofs
) & mask
;
146 if ((bit
+ size
- 1) / 32 > i
) {
149 tmp
= readl(&base
->word
[word
].data
[i
+ 1]);
150 tmp
&= mask
>> (ofs
? (32 - ofs
) : 0);
151 val
|= tmp
<< (ofs
? (32 - ofs
) : 0);
158 * The V4L2 spec defines packed RGB formats in memory byte order, which from
159 * point of view of the IPU corresponds to little-endian words with the first
160 * component in the least significant bits.
161 * The DRM pixel formats and IPU internal representation are ordered the other
162 * way around, with the first named component ordered at the most significant
163 * bits. Further, V4L2 formats are not well defined:
164 * https://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html
165 * We choose the interpretation which matches GStreamer behavior.
167 static int v4l2_pix_fmt_to_drm_fourcc(u32 pixelformat
)
169 switch (pixelformat
) {
170 case V4L2_PIX_FMT_RGB565
:
172 * Here we choose the 'corrected' interpretation of RGBP, a
173 * little-endian 16-bit word with the red component at the most
175 * g[2:0]b[4:0] r[4:0]g[5:3] <=> [16:0] R:G:B
177 return DRM_FORMAT_RGB565
;
178 case V4L2_PIX_FMT_BGR24
:
179 /* B G R <=> [24:0] R:G:B */
180 return DRM_FORMAT_RGB888
;
181 case V4L2_PIX_FMT_RGB24
:
182 /* R G B <=> [24:0] B:G:R */
183 return DRM_FORMAT_BGR888
;
184 case V4L2_PIX_FMT_BGR32
:
185 /* B G R A <=> [32:0] A:B:G:R */
186 return DRM_FORMAT_XRGB8888
;
187 case V4L2_PIX_FMT_RGB32
:
188 /* R G B A <=> [32:0] A:B:G:R */
189 return DRM_FORMAT_XBGR8888
;
190 case V4L2_PIX_FMT_UYVY
:
191 return DRM_FORMAT_UYVY
;
192 case V4L2_PIX_FMT_YUYV
:
193 return DRM_FORMAT_YUYV
;
194 case V4L2_PIX_FMT_YUV420
:
195 return DRM_FORMAT_YUV420
;
196 case V4L2_PIX_FMT_YUV422P
:
197 return DRM_FORMAT_YUV422
;
198 case V4L2_PIX_FMT_YVU420
:
199 return DRM_FORMAT_YVU420
;
200 case V4L2_PIX_FMT_NV12
:
201 return DRM_FORMAT_NV12
;
202 case V4L2_PIX_FMT_NV16
:
203 return DRM_FORMAT_NV16
;
209 void ipu_cpmem_zero(struct ipuv3_channel
*ch
)
211 struct ipu_ch_param __iomem
*p
= ipu_get_cpmem(ch
);
212 void __iomem
*base
= p
;
215 for (i
= 0; i
< sizeof(*p
) / sizeof(u32
); i
++)
216 writel(0, base
+ i
* sizeof(u32
));
218 EXPORT_SYMBOL_GPL(ipu_cpmem_zero
);
220 void ipu_cpmem_set_resolution(struct ipuv3_channel
*ch
, int xres
, int yres
)
222 ipu_ch_param_write_field(ch
, IPU_FIELD_FW
, xres
- 1);
223 ipu_ch_param_write_field(ch
, IPU_FIELD_FH
, yres
- 1);
225 EXPORT_SYMBOL_GPL(ipu_cpmem_set_resolution
);
227 void ipu_cpmem_skip_odd_chroma_rows(struct ipuv3_channel
*ch
)
229 ipu_ch_param_write_field(ch
, IPU_FIELD_RDRW
, 1);
231 EXPORT_SYMBOL_GPL(ipu_cpmem_skip_odd_chroma_rows
);
233 void ipu_cpmem_set_stride(struct ipuv3_channel
*ch
, int stride
)
235 ipu_ch_param_write_field(ch
, IPU_FIELD_SLY
, stride
- 1);
237 EXPORT_SYMBOL_GPL(ipu_cpmem_set_stride
);
239 void ipu_cpmem_set_high_priority(struct ipuv3_channel
*ch
)
241 struct ipu_soc
*ipu
= ch
->ipu
;
244 if (ipu
->ipu_type
== IPUV3EX
)
245 ipu_ch_param_write_field(ch
, IPU_FIELD_ID
, 1);
247 val
= ipu_idmac_read(ipu
, IDMAC_CHA_PRI(ch
->num
));
248 val
|= 1 << (ch
->num
% 32);
249 ipu_idmac_write(ipu
, val
, IDMAC_CHA_PRI(ch
->num
));
251 EXPORT_SYMBOL_GPL(ipu_cpmem_set_high_priority
);
253 void ipu_cpmem_set_buffer(struct ipuv3_channel
*ch
, int bufnum
, dma_addr_t buf
)
256 ipu_ch_param_write_field(ch
, IPU_FIELD_EBA1
, buf
>> 3);
258 ipu_ch_param_write_field(ch
, IPU_FIELD_EBA0
, buf
>> 3);
260 EXPORT_SYMBOL_GPL(ipu_cpmem_set_buffer
);
262 void ipu_cpmem_set_uv_offset(struct ipuv3_channel
*ch
, u32 u_off
, u32 v_off
)
264 ipu_ch_param_write_field(ch
, IPU_FIELD_UBO
, u_off
/ 8);
265 ipu_ch_param_write_field(ch
, IPU_FIELD_VBO
, v_off
/ 8);
267 EXPORT_SYMBOL_GPL(ipu_cpmem_set_uv_offset
);
269 void ipu_cpmem_interlaced_scan(struct ipuv3_channel
*ch
, int stride
)
271 ipu_ch_param_write_field(ch
, IPU_FIELD_SO
, 1);
272 ipu_ch_param_write_field(ch
, IPU_FIELD_ILO
, stride
/ 8);
273 ipu_ch_param_write_field(ch
, IPU_FIELD_SLY
, (stride
* 2) - 1);
275 EXPORT_SYMBOL_GPL(ipu_cpmem_interlaced_scan
);
277 void ipu_cpmem_set_axi_id(struct ipuv3_channel
*ch
, u32 id
)
280 ipu_ch_param_write_field(ch
, IPU_FIELD_ID
, id
);
282 EXPORT_SYMBOL_GPL(ipu_cpmem_set_axi_id
);
284 int ipu_cpmem_get_burstsize(struct ipuv3_channel
*ch
)
286 return ipu_ch_param_read_field(ch
, IPU_FIELD_NPB
) + 1;
288 EXPORT_SYMBOL_GPL(ipu_cpmem_get_burstsize
);
290 void ipu_cpmem_set_burstsize(struct ipuv3_channel
*ch
, int burstsize
)
292 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, burstsize
- 1);
294 EXPORT_SYMBOL_GPL(ipu_cpmem_set_burstsize
);
296 void ipu_cpmem_set_block_mode(struct ipuv3_channel
*ch
)
298 ipu_ch_param_write_field(ch
, IPU_FIELD_BM
, 1);
300 EXPORT_SYMBOL_GPL(ipu_cpmem_set_block_mode
);
302 void ipu_cpmem_set_rotation(struct ipuv3_channel
*ch
,
303 enum ipu_rotate_mode rot
)
305 u32 temp_rot
= bitrev8(rot
) >> 5;
307 ipu_ch_param_write_field(ch
, IPU_FIELD_ROT_HF_VF
, temp_rot
);
309 EXPORT_SYMBOL_GPL(ipu_cpmem_set_rotation
);
311 int ipu_cpmem_set_format_rgb(struct ipuv3_channel
*ch
,
312 const struct ipu_rgb
*rgb
)
314 int bpp
= 0, npb
= 0, ro
, go
, bo
, to
;
316 ro
= rgb
->bits_per_pixel
- rgb
->red
.length
- rgb
->red
.offset
;
317 go
= rgb
->bits_per_pixel
- rgb
->green
.length
- rgb
->green
.offset
;
318 bo
= rgb
->bits_per_pixel
- rgb
->blue
.length
- rgb
->blue
.offset
;
319 to
= rgb
->bits_per_pixel
- rgb
->transp
.length
- rgb
->transp
.offset
;
321 ipu_ch_param_write_field(ch
, IPU_FIELD_WID0
, rgb
->red
.length
- 1);
322 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS0
, ro
);
323 ipu_ch_param_write_field(ch
, IPU_FIELD_WID1
, rgb
->green
.length
- 1);
324 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS1
, go
);
325 ipu_ch_param_write_field(ch
, IPU_FIELD_WID2
, rgb
->blue
.length
- 1);
326 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS2
, bo
);
328 if (rgb
->transp
.length
) {
329 ipu_ch_param_write_field(ch
, IPU_FIELD_WID3
,
330 rgb
->transp
.length
- 1);
331 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS3
, to
);
333 ipu_ch_param_write_field(ch
, IPU_FIELD_WID3
, 7);
334 ipu_ch_param_write_field(ch
, IPU_FIELD_OFS3
,
335 rgb
->bits_per_pixel
);
338 switch (rgb
->bits_per_pixel
) {
358 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, bpp
);
359 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, npb
);
360 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 7); /* rgb mode */
364 EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_rgb
);
366 int ipu_cpmem_set_format_passthrough(struct ipuv3_channel
*ch
, int width
)
368 int bpp
= 0, npb
= 0;
391 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, bpp
);
392 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, npb
);
393 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 6); /* raw mode */
397 EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_passthrough
);
399 void ipu_cpmem_set_yuv_interleaved(struct ipuv3_channel
*ch
, u32 pixel_format
)
401 switch (pixel_format
) {
402 case V4L2_PIX_FMT_UYVY
:
403 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3); /* bits/pixel */
404 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0xA);/* pix fmt */
405 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);/* burst size */
407 case V4L2_PIX_FMT_YUYV
:
408 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3); /* bits/pixel */
409 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0x8);/* pix fmt */
410 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);/* burst size */
414 EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_interleaved
);
416 void ipu_cpmem_set_yuv_planar_full(struct ipuv3_channel
*ch
,
417 unsigned int uv_stride
,
418 unsigned int u_offset
, unsigned int v_offset
)
420 ipu_ch_param_write_field(ch
, IPU_FIELD_SLUV
, uv_stride
- 1);
421 ipu_ch_param_write_field(ch
, IPU_FIELD_UBO
, u_offset
/ 8);
422 ipu_ch_param_write_field(ch
, IPU_FIELD_VBO
, v_offset
/ 8);
424 EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_planar_full
);
426 static const struct ipu_rgb def_xrgb_32
= {
427 .red
= { .offset
= 16, .length
= 8, },
428 .green
= { .offset
= 8, .length
= 8, },
429 .blue
= { .offset
= 0, .length
= 8, },
430 .transp
= { .offset
= 24, .length
= 8, },
431 .bits_per_pixel
= 32,
434 static const struct ipu_rgb def_xbgr_32
= {
435 .red
= { .offset
= 0, .length
= 8, },
436 .green
= { .offset
= 8, .length
= 8, },
437 .blue
= { .offset
= 16, .length
= 8, },
438 .transp
= { .offset
= 24, .length
= 8, },
439 .bits_per_pixel
= 32,
442 static const struct ipu_rgb def_rgbx_32
= {
443 .red
= { .offset
= 24, .length
= 8, },
444 .green
= { .offset
= 16, .length
= 8, },
445 .blue
= { .offset
= 8, .length
= 8, },
446 .transp
= { .offset
= 0, .length
= 8, },
447 .bits_per_pixel
= 32,
450 static const struct ipu_rgb def_bgrx_32
= {
451 .red
= { .offset
= 8, .length
= 8, },
452 .green
= { .offset
= 16, .length
= 8, },
453 .blue
= { .offset
= 24, .length
= 8, },
454 .transp
= { .offset
= 0, .length
= 8, },
455 .bits_per_pixel
= 32,
458 static const struct ipu_rgb def_rgb_24
= {
459 .red
= { .offset
= 16, .length
= 8, },
460 .green
= { .offset
= 8, .length
= 8, },
461 .blue
= { .offset
= 0, .length
= 8, },
462 .transp
= { .offset
= 0, .length
= 0, },
463 .bits_per_pixel
= 24,
466 static const struct ipu_rgb def_bgr_24
= {
467 .red
= { .offset
= 0, .length
= 8, },
468 .green
= { .offset
= 8, .length
= 8, },
469 .blue
= { .offset
= 16, .length
= 8, },
470 .transp
= { .offset
= 0, .length
= 0, },
471 .bits_per_pixel
= 24,
474 static const struct ipu_rgb def_rgb_16
= {
475 .red
= { .offset
= 11, .length
= 5, },
476 .green
= { .offset
= 5, .length
= 6, },
477 .blue
= { .offset
= 0, .length
= 5, },
478 .transp
= { .offset
= 0, .length
= 0, },
479 .bits_per_pixel
= 16,
482 static const struct ipu_rgb def_bgr_16
= {
483 .red
= { .offset
= 0, .length
= 5, },
484 .green
= { .offset
= 5, .length
= 6, },
485 .blue
= { .offset
= 11, .length
= 5, },
486 .transp
= { .offset
= 0, .length
= 0, },
487 .bits_per_pixel
= 16,
490 static const struct ipu_rgb def_argb_16
= {
491 .red
= { .offset
= 10, .length
= 5, },
492 .green
= { .offset
= 5, .length
= 5, },
493 .blue
= { .offset
= 0, .length
= 5, },
494 .transp
= { .offset
= 15, .length
= 1, },
495 .bits_per_pixel
= 16,
498 static const struct ipu_rgb def_argb_16_4444
= {
499 .red
= { .offset
= 8, .length
= 4, },
500 .green
= { .offset
= 4, .length
= 4, },
501 .blue
= { .offset
= 0, .length
= 4, },
502 .transp
= { .offset
= 12, .length
= 4, },
503 .bits_per_pixel
= 16,
506 static const struct ipu_rgb def_abgr_16
= {
507 .red
= { .offset
= 0, .length
= 5, },
508 .green
= { .offset
= 5, .length
= 5, },
509 .blue
= { .offset
= 10, .length
= 5, },
510 .transp
= { .offset
= 15, .length
= 1, },
511 .bits_per_pixel
= 16,
514 static const struct ipu_rgb def_rgba_16
= {
515 .red
= { .offset
= 11, .length
= 5, },
516 .green
= { .offset
= 6, .length
= 5, },
517 .blue
= { .offset
= 1, .length
= 5, },
518 .transp
= { .offset
= 0, .length
= 1, },
519 .bits_per_pixel
= 16,
522 static const struct ipu_rgb def_bgra_16
= {
523 .red
= { .offset
= 1, .length
= 5, },
524 .green
= { .offset
= 6, .length
= 5, },
525 .blue
= { .offset
= 11, .length
= 5, },
526 .transp
= { .offset
= 0, .length
= 1, },
527 .bits_per_pixel
= 16,
530 #define Y_OFFSET(pix, x, y) ((x) + pix->width * (y))
531 #define U_OFFSET(pix, x, y) ((pix->width * pix->height) + \
532 (pix->width * (y) / 4) + (x) / 2)
533 #define V_OFFSET(pix, x, y) ((pix->width * pix->height) + \
534 (pix->width * pix->height / 4) + \
535 (pix->width * (y) / 4) + (x) / 2)
536 #define U2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
537 (pix->width * (y) / 2) + (x) / 2)
538 #define V2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
539 (pix->width * pix->height / 2) + \
540 (pix->width * (y) / 2) + (x) / 2)
541 #define UV_OFFSET(pix, x, y) ((pix->width * pix->height) + \
542 (pix->width * (y) / 2) + (x))
543 #define UV2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
544 (pix->width * y) + (x))
546 #define NUM_ALPHA_CHANNELS 7
548 /* See Table 37-12. Alpha channels mapping. */
549 static int ipu_channel_albm(int ch_num
)
552 case IPUV3_CHANNEL_G_MEM_IC_PRP_VF
: return 0;
553 case IPUV3_CHANNEL_G_MEM_IC_PP
: return 1;
554 case IPUV3_CHANNEL_MEM_FG_SYNC
: return 2;
555 case IPUV3_CHANNEL_MEM_FG_ASYNC
: return 3;
556 case IPUV3_CHANNEL_MEM_BG_SYNC
: return 4;
557 case IPUV3_CHANNEL_MEM_BG_ASYNC
: return 5;
558 case IPUV3_CHANNEL_MEM_VDI_PLANE1_COMB
: return 6;
564 static void ipu_cpmem_set_separate_alpha(struct ipuv3_channel
*ch
)
566 struct ipu_soc
*ipu
= ch
->ipu
;
570 albm
= ipu_channel_albm(ch
->num
);
574 ipu_ch_param_write_field(ch
, IPU_FIELD_ALU
, 1);
575 ipu_ch_param_write_field(ch
, IPU_FIELD_ALBM
, albm
);
576 ipu_ch_param_write_field(ch
, IPU_FIELD_CRE
, 1);
578 val
= ipu_idmac_read(ipu
, IDMAC_SEP_ALPHA
);
580 ipu_idmac_write(ipu
, val
, IDMAC_SEP_ALPHA
);
583 int ipu_cpmem_set_fmt(struct ipuv3_channel
*ch
, u32 drm_fourcc
)
585 switch (drm_fourcc
) {
586 case DRM_FORMAT_YUV420
:
587 case DRM_FORMAT_YVU420
:
589 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 2);
591 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
593 case DRM_FORMAT_YUV422
:
594 case DRM_FORMAT_YVU422
:
596 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 1);
598 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
600 case DRM_FORMAT_YUV444
:
601 case DRM_FORMAT_YVU444
:
603 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0);
605 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
607 case DRM_FORMAT_NV12
:
609 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 4);
611 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
613 case DRM_FORMAT_NV16
:
615 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 3);
617 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
619 case DRM_FORMAT_UYVY
:
621 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3);
623 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0xA);
625 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
627 case DRM_FORMAT_YUYV
:
629 ipu_ch_param_write_field(ch
, IPU_FIELD_BPP
, 3);
631 ipu_ch_param_write_field(ch
, IPU_FIELD_PFS
, 0x8);
633 ipu_ch_param_write_field(ch
, IPU_FIELD_NPB
, 31);
635 case DRM_FORMAT_ABGR8888
:
636 case DRM_FORMAT_XBGR8888
:
637 ipu_cpmem_set_format_rgb(ch
, &def_xbgr_32
);
639 case DRM_FORMAT_ARGB8888
:
640 case DRM_FORMAT_XRGB8888
:
641 ipu_cpmem_set_format_rgb(ch
, &def_xrgb_32
);
643 case DRM_FORMAT_RGBA8888
:
644 case DRM_FORMAT_RGBX8888
:
645 case DRM_FORMAT_RGBX8888_A8
:
646 ipu_cpmem_set_format_rgb(ch
, &def_rgbx_32
);
648 case DRM_FORMAT_BGRA8888
:
649 case DRM_FORMAT_BGRX8888
:
650 case DRM_FORMAT_BGRX8888_A8
:
651 ipu_cpmem_set_format_rgb(ch
, &def_bgrx_32
);
653 case DRM_FORMAT_BGR888
:
654 case DRM_FORMAT_BGR888_A8
:
655 ipu_cpmem_set_format_rgb(ch
, &def_bgr_24
);
657 case DRM_FORMAT_RGB888
:
658 case DRM_FORMAT_RGB888_A8
:
659 ipu_cpmem_set_format_rgb(ch
, &def_rgb_24
);
661 case DRM_FORMAT_RGB565
:
662 case DRM_FORMAT_RGB565_A8
:
663 ipu_cpmem_set_format_rgb(ch
, &def_rgb_16
);
665 case DRM_FORMAT_BGR565
:
666 case DRM_FORMAT_BGR565_A8
:
667 ipu_cpmem_set_format_rgb(ch
, &def_bgr_16
);
669 case DRM_FORMAT_ARGB1555
:
670 ipu_cpmem_set_format_rgb(ch
, &def_argb_16
);
672 case DRM_FORMAT_ABGR1555
:
673 ipu_cpmem_set_format_rgb(ch
, &def_abgr_16
);
675 case DRM_FORMAT_RGBA5551
:
676 ipu_cpmem_set_format_rgb(ch
, &def_rgba_16
);
678 case DRM_FORMAT_BGRA5551
:
679 ipu_cpmem_set_format_rgb(ch
, &def_bgra_16
);
681 case DRM_FORMAT_ARGB4444
:
682 ipu_cpmem_set_format_rgb(ch
, &def_argb_16_4444
);
688 switch (drm_fourcc
) {
689 case DRM_FORMAT_RGB565_A8
:
690 case DRM_FORMAT_BGR565_A8
:
691 case DRM_FORMAT_RGB888_A8
:
692 case DRM_FORMAT_BGR888_A8
:
693 case DRM_FORMAT_RGBX8888_A8
:
694 case DRM_FORMAT_BGRX8888_A8
:
695 ipu_ch_param_write_field(ch
, IPU_FIELD_WID3
, 7);
696 ipu_cpmem_set_separate_alpha(ch
);
704 EXPORT_SYMBOL_GPL(ipu_cpmem_set_fmt
);
706 int ipu_cpmem_set_image(struct ipuv3_channel
*ch
, struct ipu_image
*image
)
708 struct v4l2_pix_format
*pix
= &image
->pix
;
709 int offset
, u_offset
, v_offset
;
712 pr_debug("%s: resolution: %dx%d stride: %d\n",
713 __func__
, pix
->width
, pix
->height
,
716 ipu_cpmem_set_resolution(ch
, image
->rect
.width
, image
->rect
.height
);
717 ipu_cpmem_set_stride(ch
, pix
->bytesperline
);
719 ipu_cpmem_set_fmt(ch
, v4l2_pix_fmt_to_drm_fourcc(pix
->pixelformat
));
721 switch (pix
->pixelformat
) {
722 case V4L2_PIX_FMT_YUV420
:
723 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
724 u_offset
= U_OFFSET(pix
, image
->rect
.left
,
725 image
->rect
.top
) - offset
;
726 v_offset
= V_OFFSET(pix
, image
->rect
.left
,
727 image
->rect
.top
) - offset
;
729 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
/ 2,
732 case V4L2_PIX_FMT_YVU420
:
733 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
734 u_offset
= U_OFFSET(pix
, image
->rect
.left
,
735 image
->rect
.top
) - offset
;
736 v_offset
= V_OFFSET(pix
, image
->rect
.left
,
737 image
->rect
.top
) - offset
;
739 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
/ 2,
742 case V4L2_PIX_FMT_YUV422P
:
743 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
744 u_offset
= U2_OFFSET(pix
, image
->rect
.left
,
745 image
->rect
.top
) - offset
;
746 v_offset
= V2_OFFSET(pix
, image
->rect
.left
,
747 image
->rect
.top
) - offset
;
749 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
/ 2,
752 case V4L2_PIX_FMT_NV12
:
753 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
754 u_offset
= UV_OFFSET(pix
, image
->rect
.left
,
755 image
->rect
.top
) - offset
;
758 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
,
761 case V4L2_PIX_FMT_NV16
:
762 offset
= Y_OFFSET(pix
, image
->rect
.left
, image
->rect
.top
);
763 u_offset
= UV2_OFFSET(pix
, image
->rect
.left
,
764 image
->rect
.top
) - offset
;
767 ipu_cpmem_set_yuv_planar_full(ch
, pix
->bytesperline
,
770 case V4L2_PIX_FMT_UYVY
:
771 case V4L2_PIX_FMT_YUYV
:
772 case V4L2_PIX_FMT_RGB565
:
773 offset
= image
->rect
.left
* 2 +
774 image
->rect
.top
* pix
->bytesperline
;
776 case V4L2_PIX_FMT_RGB32
:
777 case V4L2_PIX_FMT_BGR32
:
778 offset
= image
->rect
.left
* 4 +
779 image
->rect
.top
* pix
->bytesperline
;
781 case V4L2_PIX_FMT_RGB24
:
782 case V4L2_PIX_FMT_BGR24
:
783 offset
= image
->rect
.left
* 3 +
784 image
->rect
.top
* pix
->bytesperline
;
786 case V4L2_PIX_FMT_SBGGR8
:
787 case V4L2_PIX_FMT_SGBRG8
:
788 case V4L2_PIX_FMT_SGRBG8
:
789 case V4L2_PIX_FMT_SRGGB8
:
790 offset
= image
->rect
.left
+ image
->rect
.top
* pix
->bytesperline
;
792 case V4L2_PIX_FMT_SBGGR16
:
793 case V4L2_PIX_FMT_SGBRG16
:
794 case V4L2_PIX_FMT_SGRBG16
:
795 case V4L2_PIX_FMT_SRGGB16
:
796 offset
= image
->rect
.left
* 2 +
797 image
->rect
.top
* pix
->bytesperline
;
800 /* This should not happen */
806 ipu_cpmem_set_buffer(ch
, 0, image
->phys0
+ offset
);
807 ipu_cpmem_set_buffer(ch
, 1, image
->phys1
+ offset
);
811 EXPORT_SYMBOL_GPL(ipu_cpmem_set_image
);
813 void ipu_cpmem_dump(struct ipuv3_channel
*ch
)
815 struct ipu_ch_param __iomem
*p
= ipu_get_cpmem(ch
);
816 struct ipu_soc
*ipu
= ch
->ipu
;
819 dev_dbg(ipu
->dev
, "ch %d word 0 - %08X %08X %08X %08X %08X\n", chno
,
820 readl(&p
->word
[0].data
[0]),
821 readl(&p
->word
[0].data
[1]),
822 readl(&p
->word
[0].data
[2]),
823 readl(&p
->word
[0].data
[3]),
824 readl(&p
->word
[0].data
[4]));
825 dev_dbg(ipu
->dev
, "ch %d word 1 - %08X %08X %08X %08X %08X\n", chno
,
826 readl(&p
->word
[1].data
[0]),
827 readl(&p
->word
[1].data
[1]),
828 readl(&p
->word
[1].data
[2]),
829 readl(&p
->word
[1].data
[3]),
830 readl(&p
->word
[1].data
[4]));
831 dev_dbg(ipu
->dev
, "PFS 0x%x, ",
832 ipu_ch_param_read_field(ch
, IPU_FIELD_PFS
));
833 dev_dbg(ipu
->dev
, "BPP 0x%x, ",
834 ipu_ch_param_read_field(ch
, IPU_FIELD_BPP
));
835 dev_dbg(ipu
->dev
, "NPB 0x%x\n",
836 ipu_ch_param_read_field(ch
, IPU_FIELD_NPB
));
838 dev_dbg(ipu
->dev
, "FW %d, ",
839 ipu_ch_param_read_field(ch
, IPU_FIELD_FW
));
840 dev_dbg(ipu
->dev
, "FH %d, ",
841 ipu_ch_param_read_field(ch
, IPU_FIELD_FH
));
842 dev_dbg(ipu
->dev
, "EBA0 0x%x\n",
843 ipu_ch_param_read_field(ch
, IPU_FIELD_EBA0
) << 3);
844 dev_dbg(ipu
->dev
, "EBA1 0x%x\n",
845 ipu_ch_param_read_field(ch
, IPU_FIELD_EBA1
) << 3);
846 dev_dbg(ipu
->dev
, "Stride %d\n",
847 ipu_ch_param_read_field(ch
, IPU_FIELD_SL
));
848 dev_dbg(ipu
->dev
, "scan_order %d\n",
849 ipu_ch_param_read_field(ch
, IPU_FIELD_SO
));
850 dev_dbg(ipu
->dev
, "uv_stride %d\n",
851 ipu_ch_param_read_field(ch
, IPU_FIELD_SLUV
));
852 dev_dbg(ipu
->dev
, "u_offset 0x%x\n",
853 ipu_ch_param_read_field(ch
, IPU_FIELD_UBO
) << 3);
854 dev_dbg(ipu
->dev
, "v_offset 0x%x\n",
855 ipu_ch_param_read_field(ch
, IPU_FIELD_VBO
) << 3);
857 dev_dbg(ipu
->dev
, "Width0 %d+1, ",
858 ipu_ch_param_read_field(ch
, IPU_FIELD_WID0
));
859 dev_dbg(ipu
->dev
, "Width1 %d+1, ",
860 ipu_ch_param_read_field(ch
, IPU_FIELD_WID1
));
861 dev_dbg(ipu
->dev
, "Width2 %d+1, ",
862 ipu_ch_param_read_field(ch
, IPU_FIELD_WID2
));
863 dev_dbg(ipu
->dev
, "Width3 %d+1, ",
864 ipu_ch_param_read_field(ch
, IPU_FIELD_WID3
));
865 dev_dbg(ipu
->dev
, "Offset0 %d, ",
866 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS0
));
867 dev_dbg(ipu
->dev
, "Offset1 %d, ",
868 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS1
));
869 dev_dbg(ipu
->dev
, "Offset2 %d, ",
870 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS2
));
871 dev_dbg(ipu
->dev
, "Offset3 %d\n",
872 ipu_ch_param_read_field(ch
, IPU_FIELD_OFS3
));
874 EXPORT_SYMBOL_GPL(ipu_cpmem_dump
);
876 int ipu_cpmem_init(struct ipu_soc
*ipu
, struct device
*dev
, unsigned long base
)
878 struct ipu_cpmem
*cpmem
;
880 cpmem
= devm_kzalloc(dev
, sizeof(*cpmem
), GFP_KERNEL
);
884 ipu
->cpmem_priv
= cpmem
;
886 spin_lock_init(&cpmem
->lock
);
887 cpmem
->base
= devm_ioremap(dev
, base
, SZ_128K
);
891 dev_dbg(dev
, "CPMEM base: 0x%08lx remapped to %p\n",
898 void ipu_cpmem_exit(struct ipu_soc
*ipu
)