xref: /aosp_15_r20/external/coreboot/src/soc/mediatek/common/display.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <cbfs.h>
4 #include <console/console.h>
5 #include <delay.h>
6 #include <edid.h>
7 #include <framebuffer_info.h>
8 #include <soc/ddp.h>
9 #include <soc/display.h>
10 #include <soc/dptx.h>
11 #include <soc/dsi.h>
12 #include <soc/mtcmos.h>
13 #include <stdio.h>
14 
get_mipi_cmd_from_cbfs(struct panel_description * desc)15 static struct panel_serializable_data *get_mipi_cmd_from_cbfs(struct panel_description *desc)
16 {
17 	/*
18 	 * The CBFS file name is panel-{MANUFACTURER}-${PANEL_NAME}, where MANUFACTURER is 3
19 	 * characters and PANEL_NAME is usually 13 characters.
20 	 */
21 	char cbfs_name[64];
22 	static union {
23 		u8 raw[4 * 1024];  /* Most panels only need < 2K. */
24 		struct panel_serializable_data s;
25 	} buffer;
26 
27 	if (!desc->name) {
28 		printk(BIOS_ERR, "Missing panel CBFS file name.\n");
29 		return NULL;
30 	}
31 
32 	snprintf(cbfs_name, sizeof(cbfs_name), "panel-%s", desc->name);
33 	if (cbfs_load(cbfs_name, buffer.raw, sizeof(buffer)))
34 		return &buffer.s;
35 
36 	printk(BIOS_ERR, "Missing %s in CBFS.\n", cbfs_name);
37 	return NULL;
38 }
39 
mtk_edp_init(struct edid * edid)40 __weak int mtk_edp_init(struct edid *edid)
41 {
42 	printk(BIOS_WARNING, "%s: Not supported\n", __func__);
43 	return -1;
44 }
45 
mtk_dsi_init(u32 mode_flags,u32 format,u32 lanes,const struct edid * edid,const u8 * init_commands)46 __weak int mtk_dsi_init(u32 mode_flags, u32 format, u32 lanes,
47 			const struct edid *edid, const u8 *init_commands)
48 {
49 	printk(BIOS_WARNING, "%s: Not supported\n", __func__);
50 	return -1;
51 }
52 
mtk_display_init(void)53 int mtk_display_init(void)
54 {
55 	struct edid edid;
56 	struct fb_info *info;
57 	const char *name;
58 	struct panel_description *panel = get_active_panel();
59 
60 	if (!panel || panel->disp_path == DISP_PATH_NONE) {
61 		printk(BIOS_ERR, "%s: Failed to get the active panel\n", __func__);
62 		return -1;
63 	}
64 
65 	printk(BIOS_INFO, "%s: Starting display initialization\n", __func__);
66 
67 	mtcmos_display_power_on();
68 	mtcmos_protect_display_bus();
69 
70 	if (panel->configure_backlight)
71 		panel->configure_backlight();
72 	if (panel->power_on)
73 		panel->power_on();
74 
75 	mtk_ddp_init();
76 
77 	if (panel->disp_path == DISP_PATH_EDP) {
78 		mdelay(200);
79 		if (mtk_edp_init(&edid) < 0) {
80 			printk(BIOS_ERR, "%s: Failed to initialize eDP\n", __func__);
81 			return -1;
82 		}
83 	} else {
84 		struct panel_serializable_data *mipi_data = NULL;
85 
86 		if (panel->get_edid) {
87 			if (panel->get_edid(&edid) < 0)
88 				return -1;
89 		} else {
90 			mipi_data = get_mipi_cmd_from_cbfs(panel);
91 			if (!mipi_data)
92 				return -1;
93 			edid = mipi_data->edid;
94 		}
95 
96 		u32 mipi_dsi_flags = (MIPI_DSI_MODE_VIDEO |
97 				      MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
98 				      MIPI_DSI_MODE_LPM |
99 				      MIPI_DSI_MODE_EOT_PACKET);
100 
101 		if (mtk_dsi_init(mipi_dsi_flags, MIPI_DSI_FMT_RGB888, 4, &edid,
102 				 mipi_data ? mipi_data->init : NULL) < 0) {
103 			printk(BIOS_ERR, "%s: Failed in DSI init\n", __func__);
104 			return -1;
105 		}
106 
107 		if (panel->post_power_on && panel->post_power_on(&edid) < 0) {
108 			printk(BIOS_ERR, "%s: Failed to post power on bridge\n", __func__);
109 			return -1;
110 		}
111 	}
112 
113 	name = edid.ascii_string;
114 	if (name[0] == '\0')
115 		name = "unknown name";
116 	printk(BIOS_INFO, "%s: '%s %s' %dx%d@%dHz\n", __func__,
117 	       edid.manufacturer_name, name, edid.mode.ha, edid.mode.va,
118 	       edid.mode.refresh);
119 
120 	edid_set_framebuffer_bits_per_pixel(&edid, 32, 0);
121 
122 	mtk_ddp_mode_set(&edid, panel->disp_path);
123 	info = fb_new_framebuffer_info_from_edid(&edid, (uintptr_t)0);
124 	if (info)
125 		fb_set_orientation(info, panel->orientation);
126 
127 	return 0;
128 }
129