2 * Copyright (C) STMicroelectronics SA 2014
3 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
4 * Fabien Dessenne <fabien.dessenne@st.com>
5 * for STMicroelectronics.
6 * License terms: GNU General Public License (GPL), version 2
9 #include "sti_compositor.h"
10 #include "sti_mixer.h"
13 /* Identity: G=Y , B=Cb , R=Cr */
14 static const u32 mixerColorSpaceMatIdentity
[] = {
15 0x10000000, 0x00000000, 0x10000000, 0x00001000,
16 0x00000000, 0x00000000, 0x00000000, 0x00000000
20 #define GAM_MIXER_CTL 0x00
21 #define GAM_MIXER_BKC 0x04
22 #define GAM_MIXER_BCO 0x0C
23 #define GAM_MIXER_BCS 0x10
24 #define GAM_MIXER_AVO 0x28
25 #define GAM_MIXER_AVS 0x2C
26 #define GAM_MIXER_CRB 0x34
27 #define GAM_MIXER_ACT 0x38
28 #define GAM_MIXER_MBP 0x3C
29 #define GAM_MIXER_MX0 0x80
31 /* id for depth of CRB reg */
32 #define GAM_DEPTH_VID0_ID 1
33 #define GAM_DEPTH_VID1_ID 2
34 #define GAM_DEPTH_GDP0_ID 3
35 #define GAM_DEPTH_GDP1_ID 4
36 #define GAM_DEPTH_GDP2_ID 5
37 #define GAM_DEPTH_GDP3_ID 6
38 #define GAM_DEPTH_MASK_ID 7
41 #define GAM_CTL_BACK_MASK BIT(0)
42 #define GAM_CTL_VID0_MASK BIT(1)
43 #define GAM_CTL_VID1_MASK BIT(2)
44 #define GAM_CTL_GDP0_MASK BIT(3)
45 #define GAM_CTL_GDP1_MASK BIT(4)
46 #define GAM_CTL_GDP2_MASK BIT(5)
47 #define GAM_CTL_GDP3_MASK BIT(6)
48 #define GAM_CTL_CURSOR_MASK BIT(9)
50 const char *sti_mixer_to_str(struct sti_mixer
*mixer
)
58 return "<UNKNOWN MIXER>";
62 static inline u32
sti_mixer_reg_read(struct sti_mixer
*mixer
, u32 reg_id
)
64 return readl(mixer
->regs
+ reg_id
);
67 static inline void sti_mixer_reg_write(struct sti_mixer
*mixer
,
70 writel(val
, mixer
->regs
+ reg_id
);
73 void sti_mixer_set_background_status(struct sti_mixer
*mixer
, bool enable
)
75 u32 val
= sti_mixer_reg_read(mixer
, GAM_MIXER_CTL
);
77 val
&= ~GAM_CTL_BACK_MASK
;
79 sti_mixer_reg_write(mixer
, GAM_MIXER_CTL
, val
);
82 static void sti_mixer_set_background_color(struct sti_mixer
*mixer
,
83 u8 red
, u8 green
, u8 blue
)
85 u32 val
= (red
<< 16) | (green
<< 8) | blue
;
87 sti_mixer_reg_write(mixer
, GAM_MIXER_BKC
, val
);
90 static void sti_mixer_set_background_area(struct sti_mixer
*mixer
,
91 struct drm_display_mode
*mode
)
93 u32 ydo
, xdo
, yds
, xds
;
95 ydo
= sti_vtg_get_line_number(*mode
, 0);
96 yds
= sti_vtg_get_line_number(*mode
, mode
->vdisplay
- 1);
97 xdo
= sti_vtg_get_pixel_number(*mode
, 0);
98 xds
= sti_vtg_get_pixel_number(*mode
, mode
->hdisplay
- 1);
100 sti_mixer_reg_write(mixer
, GAM_MIXER_BCO
, ydo
<< 16 | xdo
);
101 sti_mixer_reg_write(mixer
, GAM_MIXER_BCS
, yds
<< 16 | xds
);
104 int sti_mixer_set_layer_depth(struct sti_mixer
*mixer
, struct sti_layer
*layer
)
106 int layer_id
= 0, depth
= layer
->zorder
;
109 if (depth
>= GAM_MIXER_NB_DEPTH_LEVEL
)
112 switch (layer
->desc
) {
114 layer_id
= GAM_DEPTH_GDP0_ID
;
117 layer_id
= GAM_DEPTH_GDP1_ID
;
120 layer_id
= GAM_DEPTH_GDP2_ID
;
123 layer_id
= GAM_DEPTH_GDP3_ID
;
127 layer_id
= GAM_DEPTH_VID0_ID
;
130 layer_id
= GAM_DEPTH_VID1_ID
;
133 /* no need to set depth for cursor */
136 DRM_ERROR("Unknown layer %d\n", layer
->desc
);
139 mask
= GAM_DEPTH_MASK_ID
<< (3 * depth
);
140 layer_id
= layer_id
<< (3 * depth
);
142 DRM_DEBUG_DRIVER("%s %s depth=%d\n", sti_mixer_to_str(mixer
),
143 sti_layer_to_str(layer
), depth
);
144 dev_dbg(mixer
->dev
, "GAM_MIXER_CRB val 0x%x mask 0x%x\n",
147 val
= sti_mixer_reg_read(mixer
, GAM_MIXER_CRB
);
150 sti_mixer_reg_write(mixer
, GAM_MIXER_CRB
, val
);
152 dev_dbg(mixer
->dev
, "Read GAM_MIXER_CRB 0x%x\n",
153 sti_mixer_reg_read(mixer
, GAM_MIXER_CRB
));
157 int sti_mixer_active_video_area(struct sti_mixer
*mixer
,
158 struct drm_display_mode
*mode
)
160 u32 ydo
, xdo
, yds
, xds
;
162 ydo
= sti_vtg_get_line_number(*mode
, 0);
163 yds
= sti_vtg_get_line_number(*mode
, mode
->vdisplay
- 1);
164 xdo
= sti_vtg_get_pixel_number(*mode
, 0);
165 xds
= sti_vtg_get_pixel_number(*mode
, mode
->hdisplay
- 1);
167 DRM_DEBUG_DRIVER("%s active video area xdo:%d ydo:%d xds:%d yds:%d\n",
168 sti_mixer_to_str(mixer
), xdo
, ydo
, xds
, yds
);
169 sti_mixer_reg_write(mixer
, GAM_MIXER_AVO
, ydo
<< 16 | xdo
);
170 sti_mixer_reg_write(mixer
, GAM_MIXER_AVS
, yds
<< 16 | xds
);
172 sti_mixer_set_background_color(mixer
, 0xFF, 0, 0);
174 sti_mixer_set_background_area(mixer
, mode
);
175 sti_mixer_set_background_status(mixer
, true);
179 static u32
sti_mixer_get_layer_mask(struct sti_layer
*layer
)
181 switch (layer
->desc
) {
183 return GAM_CTL_BACK_MASK
;
185 return GAM_CTL_GDP0_MASK
;
187 return GAM_CTL_GDP1_MASK
;
189 return GAM_CTL_GDP2_MASK
;
191 return GAM_CTL_GDP3_MASK
;
194 return GAM_CTL_VID0_MASK
;
196 return GAM_CTL_VID1_MASK
;
198 return GAM_CTL_CURSOR_MASK
;
204 int sti_mixer_set_layer_status(struct sti_mixer
*mixer
,
205 struct sti_layer
*layer
, bool status
)
209 DRM_DEBUG_DRIVER("%s %s %s\n", status
? "enable" : "disable",
210 sti_mixer_to_str(mixer
), sti_layer_to_str(layer
));
212 mask
= sti_mixer_get_layer_mask(layer
);
214 DRM_ERROR("Can not find layer mask\n");
218 val
= sti_mixer_reg_read(mixer
, GAM_MIXER_CTL
);
220 val
|= status
? mask
: 0;
221 sti_mixer_reg_write(mixer
, GAM_MIXER_CTL
, val
);
226 void sti_mixer_clear_all_layers(struct sti_mixer
*mixer
)
230 DRM_DEBUG_DRIVER("%s clear all layer\n", sti_mixer_to_str(mixer
));
231 val
= sti_mixer_reg_read(mixer
, GAM_MIXER_CTL
) & 0xFFFF0000;
232 sti_mixer_reg_write(mixer
, GAM_MIXER_CTL
, val
);
235 void sti_mixer_set_matrix(struct sti_mixer
*mixer
)
239 for (i
= 0; i
< ARRAY_SIZE(mixerColorSpaceMatIdentity
); i
++)
240 sti_mixer_reg_write(mixer
, GAM_MIXER_MX0
+ (i
* 4),
241 mixerColorSpaceMatIdentity
[i
]);
244 struct sti_mixer
*sti_mixer_create(struct device
*dev
, int id
,
245 void __iomem
*baseaddr
)
247 struct sti_mixer
*mixer
= devm_kzalloc(dev
, sizeof(*mixer
), GFP_KERNEL
);
248 struct device_node
*np
= dev
->of_node
;
250 dev_dbg(dev
, "%s\n", __func__
);
252 DRM_ERROR("Failed to allocated memory for mixer\n");
255 mixer
->regs
= baseaddr
;
259 if (of_device_is_compatible(np
, "st,stih416-compositor"))
260 sti_mixer_set_matrix(mixer
);
262 DRM_DEBUG_DRIVER("%s created. Regs=%p\n",
263 sti_mixer_to_str(mixer
), mixer
->regs
);