blob: 4420a660b0936b46429d3c00ac1f07d01da90386 [file] [log] [blame]
Nico Huber8c45bcf2016-11-20 17:30:57 +01001--
2-- Copyright (C) 2015-2016 secunet Security Networks AG
3--
4-- This program is free software; you can redistribute it and/or modify
5-- it under the terms of the GNU General Public License as published by
6-- the Free Software Foundation; either version 2 of the License, or
7-- (at your option) any later version.
8--
9-- This program is distributed in the hope that it will be useful,
10-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-- GNU General Public License for more details.
13--
14
15with HW.GFX.I2C;
16with HW.GFX.EDID;
17with 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
30 function Port_Configured
31 (Configs : Pipe_Configs;
32 Port : Port_Type)
33 return Boolean
34 with
35 Global => null
36 is
37 begin
38 return Configs (Primary).Port = Port or
39 Configs (Secondary).Port = Port or
40 Configs (Tertiary).Port = Port;
41 end Port_Configured;
42
43 -- DP and HDMI share physical pins.
44 function Sibling_Port (Port : Port_Type) return Port_Type
45 is
46 begin
47 return
48 (case Port is
49 when HDMI1 => DP1,
50 when HDMI2 => DP2,
51 when HDMI3 => DP3,
52 when DP1 => HDMI1,
53 when DP2 => HDMI2,
54 when DP3 => HDMI3,
55 when others => Disabled);
56 end Sibling_Port;
57
58 function Has_Sibling_Port (Port : Port_Type) return Boolean
59 is
60 begin
61 return Sibling_Port (Port) /= Disabled;
62 end Has_Sibling_Port;
63
64 procedure Read_EDID
65 (Raw_EDID : out EDID.Raw_EDID_Data;
66 Port : in Active_Port_Type;
67 Success : out Boolean)
68 with
69 Post => (if Success then EDID.Valid (Raw_EDID))
70 is
71 Raw_EDID_Length : GFX.I2C.Transfer_Length := Raw_EDID'Length;
72 begin
73 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
74
75 for I in 1 .. 2 loop
76 if Config_Helpers.To_Display_Type (Port) = DP then
77 -- May need power to read edid
78 declare
79 Temp_Configs : Pipe_Configs := Cur_Configs;
80 begin
81 Temp_Configs (Primary).Port := Port;
82 Power_And_Clocks.Power_Up (Cur_Configs, Temp_Configs);
83 end;
84
85 declare
86 DP_Port : constant GMA.DP_Port :=
87 (case Port is
88 when Internal => DP_A,
89 when DP1 => DP_B,
90 when DP2 => DP_C,
91 when DP3 => DP_D,
92 when others => GMA.DP_Port'First);
93 begin
94 DP_Aux_Ch.I2C_Read
95 (Port => DP_Port,
96 Address => 16#50#,
97 Length => Raw_EDID_Length,
98 Data => Raw_EDID,
99 Success => Success);
100 end;
101 else
102 I2C.I2C_Read
103 (Port => (if Port = Analog
104 then Config.Analog_I2C_Port
105 else Config_Helpers.To_PCH_Port (Port)),
106 Address => 16#50#,
107 Length => Raw_EDID_Length,
108 Data => Raw_EDID,
109 Success => Success);
110 end if;
111 exit when not Success; -- don't retry if reading itself failed
112
113 pragma Debug (Debug.Put_Buffer ("EDID", Raw_EDID, Raw_EDID_Length));
114 EDID.Sanitize (Raw_EDID, Success);
115 exit when Success;
116 end loop;
117 end Read_EDID;
118
119 procedure Probe_Port
120 (Pipe_Cfg : in out Pipe_Config;
121 Port : in Active_Port_Type;
122 Success : out Boolean)
123 with Pre => True
124 is
125 Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#);
126 begin
127 Success := Config.Valid_Port (Port);
128
129 if Success then
130 if Port = Internal then
Nico Huber6f9a50d2016-11-21 23:21:14 +0100131 Panel.Wait_On;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100132 end if;
133 Read_EDID (Raw_EDID, Port, Success);
134 end if;
135
136 if Success and then
137 (EDID.Compatible_Display
138 (Raw_EDID, Config_Helpers.To_Display_Type (Port)) and
139 EDID.Has_Preferred_Mode (Raw_EDID))
140 then
141 Pipe_Cfg.Port := Port;
142 Pipe_Cfg.Mode := EDID.Preferred_Mode (Raw_EDID);
143
144 pragma Warnings (GNATprove, Off, "unused assignment to ""Raw_EDID""",
145 Reason => "We just want to check if it's readable.");
146 if Has_Sibling_Port (Port) then
147 -- Probe sibling port too and bail out if something is detected.
148 -- This is a precaution for adapters that expose the pins of a
149 -- port for both HDMI/DVI and DP (like some ThinkPad docks). A
150 -- user might have attached both by accident and there are ru-
151 -- mors of displays that got fried by applying the wrong signal.
152 declare
153 Have_Sibling_EDID : Boolean;
154 begin
155 Read_EDID (Raw_EDID, Sibling_Port (Port), Have_Sibling_EDID);
156 if Have_Sibling_EDID then
157 Pipe_Cfg.Port := Disabled;
158 Success := False;
159 end if;
160 end;
161 end if;
162 pragma Warnings (GNATprove, On, "unused assignment to ""Raw_EDID""");
163 else
164 Success := False;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100165 end if;
166 end Probe_Port;
167
168 procedure Scan_Ports
169 (Configs : out Pipe_Configs;
170 Ports : in Port_List;
171 Max_Pipe : in Pipe_Index := Pipe_Index'Last)
172 is
Nico Huber6f9a50d2016-11-21 23:21:14 +0100173 Probe_Internal : Boolean := False;
174
Nico Huber8c45bcf2016-11-20 17:30:57 +0100175 Port_Idx : Port_List_Range := Port_List_Range'First;
176 Success : Boolean;
177 begin
178 Configs := (Pipe_Index =>
179 (Port => Disabled,
180 Mode => Invalid_Mode,
181 Framebuffer => Default_FB));
182
Nico Huber6f9a50d2016-11-21 23:21:14 +0100183 -- Turn panel on early to probe other ports during the power on delay.
184 for Idx in Port_List_Range loop
185 exit when Ports (Idx) = Disabled;
186 if Ports (Idx) = Internal then
187 Panel.On (Wait => False);
188 Probe_Internal := True;
189 exit;
190 end if;
191 end loop;
192
Nico Huber8c45bcf2016-11-20 17:30:57 +0100193 for Pipe in Pipe_Index range
194 Pipe_Index'First .. Pipe_Index'Min (Max_Pipe, Config.Max_Pipe)
195 loop
196 while Ports (Port_Idx) /= Disabled loop
197 if not Port_Configured (Configs, Ports (Port_Idx)) and
198 (not Has_Sibling_Port (Ports (Port_Idx)) or
199 not Port_Configured (Configs, Sibling_Port (Ports (Port_Idx))))
200 then
201 Probe_Port (Configs (Pipe), Ports (Port_Idx), Success);
202 else
203 Success := False;
204 end if;
205
206 exit when Port_Idx = Port_List_Range'Last;
207 Port_Idx := Port_List_Range'Succ (Port_Idx);
208
209 exit when Success;
210 end loop;
211 end loop;
212
213 -- Restore power settings
214 Power_And_Clocks.Power_Set_To (Cur_Configs);
Nico Huber6f9a50d2016-11-21 23:21:14 +0100215
216 -- Turn panel power off if probing failed.
217 if Probe_Internal and not Port_Configured (Configs, Internal) then
218 Panel.Off;
219 end if;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100220 end Scan_Ports;
221
222end HW.GFX.GMA.Display_Probing;