1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2014
4 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
5 * Fabien Dessenne <fabien.dessenne@st.com>
6 * for STMicroelectronics.
9 #include <linux/moduleparam.h>
10 #include <linux/seq_file.h>
12 #include <drm/drm_print.h>
14 #include "sti_compositor.h"
15 #include "sti_mixer.h"
18 /* Module parameter to set the background color of the mixer */
19 static unsigned int bkg_color = 0x000000;
20 MODULE_PARM_DESC(bkgcolor, "Value of the background color 0xRRGGBB");
21 module_param_named(bkgcolor, bkg_color, int, 0644);
24 #define GAM_MIXER_CTL 0x00
25 #define GAM_MIXER_BKC 0x04
26 #define GAM_MIXER_BCO 0x0C
27 #define GAM_MIXER_BCS 0x10
28 #define GAM_MIXER_AVO 0x28
29 #define GAM_MIXER_AVS 0x2C
30 #define GAM_MIXER_CRB 0x34
31 #define GAM_MIXER_ACT 0x38
32 #define GAM_MIXER_MBP 0x3C
33 #define GAM_MIXER_MX0 0x80
35 /* id for depth of CRB reg */
36 #define GAM_DEPTH_VID0_ID 1
37 #define GAM_DEPTH_VID1_ID 2
38 #define GAM_DEPTH_GDP0_ID 3
39 #define GAM_DEPTH_GDP1_ID 4
40 #define GAM_DEPTH_GDP2_ID 5
41 #define GAM_DEPTH_GDP3_ID 6
42 #define GAM_DEPTH_MASK_ID 7
45 #define GAM_CTL_BACK_MASK BIT(0)
46 #define GAM_CTL_VID0_MASK BIT(1)
47 #define GAM_CTL_VID1_MASK BIT(2)
48 #define GAM_CTL_GDP0_MASK BIT(3)
49 #define GAM_CTL_GDP1_MASK BIT(4)
50 #define GAM_CTL_GDP2_MASK BIT(5)
51 #define GAM_CTL_GDP3_MASK BIT(6)
52 #define GAM_CTL_CURSOR_MASK BIT(9)
54 const char *sti_mixer_to_str(struct sti_mixer *mixer)
62 return "<UNKNOWN MIXER>";
66 static inline u32 sti_mixer_reg_read(struct sti_mixer *mixer, u32 reg_id)
68 return readl(mixer->regs + reg_id);
71 static inline void sti_mixer_reg_write(struct sti_mixer *mixer,
74 writel(val, mixer->regs + reg_id);
77 #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
78 sti_mixer_reg_read(mixer, reg))
80 static void mixer_dbg_ctl(struct seq_file *s, int val)
84 char *const disp_layer[] = {"BKG", "VID0", "VID1", "GDP0",
85 "GDP1", "GDP2", "GDP3"};
87 seq_puts(s, "\tEnabled: ");
88 for (i = 0; i < 7; i++) {
90 seq_printf(s, "%s ", disp_layer[i]);
102 seq_puts(s, "Nothing");
105 static void mixer_dbg_crb(struct seq_file *s, int val)
109 seq_puts(s, "\tDepth: ");
110 for (i = 0; i < GAM_MIXER_NB_DEPTH_LEVEL; i++) {
111 switch (val & GAM_DEPTH_MASK_ID) {
112 case GAM_DEPTH_VID0_ID:
115 case GAM_DEPTH_VID1_ID:
118 case GAM_DEPTH_GDP0_ID:
121 case GAM_DEPTH_GDP1_ID:
124 case GAM_DEPTH_GDP2_ID:
127 case GAM_DEPTH_GDP3_ID:
134 if (i < GAM_MIXER_NB_DEPTH_LEVEL - 1)
140 static void mixer_dbg_mxn(struct seq_file *s, void *addr)
144 for (i = 1; i < 8; i++)
145 seq_printf(s, "-0x%08X", (int)readl(addr + i * 4));
148 static int mixer_dbg_show(struct seq_file *s, void *arg)
150 struct drm_info_node *node = s->private;
151 struct sti_mixer *mixer = (struct sti_mixer *)node->info_ent->data;
153 seq_printf(s, "%s: (vaddr = 0x%p)",
154 sti_mixer_to_str(mixer), mixer->regs);
156 DBGFS_DUMP(GAM_MIXER_CTL);
157 mixer_dbg_ctl(s, sti_mixer_reg_read(mixer, GAM_MIXER_CTL));
158 DBGFS_DUMP(GAM_MIXER_BKC);
159 DBGFS_DUMP(GAM_MIXER_BCO);
160 DBGFS_DUMP(GAM_MIXER_BCS);
161 DBGFS_DUMP(GAM_MIXER_AVO);
162 DBGFS_DUMP(GAM_MIXER_AVS);
163 DBGFS_DUMP(GAM_MIXER_CRB);
164 mixer_dbg_crb(s, sti_mixer_reg_read(mixer, GAM_MIXER_CRB));
165 DBGFS_DUMP(GAM_MIXER_ACT);
166 DBGFS_DUMP(GAM_MIXER_MBP);
167 DBGFS_DUMP(GAM_MIXER_MX0);
168 mixer_dbg_mxn(s, mixer->regs + GAM_MIXER_MX0);
173 static struct drm_info_list mixer0_debugfs_files[] = {
174 { "mixer_main", mixer_dbg_show, 0, NULL },
177 static struct drm_info_list mixer1_debugfs_files[] = {
178 { "mixer_aux", mixer_dbg_show, 0, NULL },
181 void sti_mixer_debugfs_init(struct sti_mixer *mixer, struct drm_minor *minor)
184 struct drm_info_list *mixer_debugfs_files;
189 mixer_debugfs_files = mixer0_debugfs_files;
190 nb_files = ARRAY_SIZE(mixer0_debugfs_files);
193 mixer_debugfs_files = mixer1_debugfs_files;
194 nb_files = ARRAY_SIZE(mixer1_debugfs_files);
200 for (i = 0; i < nb_files; i++)
201 mixer_debugfs_files[i].data = mixer;
203 drm_debugfs_create_files(mixer_debugfs_files,
205 minor->debugfs_root, minor);
208 void sti_mixer_set_background_status(struct sti_mixer *mixer, bool enable)
210 u32 val = sti_mixer_reg_read(mixer, GAM_MIXER_CTL);
212 val &= ~GAM_CTL_BACK_MASK;
214 sti_mixer_reg_write(mixer, GAM_MIXER_CTL, val);
217 static void sti_mixer_set_background_color(struct sti_mixer *mixer,
220 sti_mixer_reg_write(mixer, GAM_MIXER_BKC, rgb);
223 static void sti_mixer_set_background_area(struct sti_mixer *mixer,
224 struct drm_display_mode *mode)
226 u32 ydo, xdo, yds, xds;
228 ydo = sti_vtg_get_line_number(*mode, 0);
229 yds = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
230 xdo = sti_vtg_get_pixel_number(*mode, 0);
231 xds = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
233 sti_mixer_reg_write(mixer, GAM_MIXER_BCO, ydo << 16 | xdo);
234 sti_mixer_reg_write(mixer, GAM_MIXER_BCS, yds << 16 | xds);
237 int sti_mixer_set_plane_depth(struct sti_mixer *mixer, struct sti_plane *plane)
239 int plane_id, depth = plane->drm_plane.state->normalized_zpos;
243 switch (plane->desc) {
245 plane_id = GAM_DEPTH_GDP0_ID;
248 plane_id = GAM_DEPTH_GDP1_ID;
251 plane_id = GAM_DEPTH_GDP2_ID;
254 plane_id = GAM_DEPTH_GDP3_ID;
257 plane_id = GAM_DEPTH_VID0_ID;
260 /* no need to set depth for cursor */
263 DRM_ERROR("Unknown plane %d\n", plane->desc);
267 /* Search if a previous depth was already assigned to the plane */
268 val = sti_mixer_reg_read(mixer, GAM_MIXER_CRB);
269 for (i = 0; i < GAM_MIXER_NB_DEPTH_LEVEL; i++) {
270 mask = GAM_DEPTH_MASK_ID << (3 * i);
271 if ((val & mask) == plane_id << (3 * i))
275 mask |= GAM_DEPTH_MASK_ID << (3 * depth);
276 plane_id = plane_id << (3 * depth);
278 DRM_DEBUG_DRIVER("%s %s depth=%d\n", sti_mixer_to_str(mixer),
279 sti_plane_to_str(plane), depth);
280 dev_dbg(mixer->dev, "GAM_MIXER_CRB val 0x%x mask 0x%x\n",
285 sti_mixer_reg_write(mixer, GAM_MIXER_CRB, val);
287 dev_dbg(mixer->dev, "Read GAM_MIXER_CRB 0x%x\n",
288 sti_mixer_reg_read(mixer, GAM_MIXER_CRB));
292 int sti_mixer_active_video_area(struct sti_mixer *mixer,
293 struct drm_display_mode *mode)
295 u32 ydo, xdo, yds, xds;
297 ydo = sti_vtg_get_line_number(*mode, 0);
298 yds = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
299 xdo = sti_vtg_get_pixel_number(*mode, 0);
300 xds = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
302 DRM_DEBUG_DRIVER("%s active video area xdo:%d ydo:%d xds:%d yds:%d\n",
303 sti_mixer_to_str(mixer), xdo, ydo, xds, yds);
304 sti_mixer_reg_write(mixer, GAM_MIXER_AVO, ydo << 16 | xdo);
305 sti_mixer_reg_write(mixer, GAM_MIXER_AVS, yds << 16 | xds);
307 sti_mixer_set_background_color(mixer, bkg_color);
309 sti_mixer_set_background_area(mixer, mode);
310 sti_mixer_set_background_status(mixer, true);
314 static u32 sti_mixer_get_plane_mask(struct sti_plane *plane)
316 switch (plane->desc) {
318 return GAM_CTL_BACK_MASK;
320 return GAM_CTL_GDP0_MASK;
322 return GAM_CTL_GDP1_MASK;
324 return GAM_CTL_GDP2_MASK;
326 return GAM_CTL_GDP3_MASK;
328 return GAM_CTL_VID0_MASK;
330 return GAM_CTL_CURSOR_MASK;
336 int sti_mixer_set_plane_status(struct sti_mixer *mixer,
337 struct sti_plane *plane, bool status)
341 DRM_DEBUG_DRIVER("%s %s %s\n", status ? "enable" : "disable",
342 sti_mixer_to_str(mixer), sti_plane_to_str(plane));
344 mask = sti_mixer_get_plane_mask(plane);
346 DRM_ERROR("Can't find layer mask\n");
350 val = sti_mixer_reg_read(mixer, GAM_MIXER_CTL);
352 val |= status ? mask : 0;
353 sti_mixer_reg_write(mixer, GAM_MIXER_CTL, val);
358 struct sti_mixer *sti_mixer_create(struct device *dev,
359 struct drm_device *drm_dev,
361 void __iomem *baseaddr)
363 struct sti_mixer *mixer = devm_kzalloc(dev, sizeof(*mixer), GFP_KERNEL);
365 dev_dbg(dev, "%s\n", __func__);
367 DRM_ERROR("Failed to allocated memory for mixer\n");
370 mixer->regs = baseaddr;
374 DRM_DEBUG_DRIVER("%s created. Regs=%p\n",
375 sti_mixer_to_str(mixer), mixer->regs);