| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 1 | -- |
| 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 | |
| 15 | with HW.GFX.I2C; |
| 16 | with HW.GFX.EDID; |
| 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; |
| 22 | with HW.GFX.GMA.Power_And_Clocks; |
| 23 | |
| 24 | with HW.Debug; |
| 25 | with GNAT.Source_Info; |
| 26 | |
| 27 | package body HW.GFX.GMA.Display_Probing |
| 28 | is |
| 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 Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 131 | Panel.Wait_On; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 132 | 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 Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 165 | end if; |
| 166 | end Probe_Port; |
| 167 | |
| 168 | procedure Scan_Ports |
| Nico Huber | 4c7356d | 2016-12-16 14:22:32 +0100 | [diff] [blame^] | 169 | (Configs : out Pipe_Configs; |
| 170 | Ports : in Port_List := All_Ports; |
| 171 | Max_Pipe : in Pipe_Index := Pipe_Index'Last; |
| 172 | Keep_Power : in Boolean := False) |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 173 | is |
| Nico Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 174 | Probe_Internal : Boolean := False; |
| 175 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 176 | Port_Idx : Port_List_Range := Port_List_Range'First; |
| 177 | Success : Boolean; |
| 178 | begin |
| 179 | Configs := (Pipe_Index => |
| 180 | (Port => Disabled, |
| 181 | Mode => Invalid_Mode, |
| 182 | Framebuffer => Default_FB)); |
| 183 | |
| Nico Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 184 | -- Turn panel on early to probe other ports during the power on delay. |
| 185 | for Idx in Port_List_Range loop |
| 186 | exit when Ports (Idx) = Disabled; |
| 187 | if Ports (Idx) = Internal then |
| 188 | Panel.On (Wait => False); |
| 189 | Probe_Internal := True; |
| 190 | exit; |
| 191 | end if; |
| 192 | end loop; |
| 193 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 194 | for Pipe in Pipe_Index range |
| 195 | Pipe_Index'First .. Pipe_Index'Min (Max_Pipe, Config.Max_Pipe) |
| 196 | loop |
| 197 | while Ports (Port_Idx) /= Disabled loop |
| 198 | if not Port_Configured (Configs, Ports (Port_Idx)) and |
| 199 | (not Has_Sibling_Port (Ports (Port_Idx)) or |
| 200 | not Port_Configured (Configs, Sibling_Port (Ports (Port_Idx)))) |
| 201 | then |
| 202 | Probe_Port (Configs (Pipe), Ports (Port_Idx), Success); |
| 203 | else |
| 204 | Success := False; |
| 205 | end if; |
| 206 | |
| 207 | exit when Port_Idx = Port_List_Range'Last; |
| 208 | Port_Idx := Port_List_Range'Succ (Port_Idx); |
| 209 | |
| 210 | exit when Success; |
| 211 | end loop; |
| 212 | end loop; |
| 213 | |
| 214 | -- Restore power settings |
| Nico Huber | 4c7356d | 2016-12-16 14:22:32 +0100 | [diff] [blame^] | 215 | if not Keep_Power then |
| 216 | Power_And_Clocks.Power_Set_To (Cur_Configs); |
| 217 | end if; |
| Nico Huber | 6f9a50d | 2016-11-21 23:21:14 +0100 | [diff] [blame] | 218 | |
| 219 | -- Turn panel power off if probing failed. |
| 220 | if Probe_Internal and not Port_Configured (Configs, Internal) then |
| 221 | Panel.Off; |
| 222 | end if; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 223 | end Scan_Ports; |
| 224 | |
| 225 | end HW.GFX.GMA.Display_Probing; |