| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 1 | -- |
| 2 | -- Copyright (C) 2015-2016 secunet Security Networks AG |
| Nico Huber | 4fc6dc2 | 2019-05-10 13:01:29 +0200 | [diff] [blame] | 3 | -- Copyright (C) 2019 Nico Huber <nico.h@gmx.de> |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 4 | -- |
| 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 | |
| 16 | with HW.GFX.I2C; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 17 | with HW.GFX.GMA.Config; |
| 18 | with HW.GFX.GMA.Config_Helpers; |
| 19 | with HW.GFX.GMA.I2C; |
| 20 | with HW.GFX.GMA.DP_Aux_Ch; |
| 21 | with HW.GFX.GMA.Panel; |
| Nico Huber | a825448 | 2024-07-03 12:23:00 +0200 | [diff] [blame^] | 22 | with HW.GFX.GMA.Connectors; |
| Nico Huber | 92de9c4 | 2019-09-29 19:03:58 +0200 | [diff] [blame] | 23 | with HW.GFX.GMA.Port_Detect; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 24 | with HW.GFX.GMA.Power_And_Clocks; |
| 25 | |
| 26 | with HW.Debug; |
| 27 | with GNAT.Source_Info; |
| 28 | |
| 29 | package body HW.GFX.GMA.Display_Probing |
| 30 | is |
| 31 | |
| Nico Huber | 5a3191f | 2018-06-04 14:42:13 +0200 | [diff] [blame] | 32 | function Port_Configured (Configs : Pipe_Configs; Port : Port_Type) |
| 33 | return Boolean is |
| 34 | (Configs (Primary).Port = Port or |
| 35 | Configs (Secondary).Port = Port or |
| 36 | Configs (Tertiary).Port = Port); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 37 | |
| 38 | -- DP and HDMI share physical pins. |
| Nico Huber | 5a3191f | 2018-06-04 14:42:13 +0200 | [diff] [blame] | 39 | function Sibling_Port (Port : Port_Type) return Port_Type is |
| 40 | (case Port is |
| 41 | when HDMI1 => DP1, |
| 42 | when HDMI2 => DP2, |
| 43 | when HDMI3 => DP3, |
| 44 | when DP1 => HDMI1, |
| 45 | when DP2 => HDMI2, |
| 46 | when DP3 => HDMI3, |
| Nico Huber | 8b381fc | 2024-03-26 18:04:19 +0100 | [diff] [blame] | 47 | when HDMI_TC1 => DP_TC1, |
| 48 | when HDMI_TC2 => DP_TC2, |
| 49 | when HDMI_TC3 => DP_TC3, |
| 50 | when HDMI_TC4 => DP_TC4, |
| 51 | when DP_TC1 => HDMI_TC1, |
| 52 | when DP_TC2 => HDMI_TC2, |
| 53 | when DP_TC3 => HDMI_TC3, |
| 54 | when DP_TC4 => HDMI_TC4, |
| Nico Huber | 5a3191f | 2018-06-04 14:42:13 +0200 | [diff] [blame] | 55 | when others => Disabled); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 56 | |
| Nico Huber | 5a3191f | 2018-06-04 14:42:13 +0200 | [diff] [blame] | 57 | function Has_Sibling_Port (Port : Port_Type) return Boolean is |
| 58 | (Sibling_Port (Port) /= Disabled); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 59 | |
| Nico Huber | 5a3191f | 2018-06-04 14:42:13 +0200 | [diff] [blame] | 60 | function Is_DVI_I (Port : Active_Port_Type) return Boolean is |
| 61 | (Config.Have_DVI_I and |
| 62 | (Port = Analog or |
| 63 | Config_Helpers.To_PCH_Port (Port) = Config.Analog_I2C_Port)); |
| Nico Huber | 1bc496f | 2017-06-09 22:23:28 +0200 | [diff] [blame] | 64 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 65 | procedure Read_EDID |
| 66 | (Raw_EDID : out EDID.Raw_EDID_Data; |
| 67 | Port : in Active_Port_Type; |
| 68 | Success : out Boolean) |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 69 | is |
| 70 | Raw_EDID_Length : GFX.I2C.Transfer_Length := Raw_EDID'Length; |
| 71 | begin |
| 72 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 73 | |
| 74 | for I in 1 .. 2 loop |
| 75 | if Config_Helpers.To_Display_Type (Port) = DP then |
| Nico Huber | b0bbdbc | 2019-09-27 22:32:21 +0200 | [diff] [blame] | 76 | -- May need power and CDClk to read EDID |
| Nico Huber | 41e8674 | 2024-07-17 17:10:28 +0200 | [diff] [blame] | 77 | Power_And_Clocks.Power_Up (Port, Success); |
| 78 | exit when not Success; |
| 79 | Power_And_Clocks.Enable_CDClk; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 80 | |
| 81 | declare |
| 82 | DP_Port : constant GMA.DP_Port := |
| Nico Huber | 8b381fc | 2024-03-26 18:04:19 +0100 | [diff] [blame] | 83 | (if Config.Has_Type_C_Ports then |
| 84 | (case Config_Helpers.To_GPU_Port (Pipe_Index'First, Port) is |
| 85 | when DIGI_A => DP_A, |
| 86 | when DIGI_B => DP_B, |
| 87 | when DIGI_C => DP_C, |
| 88 | when DDI_TC1 => DP_D, |
| 89 | when DDI_TC2 => DP_E, |
| 90 | when DDI_TC3 => DP_F, |
| 91 | when DDI_TC4 => DP_G, |
| 92 | when DDI_TC5 => DP_H, |
| 93 | when DDI_TC6 => DP_I, |
| 94 | when others => GMA.DP_Port'First) |
| 95 | else |
| 96 | (case Port is |
| 97 | when eDP => DP_A, |
| 98 | when DP1 => DP_B, |
| 99 | when DP2 => DP_C, |
| 100 | when DP3 => DP_D, |
| 101 | when others => GMA.DP_Port'First)); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 102 | begin |
| 103 | DP_Aux_Ch.I2C_Read |
| 104 | (Port => DP_Port, |
| 105 | Address => 16#50#, |
| 106 | Length => Raw_EDID_Length, |
| 107 | Data => Raw_EDID, |
| 108 | Success => Success); |
| 109 | end; |
| 110 | else |
| 111 | I2C.I2C_Read |
| 112 | (Port => (if Port = Analog |
| 113 | then Config.Analog_I2C_Port |
| 114 | else Config_Helpers.To_PCH_Port (Port)), |
| 115 | Address => 16#50#, |
| 116 | Length => Raw_EDID_Length, |
| 117 | Data => Raw_EDID, |
| 118 | Success => Success); |
| 119 | end if; |
| 120 | exit when not Success; -- don't retry if reading itself failed |
| 121 | |
| 122 | pragma Debug (Debug.Put_Buffer ("EDID", Raw_EDID, Raw_EDID_Length)); |
| 123 | EDID.Sanitize (Raw_EDID, Success); |
| 124 | exit when Success; |
| 125 | end loop; |
| 126 | end Read_EDID; |
| 127 | |
| 128 | procedure Probe_Port |
| 129 | (Pipe_Cfg : in out Pipe_Config; |
| 130 | Port : in Active_Port_Type; |
| 131 | Success : out Boolean) |
| 132 | with Pre => True |
| 133 | is |
| Nico Huber | 41e8674 | 2024-07-17 17:10:28 +0200 | [diff] [blame] | 134 | Raw_EDID : EDID.Raw_EDID_Data with Relaxed_Initialization; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 135 | begin |
| 136 | Success := Config.Valid_Port (Port); |
| 137 | |
| 138 | if Success then |
| Nico Huber | 2bbd6e7 | 2020-01-07 18:22:59 +0100 | [diff] [blame] | 139 | Panel.Wait_On (Config_Helpers.To_Panel (Port)); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 140 | Read_EDID (Raw_EDID, Port, Success); |
| 141 | end if; |
| 142 | |
| 143 | if Success and then |
| Nico Huber | 1bc496f | 2017-06-09 22:23:28 +0200 | [diff] [blame] | 144 | ((not Is_DVI_I (Port) or EDID.Compatible_Display |
| 145 | (Raw_EDID, Config_Helpers.To_Display_Type (Port))) and |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 146 | EDID.Has_Preferred_Mode (Raw_EDID)) |
| 147 | then |
| 148 | Pipe_Cfg.Port := Port; |
| 149 | Pipe_Cfg.Mode := EDID.Preferred_Mode (Raw_EDID); |
| 150 | |
| 151 | pragma Warnings (GNATprove, Off, "unused assignment to ""Raw_EDID""", |
| 152 | Reason => "We just want to check if it's readable."); |
| Nico Huber | 3c1ac18 | 2022-09-04 13:12:57 +0200 | [diff] [blame] | 153 | pragma Warnings (GNATprove, Off, """Raw_EDID"" is set by * but*", |
| 154 | Reason => "We just want to check if it's readable."); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 155 | if Has_Sibling_Port (Port) then |
| 156 | -- Probe sibling port too and bail out if something is detected. |
| 157 | -- This is a precaution for adapters that expose the pins of a |
| 158 | -- port for both HDMI/DVI and DP (like some ThinkPad docks). A |
| 159 | -- user might have attached both by accident and there are ru- |
| 160 | -- mors of displays that got fried by applying the wrong signal. |
| 161 | declare |
| 162 | Have_Sibling_EDID : Boolean; |
| 163 | begin |
| 164 | Read_EDID (Raw_EDID, Sibling_Port (Port), Have_Sibling_EDID); |
| 165 | if Have_Sibling_EDID then |
| 166 | Pipe_Cfg.Port := Disabled; |
| 167 | Success := False; |
| 168 | end if; |
| 169 | end; |
| 170 | end if; |
| Nico Huber | 3c1ac18 | 2022-09-04 13:12:57 +0200 | [diff] [blame] | 171 | pragma Warnings (GNATprove, On, """Raw_EDID"" is set by * but*"); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 172 | pragma Warnings (GNATprove, On, "unused assignment to ""Raw_EDID"""); |
| 173 | else |
| 174 | Success := False; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 175 | end if; |
| 176 | end Probe_Port; |
| 177 | |
| 178 | procedure Scan_Ports |
| Nico Huber | 4c7356d | 2016-12-16 14:22:32 +0100 | [diff] [blame] | 179 | (Configs : out Pipe_Configs; |
| 180 | Ports : in Port_List := All_Ports; |
| 181 | Max_Pipe : in Pipe_Index := Pipe_Index'Last; |
| 182 | Keep_Power : in Boolean := False) |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 183 | is |
| Nico Huber | 2bbd6e7 | 2020-01-07 18:22:59 +0100 | [diff] [blame] | 184 | Probed_Panels : array (Valid_Panels) of Boolean := (others => False); |
| Nico Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 185 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 186 | Port_Idx : Port_List_Range := Port_List_Range'First; |
| 187 | Success : Boolean; |
| 188 | begin |
| 189 | Configs := (Pipe_Index => |
| 190 | (Port => Disabled, |
| 191 | Mode => Invalid_Mode, |
| Nico Huber | a02b2c6 | 2018-01-09 15:58:34 +0100 | [diff] [blame] | 192 | Cursor => Default_Cursor, |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 193 | Framebuffer => Default_FB)); |
| 194 | |
| Nico Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 195 | -- Turn panel on early to probe other ports during the power on delay. |
| 196 | for Idx in Port_List_Range loop |
| 197 | exit when Ports (Idx) = Disabled; |
| Nico Huber | 2bbd6e7 | 2020-01-07 18:22:59 +0100 | [diff] [blame] | 198 | declare |
| 199 | P : constant Panel_Control := Config_Helpers.To_Panel (Ports (Idx)); |
| 200 | begin |
| 201 | if P /= No_Panel then |
| 202 | Panel.On (P, Wait => False); |
| 203 | Probed_Panels (P) := True; |
| 204 | end if; |
| 205 | end; |
| Nico Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 206 | end loop; |
| 207 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 208 | for Pipe in Pipe_Index range |
| 209 | Pipe_Index'First .. Pipe_Index'Min (Max_Pipe, Config.Max_Pipe) |
| 210 | loop |
| Nico Huber | 4fc6dc2 | 2019-05-10 13:01:29 +0200 | [diff] [blame] | 211 | Success := False; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 212 | while Ports (Port_Idx) /= Disabled loop |
| 213 | if not Port_Configured (Configs, Ports (Port_Idx)) and |
| 214 | (not Has_Sibling_Port (Ports (Port_Idx)) or |
| 215 | not Port_Configured (Configs, Sibling_Port (Ports (Port_Idx)))) |
| 216 | then |
| 217 | Probe_Port (Configs (Pipe), Ports (Port_Idx), Success); |
| 218 | else |
| 219 | Success := False; |
| 220 | end if; |
| 221 | |
| 222 | exit when Port_Idx = Port_List_Range'Last; |
| 223 | Port_Idx := Port_List_Range'Succ (Port_Idx); |
| 224 | |
| 225 | exit when Success; |
| 226 | end loop; |
| Nico Huber | 4fc6dc2 | 2019-05-10 13:01:29 +0200 | [diff] [blame] | 227 | exit when not Success; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 228 | end loop; |
| 229 | |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 230 | -- On pre-i965 hardware, LVDS must use Pipe B (Secondary). |
| 231 | -- If the scan assigned LVDS to Primary, swap with Secondary. |
| 232 | if Config.LVDS_Needs_Pipe_B and |
| 233 | Configs (Primary).Port = LVDS |
| 234 | then |
| 235 | declare |
| 236 | Tmp : constant Pipe_Config := Configs (Secondary); |
| 237 | begin |
| 238 | Configs (Secondary) := Configs (Primary); |
| 239 | Configs (Primary) := Tmp; |
| 240 | end; |
| 241 | end if; |
| 242 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 243 | -- Restore power settings |
| Nico Huber | 4c7356d | 2016-12-16 14:22:32 +0100 | [diff] [blame] | 244 | if not Keep_Power then |
| 245 | Power_And_Clocks.Power_Set_To (Cur_Configs); |
| 246 | end if; |
| Nico Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 247 | |
| 248 | -- Turn panel power off if probing failed. |
| Nico Huber | 2bbd6e7 | 2020-01-07 18:22:59 +0100 | [diff] [blame] | 249 | for P in Valid_Panels loop |
| 250 | if Probed_Panels (P) and not |
| 251 | Port_Configured (Configs, Config.Panel_Ports (P)) |
| 252 | then |
| 253 | Panel.Off (P); |
| 254 | end if; |
| 255 | end loop; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 256 | end Scan_Ports; |
| 257 | |
| Nico Huber | 92de9c4 | 2019-09-29 19:03:58 +0200 | [diff] [blame] | 258 | procedure Hotplug_Events (Ports : out Port_List) |
| 259 | is |
| 260 | I : Port_List_Range := Port_List_Range'First; |
| 261 | Detected : Boolean; |
| 262 | begin |
| 263 | Ports := (others => Disabled); |
| 264 | for P in Active_Port_Type loop |
| 265 | Port_Detect.Hotplug_Detect (P, Detected); |
| 266 | if Detected then |
| 267 | Ports (I) := P; |
| 268 | exit when I = Port_List_Range'Last; |
| 269 | I := Port_List_Range'Succ (I); |
| 270 | end if; |
| 271 | end loop; |
| 272 | end Hotplug_Events; |
| 273 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 274 | end HW.GFX.GMA.Display_Probing; |