blob: d21b2779f526eca36f69f8116223c87232942557 [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;
22with HW.GFX.GMA.Power_And_Clocks;
23
24with HW.Debug;
25with GNAT.Source_Info;
26
27package body HW.GFX.GMA.Display_Probing
28is
29
Nico Huber5a3191f2018-06-04 14:42:13 +020030 function Port_Configured (Configs : Pipe_Configs; Port : Port_Type)
31 return Boolean is
32 (Configs (Primary).Port = Port or
33 Configs (Secondary).Port = Port or
34 Configs (Tertiary).Port = Port);
Nico Huber8c45bcf2016-11-20 17:30:57 +010035
36 -- DP and HDMI share physical pins.
Nico Huber5a3191f2018-06-04 14:42:13 +020037 function Sibling_Port (Port : Port_Type) return Port_Type is
38 (case Port is
39 when HDMI1 => DP1,
40 when HDMI2 => DP2,
41 when HDMI3 => DP3,
42 when DP1 => HDMI1,
43 when DP2 => HDMI2,
44 when DP3 => HDMI3,
45 when others => Disabled);
Nico Huber8c45bcf2016-11-20 17:30:57 +010046
Nico Huber5a3191f2018-06-04 14:42:13 +020047 function Has_Sibling_Port (Port : Port_Type) return Boolean is
48 (Sibling_Port (Port) /= Disabled);
Nico Huber8c45bcf2016-11-20 17:30:57 +010049
Nico Huber5a3191f2018-06-04 14:42:13 +020050 function Is_DVI_I (Port : Active_Port_Type) return Boolean is
51 (Config.Have_DVI_I and
52 (Port = Analog or
53 Config_Helpers.To_PCH_Port (Port) = Config.Analog_I2C_Port));
Nico Huber1bc496f2017-06-09 22:23:28 +020054
Nico Huber8c45bcf2016-11-20 17:30:57 +010055 procedure Read_EDID
56 (Raw_EDID : out EDID.Raw_EDID_Data;
57 Port : in Active_Port_Type;
58 Success : out Boolean)
Nico Huber8c45bcf2016-11-20 17:30:57 +010059 is
60 Raw_EDID_Length : GFX.I2C.Transfer_Length := Raw_EDID'Length;
61 begin
62 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
63
64 for I in 1 .. 2 loop
65 if Config_Helpers.To_Display_Type (Port) = DP then
66 -- May need power to read edid
67 declare
68 Temp_Configs : Pipe_Configs := Cur_Configs;
69 begin
70 Temp_Configs (Primary).Port := Port;
71 Power_And_Clocks.Power_Up (Cur_Configs, Temp_Configs);
72 end;
73
74 declare
75 DP_Port : constant GMA.DP_Port :=
76 (case Port is
77 when Internal => DP_A,
78 when DP1 => DP_B,
79 when DP2 => DP_C,
80 when DP3 => DP_D,
81 when others => GMA.DP_Port'First);
82 begin
83 DP_Aux_Ch.I2C_Read
84 (Port => DP_Port,
85 Address => 16#50#,
86 Length => Raw_EDID_Length,
87 Data => Raw_EDID,
88 Success => Success);
89 end;
90 else
91 I2C.I2C_Read
92 (Port => (if Port = Analog
93 then Config.Analog_I2C_Port
94 else Config_Helpers.To_PCH_Port (Port)),
95 Address => 16#50#,
96 Length => Raw_EDID_Length,
97 Data => Raw_EDID,
98 Success => Success);
99 end if;
100 exit when not Success; -- don't retry if reading itself failed
101
102 pragma Debug (Debug.Put_Buffer ("EDID", Raw_EDID, Raw_EDID_Length));
103 EDID.Sanitize (Raw_EDID, Success);
104 exit when Success;
105 end loop;
106 end Read_EDID;
107
108 procedure Probe_Port
109 (Pipe_Cfg : in out Pipe_Config;
110 Port : in Active_Port_Type;
111 Success : out Boolean)
112 with Pre => True
113 is
114 Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#);
115 begin
116 Success := Config.Valid_Port (Port);
117
118 if Success then
119 if Port = Internal then
Nico Huber6f9a50d2016-11-21 23:21:14 +0100120 Panel.Wait_On;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100121 end if;
122 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.");
135 if Has_Sibling_Port (Port) then
136 -- Probe sibling port too and bail out if something is detected.
137 -- This is a precaution for adapters that expose the pins of a
138 -- port for both HDMI/DVI and DP (like some ThinkPad docks). A
139 -- user might have attached both by accident and there are ru-
140 -- mors of displays that got fried by applying the wrong signal.
141 declare
142 Have_Sibling_EDID : Boolean;
143 begin
144 Read_EDID (Raw_EDID, Sibling_Port (Port), Have_Sibling_EDID);
145 if Have_Sibling_EDID then
146 Pipe_Cfg.Port := Disabled;
147 Success := False;
148 end if;
149 end;
150 end if;
151 pragma Warnings (GNATprove, On, "unused assignment to ""Raw_EDID""");
152 else
153 Success := False;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100154 end if;
155 end Probe_Port;
156
157 procedure Scan_Ports
Nico Huber4c7356d2016-12-16 14:22:32 +0100158 (Configs : out Pipe_Configs;
159 Ports : in Port_List := All_Ports;
160 Max_Pipe : in Pipe_Index := Pipe_Index'Last;
161 Keep_Power : in Boolean := False)
Nico Huber8c45bcf2016-11-20 17:30:57 +0100162 is
Nico Huber6f9a50d2016-11-21 23:21:14 +0100163 Probe_Internal : Boolean := False;
164
Nico Huber8c45bcf2016-11-20 17:30:57 +0100165 Port_Idx : Port_List_Range := Port_List_Range'First;
166 Success : Boolean;
167 begin
168 Configs := (Pipe_Index =>
169 (Port => Disabled,
170 Mode => Invalid_Mode,
Nico Hubera02b2c62018-01-09 15:58:34 +0100171 Cursor => Default_Cursor,
Nico Huber8c45bcf2016-11-20 17:30:57 +0100172 Framebuffer => Default_FB));
173
Nico Huber6f9a50d2016-11-21 23:21:14 +0100174 -- Turn panel on early to probe other ports during the power on delay.
175 for Idx in Port_List_Range loop
176 exit when Ports (Idx) = Disabled;
177 if Ports (Idx) = Internal then
178 Panel.On (Wait => False);
179 Probe_Internal := True;
180 exit;
181 end if;
182 end loop;
183
Nico Huber8c45bcf2016-11-20 17:30:57 +0100184 for Pipe in Pipe_Index range
185 Pipe_Index'First .. Pipe_Index'Min (Max_Pipe, Config.Max_Pipe)
186 loop
Nico Huber4fc6dc22019-05-10 13:01:29 +0200187 Success := False;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100188 while Ports (Port_Idx) /= Disabled loop
189 if not Port_Configured (Configs, Ports (Port_Idx)) and
190 (not Has_Sibling_Port (Ports (Port_Idx)) or
191 not Port_Configured (Configs, Sibling_Port (Ports (Port_Idx))))
192 then
193 Probe_Port (Configs (Pipe), Ports (Port_Idx), Success);
194 else
195 Success := False;
196 end if;
197
198 exit when Port_Idx = Port_List_Range'Last;
199 Port_Idx := Port_List_Range'Succ (Port_Idx);
200
201 exit when Success;
202 end loop;
Nico Huber4fc6dc22019-05-10 13:01:29 +0200203 exit when not Success;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100204 end loop;
205
206 -- Restore power settings
Nico Huber4c7356d2016-12-16 14:22:32 +0100207 if not Keep_Power then
208 Power_And_Clocks.Power_Set_To (Cur_Configs);
209 end if;
Nico Huber6f9a50d2016-11-21 23:21:14 +0100210
211 -- Turn panel power off if probing failed.
212 if Probe_Internal and not Port_Configured (Configs, Internal) then
213 Panel.Off;
214 end if;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100215 end Scan_Ports;
216
217end HW.GFX.GMA.Display_Probing;