blob: 72df4ed28ff3e2cf9b77d4d24cdb4ef8aafe97aa [file] [log] [blame]
Nico Huber8c45bcf2016-11-20 17:30:57 +01001--
2-- Copyright (C) 2015-2016 secunet Security Networks AG
Nico Huber4fc6dc22019-05-10 13:01:29 +02003-- Copyright (C) 2019 Nico Huber <nico.h@gmx.de>
Nico Huber8c45bcf2016-11-20 17:30:57 +01004--
5-- This program is free software; you can redistribute it and/or modify
6-- it under the terms of the GNU General Public License as published by
7-- the Free Software Foundation; either version 2 of the License, or
8-- (at your option) any later version.
9--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-- GNU General Public License for more details.
14--
15
16with HW.GFX.I2C;
Nico Huber8c45bcf2016-11-20 17:30:57 +010017with HW.GFX.GMA.Config;
18with HW.GFX.GMA.Config_Helpers;
19with HW.GFX.GMA.I2C;
20with HW.GFX.GMA.DP_Aux_Ch;
21with HW.GFX.GMA.Panel;
Nico Huber92de9c42019-09-29 19:03:58 +020022with HW.GFX.GMA.Port_Detect;
Nico Huber8c45bcf2016-11-20 17:30:57 +010023with HW.GFX.GMA.Power_And_Clocks;
24
25with HW.Debug;
26with GNAT.Source_Info;
27
28package body HW.GFX.GMA.Display_Probing
29is
30
Nico Huber5a3191f2018-06-04 14:42:13 +020031 function Port_Configured (Configs : Pipe_Configs; Port : Port_Type)
32 return Boolean is
33 (Configs (Primary).Port = Port or
34 Configs (Secondary).Port = Port or
35 Configs (Tertiary).Port = Port);
Nico Huber8c45bcf2016-11-20 17:30:57 +010036
37 -- DP and HDMI share physical pins.
Nico Huber5a3191f2018-06-04 14:42:13 +020038 function Sibling_Port (Port : Port_Type) return Port_Type is
39 (case Port is
40 when HDMI1 => DP1,
41 when HDMI2 => DP2,
42 when HDMI3 => DP3,
43 when DP1 => HDMI1,
44 when DP2 => HDMI2,
45 when DP3 => HDMI3,
Nico Huber8b381fc2024-03-26 18:04:19 +010046 when HDMI_TC1 => DP_TC1,
47 when HDMI_TC2 => DP_TC2,
48 when HDMI_TC3 => DP_TC3,
49 when HDMI_TC4 => DP_TC4,
50 when DP_TC1 => HDMI_TC1,
51 when DP_TC2 => HDMI_TC2,
52 when DP_TC3 => HDMI_TC3,
53 when DP_TC4 => HDMI_TC4,
Nico Huber5a3191f2018-06-04 14:42:13 +020054 when others => Disabled);
Nico Huber8c45bcf2016-11-20 17:30:57 +010055
Nico Huber5a3191f2018-06-04 14:42:13 +020056 function Has_Sibling_Port (Port : Port_Type) return Boolean is
57 (Sibling_Port (Port) /= Disabled);
Nico Huber8c45bcf2016-11-20 17:30:57 +010058
Nico Huber5a3191f2018-06-04 14:42:13 +020059 function Is_DVI_I (Port : Active_Port_Type) return Boolean is
60 (Config.Have_DVI_I and
61 (Port = Analog or
62 Config_Helpers.To_PCH_Port (Port) = Config.Analog_I2C_Port));
Nico Huber1bc496f2017-06-09 22:23:28 +020063
Nico Huber8c45bcf2016-11-20 17:30:57 +010064 procedure Read_EDID
65 (Raw_EDID : out EDID.Raw_EDID_Data;
66 Port : in Active_Port_Type;
67 Success : out Boolean)
Nico Huber8c45bcf2016-11-20 17:30:57 +010068 is
69 Raw_EDID_Length : GFX.I2C.Transfer_Length := Raw_EDID'Length;
70 begin
71 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
72
73 for I in 1 .. 2 loop
74 if Config_Helpers.To_Display_Type (Port) = DP then
Nico Huberb0bbdbc2019-09-27 22:32:21 +020075 -- May need power and CDClk to read EDID
Nico Huber41e86742024-07-17 17:10:28 +020076 Power_And_Clocks.Power_Up (Port, Success);
77 exit when not Success;
78 Power_And_Clocks.Enable_CDClk;
Nico Huber8c45bcf2016-11-20 17:30:57 +010079
80 declare
81 DP_Port : constant GMA.DP_Port :=
Nico Huber8b381fc2024-03-26 18:04:19 +010082 (if Config.Has_Type_C_Ports then
83 (case Config_Helpers.To_GPU_Port (Pipe_Index'First, Port) is
84 when DIGI_A => DP_A,
85 when DIGI_B => DP_B,
86 when DIGI_C => DP_C,
87 when DDI_TC1 => DP_D,
88 when DDI_TC2 => DP_E,
89 when DDI_TC3 => DP_F,
90 when DDI_TC4 => DP_G,
91 when DDI_TC5 => DP_H,
92 when DDI_TC6 => DP_I,
93 when others => GMA.DP_Port'First)
94 else
95 (case Port is
96 when eDP => DP_A,
97 when DP1 => DP_B,
98 when DP2 => DP_C,
99 when DP3 => DP_D,
100 when others => GMA.DP_Port'First));
Nico Huber8c45bcf2016-11-20 17:30:57 +0100101 begin
102 DP_Aux_Ch.I2C_Read
103 (Port => DP_Port,
104 Address => 16#50#,
105 Length => Raw_EDID_Length,
106 Data => Raw_EDID,
107 Success => Success);
108 end;
109 else
110 I2C.I2C_Read
111 (Port => (if Port = Analog
112 then Config.Analog_I2C_Port
113 else Config_Helpers.To_PCH_Port (Port)),
114 Address => 16#50#,
115 Length => Raw_EDID_Length,
116 Data => Raw_EDID,
117 Success => Success);
118 end if;
119 exit when not Success; -- don't retry if reading itself failed
120
121 pragma Debug (Debug.Put_Buffer ("EDID", Raw_EDID, Raw_EDID_Length));
122 EDID.Sanitize (Raw_EDID, Success);
123 exit when Success;
124 end loop;
125 end Read_EDID;
126
127 procedure Probe_Port
128 (Pipe_Cfg : in out Pipe_Config;
129 Port : in Active_Port_Type;
130 Success : out Boolean)
131 with Pre => True
132 is
Nico Huber41e86742024-07-17 17:10:28 +0200133 Raw_EDID : EDID.Raw_EDID_Data with Relaxed_Initialization;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100134 begin
135 Success := Config.Valid_Port (Port);
136
137 if Success then
Nico Huber2bbd6e72020-01-07 18:22:59 +0100138 Panel.Wait_On (Config_Helpers.To_Panel (Port));
Nico Huber8c45bcf2016-11-20 17:30:57 +0100139 Read_EDID (Raw_EDID, Port, Success);
140 end if;
141
142 if Success and then
Nico Huber1bc496f2017-06-09 22:23:28 +0200143 ((not Is_DVI_I (Port) or EDID.Compatible_Display
144 (Raw_EDID, Config_Helpers.To_Display_Type (Port))) and
Nico Huber8c45bcf2016-11-20 17:30:57 +0100145 EDID.Has_Preferred_Mode (Raw_EDID))
146 then
147 Pipe_Cfg.Port := Port;
148 Pipe_Cfg.Mode := EDID.Preferred_Mode (Raw_EDID);
149
150 pragma Warnings (GNATprove, Off, "unused assignment to ""Raw_EDID""",
151 Reason => "We just want to check if it's readable.");
Nico Huber3c1ac182022-09-04 13:12:57 +0200152 pragma Warnings (GNATprove, Off, """Raw_EDID"" is set by * but*",
153 Reason => "We just want to check if it's readable.");
Nico Huber8c45bcf2016-11-20 17:30:57 +0100154 if Has_Sibling_Port (Port) then
155 -- Probe sibling port too and bail out if something is detected.
156 -- This is a precaution for adapters that expose the pins of a
157 -- port for both HDMI/DVI and DP (like some ThinkPad docks). A
158 -- user might have attached both by accident and there are ru-
159 -- mors of displays that got fried by applying the wrong signal.
160 declare
161 Have_Sibling_EDID : Boolean;
162 begin
163 Read_EDID (Raw_EDID, Sibling_Port (Port), Have_Sibling_EDID);
164 if Have_Sibling_EDID then
165 Pipe_Cfg.Port := Disabled;
166 Success := False;
167 end if;
168 end;
169 end if;
Nico Huber3c1ac182022-09-04 13:12:57 +0200170 pragma Warnings (GNATprove, On, """Raw_EDID"" is set by * but*");
Nico Huber8c45bcf2016-11-20 17:30:57 +0100171 pragma Warnings (GNATprove, On, "unused assignment to ""Raw_EDID""");
172 else
173 Success := False;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100174 end if;
175 end Probe_Port;
176
177 procedure Scan_Ports
Nico Huber4c7356d2016-12-16 14:22:32 +0100178 (Configs : out Pipe_Configs;
179 Ports : in Port_List := All_Ports;
180 Max_Pipe : in Pipe_Index := Pipe_Index'Last;
181 Keep_Power : in Boolean := False)
Nico Huber8c45bcf2016-11-20 17:30:57 +0100182 is
Nico Huber2bbd6e72020-01-07 18:22:59 +0100183 Probed_Panels : array (Valid_Panels) of Boolean := (others => False);
Nico Huber6f9a50d2016-11-21 23:21:14 +0100184
Nico Huber8c45bcf2016-11-20 17:30:57 +0100185 Port_Idx : Port_List_Range := Port_List_Range'First;
186 Success : Boolean;
187 begin
188 Configs := (Pipe_Index =>
189 (Port => Disabled,
190 Mode => Invalid_Mode,
Nico Hubera02b2c62018-01-09 15:58:34 +0100191 Cursor => Default_Cursor,
Nico Huber8c45bcf2016-11-20 17:30:57 +0100192 Framebuffer => Default_FB));
193
Nico Huber6f9a50d2016-11-21 23:21:14 +0100194 -- Turn panel on early to probe other ports during the power on delay.
195 for Idx in Port_List_Range loop
196 exit when Ports (Idx) = Disabled;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100197 declare
198 P : constant Panel_Control := Config_Helpers.To_Panel (Ports (Idx));
199 begin
200 if P /= No_Panel then
201 Panel.On (P, Wait => False);
202 Probed_Panels (P) := True;
203 end if;
204 end;
Nico Huber6f9a50d2016-11-21 23:21:14 +0100205 end loop;
206
Nico Huber8c45bcf2016-11-20 17:30:57 +0100207 for Pipe in Pipe_Index range
208 Pipe_Index'First .. Pipe_Index'Min (Max_Pipe, Config.Max_Pipe)
209 loop
Nico Huber4fc6dc22019-05-10 13:01:29 +0200210 Success := False;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100211 while Ports (Port_Idx) /= Disabled loop
212 if not Port_Configured (Configs, Ports (Port_Idx)) and
213 (not Has_Sibling_Port (Ports (Port_Idx)) or
214 not Port_Configured (Configs, Sibling_Port (Ports (Port_Idx))))
215 then
216 Probe_Port (Configs (Pipe), Ports (Port_Idx), Success);
217 else
218 Success := False;
219 end if;
220
221 exit when Port_Idx = Port_List_Range'Last;
222 Port_Idx := Port_List_Range'Succ (Port_Idx);
223
224 exit when Success;
225 end loop;
Nico Huber4fc6dc22019-05-10 13:01:29 +0200226 exit when not Success;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100227 end loop;
228
Arthur Heymans960e2392026-03-03 19:45:24 +0100229 -- On pre-i965 hardware, LVDS must use Pipe B (Secondary).
230 -- If the scan assigned LVDS to Primary, swap with Secondary.
231 if Config.LVDS_Needs_Pipe_B and
232 Configs (Primary).Port = LVDS
233 then
234 declare
235 Tmp : constant Pipe_Config := Configs (Secondary);
236 begin
237 Configs (Secondary) := Configs (Primary);
238 Configs (Primary) := Tmp;
239 end;
240 end if;
241
Nico Huber8c45bcf2016-11-20 17:30:57 +0100242 -- Restore power settings
Nico Huber4c7356d2016-12-16 14:22:32 +0100243 if not Keep_Power then
244 Power_And_Clocks.Power_Set_To (Cur_Configs);
245 end if;
Nico Huber6f9a50d2016-11-21 23:21:14 +0100246
247 -- Turn panel power off if probing failed.
Nico Huber2bbd6e72020-01-07 18:22:59 +0100248 for P in Valid_Panels loop
249 if Probed_Panels (P) and not
250 Port_Configured (Configs, Config.Panel_Ports (P))
251 then
252 Panel.Off (P);
253 end if;
254 end loop;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100255 end Scan_Ports;
256
Nico Huber92de9c42019-09-29 19:03:58 +0200257 procedure Hotplug_Events (Ports : out Port_List)
258 is
259 I : Port_List_Range := Port_List_Range'First;
260 Detected : Boolean;
261 begin
262 Ports := (others => Disabled);
263 for P in Active_Port_Type loop
264 Port_Detect.Hotplug_Detect (P, Detected);
265 if Detected then
266 Ports (I) := P;
267 exit when I = Port_List_Range'Last;
268 I := Port_List_Range'Succ (I);
269 end if;
270 end loop;
271 end Hotplug_Events;
272
Nico Huber8c45bcf2016-11-20 17:30:57 +0100273end HW.GFX.GMA.Display_Probing;