blob: 67f8ddf318e7005fe4709f44d06ad9f8a025c7d1 [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,
46 when others => Disabled);
Nico Huber8c45bcf2016-11-20 17:30:57 +010047
Nico Huber5a3191f2018-06-04 14:42:13 +020048 function Has_Sibling_Port (Port : Port_Type) return Boolean is
49 (Sibling_Port (Port) /= Disabled);
Nico Huber8c45bcf2016-11-20 17:30:57 +010050
Nico Huber5a3191f2018-06-04 14:42:13 +020051 function Is_DVI_I (Port : Active_Port_Type) return Boolean is
52 (Config.Have_DVI_I and
53 (Port = Analog or
54 Config_Helpers.To_PCH_Port (Port) = Config.Analog_I2C_Port));
Nico Huber1bc496f2017-06-09 22:23:28 +020055
Nico Huber8c45bcf2016-11-20 17:30:57 +010056 procedure Read_EDID
57 (Raw_EDID : out EDID.Raw_EDID_Data;
58 Port : in Active_Port_Type;
59 Success : out Boolean)
Nico Huber8c45bcf2016-11-20 17:30:57 +010060 is
61 Raw_EDID_Length : GFX.I2C.Transfer_Length := Raw_EDID'Length;
62 begin
63 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
64
65 for I in 1 .. 2 loop
66 if Config_Helpers.To_Display_Type (Port) = DP then
Nico Huberb0bbdbc2019-09-27 22:32:21 +020067 -- May need power and CDClk to read EDID
Nico Huber8c45bcf2016-11-20 17:30:57 +010068 declare
69 Temp_Configs : Pipe_Configs := Cur_Configs;
70 begin
71 Temp_Configs (Primary).Port := Port;
72 Power_And_Clocks.Power_Up (Cur_Configs, Temp_Configs);
Nico Huberb0bbdbc2019-09-27 22:32:21 +020073 Power_And_Clocks.Enable_CDClk;
Nico Huber8c45bcf2016-11-20 17:30:57 +010074 end;
75
76 declare
77 DP_Port : constant GMA.DP_Port :=
78 (case Port is
Nico Huber8beafd72020-01-07 14:59:44 +010079 when eDP => DP_A,
Nico Huber8c45bcf2016-11-20 17:30:57 +010080 when DP1 => DP_B,
81 when DP2 => DP_C,
82 when DP3 => DP_D,
83 when others => GMA.DP_Port'First);
84 begin
85 DP_Aux_Ch.I2C_Read
86 (Port => DP_Port,
87 Address => 16#50#,
88 Length => Raw_EDID_Length,
89 Data => Raw_EDID,
90 Success => Success);
91 end;
92 else
93 I2C.I2C_Read
94 (Port => (if Port = Analog
95 then Config.Analog_I2C_Port
96 else Config_Helpers.To_PCH_Port (Port)),
97 Address => 16#50#,
98 Length => Raw_EDID_Length,
99 Data => Raw_EDID,
100 Success => Success);
101 end if;
102 exit when not Success; -- don't retry if reading itself failed
103
104 pragma Debug (Debug.Put_Buffer ("EDID", Raw_EDID, Raw_EDID_Length));
105 EDID.Sanitize (Raw_EDID, Success);
106 exit when Success;
107 end loop;
108 end Read_EDID;
109
110 procedure Probe_Port
111 (Pipe_Cfg : in out Pipe_Config;
112 Port : in Active_Port_Type;
113 Success : out Boolean)
114 with Pre => True
115 is
116 Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#);
117 begin
118 Success := Config.Valid_Port (Port);
119
120 if Success then
Nico Huber2bbd6e72020-01-07 18:22:59 +0100121 Panel.Wait_On (Config_Helpers.To_Panel (Port));
Nico Huber8c45bcf2016-11-20 17:30:57 +0100122 Read_EDID (Raw_EDID, Port, Success);
123 end if;
124
125 if Success and then
Nico Huber1bc496f2017-06-09 22:23:28 +0200126 ((not Is_DVI_I (Port) or EDID.Compatible_Display
127 (Raw_EDID, Config_Helpers.To_Display_Type (Port))) and
Nico Huber8c45bcf2016-11-20 17:30:57 +0100128 EDID.Has_Preferred_Mode (Raw_EDID))
129 then
130 Pipe_Cfg.Port := Port;
131 Pipe_Cfg.Mode := EDID.Preferred_Mode (Raw_EDID);
132
133 pragma Warnings (GNATprove, Off, "unused assignment to ""Raw_EDID""",
134 Reason => "We just want to check if it's readable.");
Nico Huber3c1ac182022-09-04 13:12:57 +0200135 pragma Warnings (GNATprove, Off, """Raw_EDID"" is set by * but*",
136 Reason => "We just want to check if it's readable.");
Nico Huber8c45bcf2016-11-20 17:30:57 +0100137 if Has_Sibling_Port (Port) then
138 -- Probe sibling port too and bail out if something is detected.
139 -- This is a precaution for adapters that expose the pins of a
140 -- port for both HDMI/DVI and DP (like some ThinkPad docks). A
141 -- user might have attached both by accident and there are ru-
142 -- mors of displays that got fried by applying the wrong signal.
143 declare
144 Have_Sibling_EDID : Boolean;
145 begin
146 Read_EDID (Raw_EDID, Sibling_Port (Port), Have_Sibling_EDID);
147 if Have_Sibling_EDID then
148 Pipe_Cfg.Port := Disabled;
149 Success := False;
150 end if;
151 end;
152 end if;
Nico Huber3c1ac182022-09-04 13:12:57 +0200153 pragma Warnings (GNATprove, On, """Raw_EDID"" is set by * but*");
Nico Huber8c45bcf2016-11-20 17:30:57 +0100154 pragma Warnings (GNATprove, On, "unused assignment to ""Raw_EDID""");
155 else
156 Success := False;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100157 end if;
158 end Probe_Port;
159
160 procedure Scan_Ports
Nico Huber4c7356d2016-12-16 14:22:32 +0100161 (Configs : out Pipe_Configs;
162 Ports : in Port_List := All_Ports;
163 Max_Pipe : in Pipe_Index := Pipe_Index'Last;
164 Keep_Power : in Boolean := False)
Nico Huber8c45bcf2016-11-20 17:30:57 +0100165 is
Nico Huber2bbd6e72020-01-07 18:22:59 +0100166 Probed_Panels : array (Valid_Panels) of Boolean := (others => False);
Nico Huber6f9a50d2016-11-21 23:21:14 +0100167
Nico Huber8c45bcf2016-11-20 17:30:57 +0100168 Port_Idx : Port_List_Range := Port_List_Range'First;
169 Success : Boolean;
170 begin
171 Configs := (Pipe_Index =>
172 (Port => Disabled,
173 Mode => Invalid_Mode,
Nico Hubera02b2c62018-01-09 15:58:34 +0100174 Cursor => Default_Cursor,
Nico Huber8c45bcf2016-11-20 17:30:57 +0100175 Framebuffer => Default_FB));
176
Nico Huber6f9a50d2016-11-21 23:21:14 +0100177 -- Turn panel on early to probe other ports during the power on delay.
178 for Idx in Port_List_Range loop
179 exit when Ports (Idx) = Disabled;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100180 declare
181 P : constant Panel_Control := Config_Helpers.To_Panel (Ports (Idx));
182 begin
183 if P /= No_Panel then
184 Panel.On (P, Wait => False);
185 Probed_Panels (P) := True;
186 end if;
187 end;
Nico Huber6f9a50d2016-11-21 23:21:14 +0100188 end loop;
189
Nico Huber8c45bcf2016-11-20 17:30:57 +0100190 for Pipe in Pipe_Index range
191 Pipe_Index'First .. Pipe_Index'Min (Max_Pipe, Config.Max_Pipe)
192 loop
Nico Huber4fc6dc22019-05-10 13:01:29 +0200193 Success := False;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100194 while Ports (Port_Idx) /= Disabled loop
195 if not Port_Configured (Configs, Ports (Port_Idx)) and
196 (not Has_Sibling_Port (Ports (Port_Idx)) or
197 not Port_Configured (Configs, Sibling_Port (Ports (Port_Idx))))
198 then
199 Probe_Port (Configs (Pipe), Ports (Port_Idx), Success);
200 else
201 Success := False;
202 end if;
203
204 exit when Port_Idx = Port_List_Range'Last;
205 Port_Idx := Port_List_Range'Succ (Port_Idx);
206
207 exit when Success;
208 end loop;
Nico Huber4fc6dc22019-05-10 13:01:29 +0200209 exit when not Success;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100210 end loop;
211
212 -- Restore power settings
Nico Huber4c7356d2016-12-16 14:22:32 +0100213 if not Keep_Power then
214 Power_And_Clocks.Power_Set_To (Cur_Configs);
215 end if;
Nico Huber6f9a50d2016-11-21 23:21:14 +0100216
217 -- Turn panel power off if probing failed.
Nico Huber2bbd6e72020-01-07 18:22:59 +0100218 for P in Valid_Panels loop
219 if Probed_Panels (P) and not
220 Port_Configured (Configs, Config.Panel_Ports (P))
221 then
222 Panel.Off (P);
223 end if;
224 end loop;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100225 end Scan_Ports;
226
Nico Huber92de9c42019-09-29 19:03:58 +0200227 procedure Hotplug_Events (Ports : out Port_List)
228 is
229 I : Port_List_Range := Port_List_Range'First;
230 Detected : Boolean;
231 begin
232 Ports := (others => Disabled);
233 for P in Active_Port_Type loop
234 Port_Detect.Hotplug_Detect (P, Detected);
235 if Detected then
236 Ports (I) := P;
237 exit when I = Port_List_Range'Last;
238 I := Port_List_Range'Succ (I);
239 end if;
240 end loop;
241 end Hotplug_Events;
242
Nico Huber8c45bcf2016-11-20 17:30:57 +0100243end HW.GFX.GMA.Display_Probing;