1 // Copyright 2017 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #pragma once 6 7 #include <fbl/unique_ptr.h> 8 #include <hwreg/bitfields.h> 9 #include <stddef.h> 10 #include <stdint.h> 11 #include <string.h> 12 #include "lib/edid/timings.h" 13 14 namespace edid { 15 16 // The size of an EDID block; 17 static constexpr uint32_t kBlockSize = 128; 18 19 // Definitions for parsing EDID data. 20 21 // EDID 18-byte detailed timing descriptor. 22 // 23 // Many of the parameters in the timing descriptor are split across 24 // multiple fields, so we define various accessors for reading them. 25 // 26 // See "Table 3.21 - Detailed Timing Definition - Part 1" (in Release 27 // A, Revision 2 of the EDID spec, 2006). 28 struct DetailedTimingDescriptor { horizontal_addressableDetailedTimingDescriptor29 uint32_t horizontal_addressable() const { 30 return horizontal_addressable_low | (horizontal_addressable_high() << 8); 31 } horizontal_blankingDetailedTimingDescriptor32 uint32_t horizontal_blanking() const { 33 return horizontal_blanking_low | (horizontal_blanking_high() << 8); 34 } vertical_addressableDetailedTimingDescriptor35 uint32_t vertical_addressable() const { 36 return vertical_addressable_low | (vertical_addressable_high() << 8); 37 } vertical_blankingDetailedTimingDescriptor38 uint32_t vertical_blanking() const { 39 return vertical_blanking_low | (vertical_blanking_high() << 8); 40 } horizontal_front_porchDetailedTimingDescriptor41 uint32_t horizontal_front_porch() const { 42 return horizontal_front_porch_low | (horizontal_front_porch_high() << 8); 43 } horizontal_sync_pulse_widthDetailedTimingDescriptor44 uint32_t horizontal_sync_pulse_width() const { 45 return horizontal_sync_pulse_width_low | (horizontal_sync_pulse_width_high() << 8); 46 } vertical_front_porchDetailedTimingDescriptor47 uint32_t vertical_front_porch() const { 48 return vertical_front_porch_low() | (vertical_front_porch_high() << 4); 49 } vertical_sync_pulse_widthDetailedTimingDescriptor50 uint32_t vertical_sync_pulse_width() const { 51 return vertical_sync_pulse_width_low() | 52 (vertical_sync_pulse_width_high() << 4); 53 } 54 55 // Offset 0 56 uint16_t pixel_clock_10khz; 57 // Offset 2 58 uint8_t horizontal_addressable_low; 59 uint8_t horizontal_blanking_low; 60 uint8_t horizontal_fields1; 61 DEF_SUBFIELD(horizontal_fields1, 7, 4, horizontal_addressable_high); 62 DEF_SUBFIELD(horizontal_fields1, 3, 0, horizontal_blanking_high); 63 // Offset 5 64 uint8_t vertical_addressable_low; 65 uint8_t vertical_blanking_low; 66 uint8_t vertical_fields1; 67 DEF_SUBFIELD(vertical_fields1, 7, 4, vertical_addressable_high); 68 DEF_SUBFIELD(vertical_fields1, 3, 0, vertical_blanking_high); 69 // Offset 8 70 uint8_t horizontal_front_porch_low; 71 uint8_t horizontal_sync_pulse_width_low; 72 // Offset 10 73 uint8_t vertical_fields2; 74 DEF_SUBFIELD(vertical_fields2, 7, 4, vertical_front_porch_low); 75 DEF_SUBFIELD(vertical_fields2, 3, 0, vertical_sync_pulse_width_low); 76 // Offset 11 77 uint8_t combined; 78 DEF_SUBFIELD(combined, 7, 6, horizontal_front_porch_high); 79 DEF_SUBFIELD(combined, 5, 4, horizontal_sync_pulse_width_high); 80 DEF_SUBFIELD(combined, 3, 2, vertical_front_porch_high); 81 DEF_SUBFIELD(combined, 1, 0, vertical_sync_pulse_width_high); 82 uint8_t rest[5]; // Fields that we don't need to read yet. 83 uint8_t features; 84 DEF_SUBBIT(features, 7, interlaced); 85 DEF_SUBFIELD(features, 4, 3, type); 86 DEF_SUBBIT(features, 2, vsync_polarity); 87 DEF_SUBBIT(features, 1, hsync_polarity); 88 }; 89 #define TYPE_ANALOG 0 90 #define TYPE_ANALOG_BIPOLAR 1 91 #define TYPE_DIGITAL_COMPOSITE 2 92 #define TYPE_DIGITAL_SEPARATE 3 93 94 static_assert(sizeof(DetailedTimingDescriptor) == 18, "Size check for EdidTimingDesc"); 95 96 union Descriptor { 97 DetailedTimingDescriptor timing; 98 struct Monitor { 99 uint16_t generic_tag; 100 uint8_t padding; 101 uint8_t type; 102 static constexpr uint8_t kDummyType = 0x10; 103 static constexpr uint8_t kName = 0xfc; 104 static constexpr uint8_t kSerial = 0xff; 105 106 uint8_t padding2; 107 uint8_t data[13]; 108 } monitor; 109 }; 110 static_assert(sizeof(Descriptor) == 18, "bad struct"); 111 112 struct StandardTimingDescriptor { horizontal_resolutionStandardTimingDescriptor113 uint32_t horizontal_resolution() const { return (byte1 + 31) * 8; } vertical_resolutionStandardTimingDescriptor114 uint32_t vertical_resolution(uint8_t edid_version, uint8_t edid_revision) const { 115 if (aspect_ratio() == 0) { 116 if (edid_version < 1 || (edid_version == 1 && edid_revision < 3)) { 117 return horizontal_resolution(); 118 } else { 119 return horizontal_resolution() * 10 / 16; 120 } 121 } else if (aspect_ratio() == 1) { 122 return horizontal_resolution() * 3 / 4; 123 } else if (aspect_ratio() == 2) { 124 return horizontal_resolution() * 4 / 5; 125 } else if (aspect_ratio() == 3) { 126 return horizontal_resolution() * 9 / 16; 127 } else { 128 ZX_DEBUG_ASSERT(false); 129 return 0; 130 } 131 } 132 133 uint8_t byte1; 134 uint8_t byte2; 135 DEF_SUBFIELD(byte2, 7, 6, aspect_ratio); 136 DEF_SUBFIELD(byte2, 5, 0, vertical_freq); 137 }; 138 139 // This covers the "base" EDID data -- the first 128 bytes (block 0). In 140 // many cases, that is all the display provides, but there may be more data 141 // in extension blocks. 142 // 143 // See "Table 3.1 - EDID Structure Version 1, Revision 4" (in Release 144 // A, Revision 2 of the EDID spec, 2006). 145 struct BaseEdid { 146 bool validate() const; 147 // Not actually a tag, but the first byte will always be this 148 static constexpr uint8_t kTag = 0x00; 149 150 // Offset 0 151 uint8_t header[8]; 152 uint8_t manufacturer_id1; 153 uint8_t manufacturer_id2; 154 uint16_t product_code; 155 uint32_t serial_number; 156 uint8_t unused1[2]; 157 uint8_t edid_version; 158 uint8_t edid_revision; 159 uint8_t video_input_definition; 160 DEF_SUBBIT(video_input_definition, 7, digital); 161 uint8_t horizontal_size_cm; 162 uint8_t vertical_size_cm; 163 uint8_t features_bitmap; 164 DEF_SUBBIT(features_bitmap, 2, standard_srgb); 165 166 uint8_t various[14]; // Fields that we don't need to read yet. 167 StandardTimingDescriptor standard_timings[8]; 168 Descriptor detailed_descriptors[4]; 169 uint8_t num_extensions; 170 uint8_t checksum_byte; 171 }; 172 173 static_assert(offsetof(BaseEdid, edid_version) == 0x12, "Layout check"); 174 static_assert(offsetof(BaseEdid, standard_timings) == 0x26, "Layout check"); 175 static_assert(offsetof(BaseEdid, detailed_descriptors) == 0x36, "Layout check"); 176 177 // Version 3 of the CEA EDID Timing Extension 178 struct CeaEdidTimingExtension { 179 static constexpr uint8_t kTag = 0x02; 180 bool validate() const; 181 182 uint8_t tag; 183 uint8_t revision_number; 184 uint8_t dtd_start_idx; 185 186 uint8_t combined; 187 DEF_SUBBIT(combined, 7, underscan); 188 DEF_SUBBIT(combined, 6, basic_audio); 189 DEF_SUBBIT(combined, 5, ycbcr_444); 190 DEF_SUBBIT(combined, 4, ycbcr_422); 191 DEF_SUBFIELD(combined, 3, 0, native_format_dtds); 192 193 uint8_t payload[123]; 194 uint8_t checksum_byte; 195 }; 196 197 // Short audio descriptor from CEA EDID timing extension's data block collection. 198 struct ShortAudioDescriptor { 199 static constexpr uint8_t kType = 1; 200 201 uint8_t format_and_channels; 202 DEF_SUBFIELD(format_and_channels, 6, 3, format); 203 static constexpr uint8_t kLPcm = 1; 204 DEF_SUBFIELD(format_and_channels, 2, 0, num_channels_minus_1); 205 uint8_t sampling_frequencies; 206 static constexpr uint8_t kHz192 = (1 << 6); 207 static constexpr uint8_t kHz176 = (1 << 5); 208 static constexpr uint8_t kHz96 = (1 << 4); 209 static constexpr uint8_t kHz88 = (1 << 3); 210 static constexpr uint8_t kHz48 = (1 << 2); 211 static constexpr uint8_t kHz44 = (1 << 1); 212 static constexpr uint8_t kHz32 = (1 << 0); 213 uint8_t bitrate; 214 DEF_SUBBIT(bitrate, 2, lpcm_24); 215 DEF_SUBBIT(bitrate, 1, lpcm_20); 216 DEF_SUBBIT(bitrate, 0, lpcm_16); 217 }; 218 static_assert(sizeof(ShortAudioDescriptor) == 3, "Bad size for ShortAudioDescriptor"); 219 220 // Short video descriptor from CEA EDID timing extension's data block collection. 221 struct ShortVideoDescriptor { 222 static constexpr uint8_t kType = 2; 223 224 uint8_t data; 225 DEF_SUBBIT(data, 7, native); 226 DEF_SUBFIELD(data, 6, 0, standard_mode_idx); 227 }; 228 static_assert(sizeof(ShortVideoDescriptor) == 1, "Bad size for ShortVideoDescriptor"); 229 230 // Vendor specific block from CEA EDID timing extension's data block collection. 231 struct VendorSpecificBlock { 232 static constexpr uint8_t kType = 3; 233 234 uint8_t vendor_number[3]; 235 uint8_t physical_addr_low; 236 uint8_t physical_addr_high; 237 // The payload contains vendor defined data. It is only valid up to the 238 // index specified by the data block's length. 239 uint8_t payload[26]; 240 }; 241 static_assert(sizeof(VendorSpecificBlock) == 31, "Bad size for VendorSpecificBlock"); 242 243 // Short speaker descriptor from CEA EDID timing extension's data block collection. 244 struct ShortSpeakerDescriptor { 245 static constexpr uint8_t kType = 4; 246 247 uint8_t features; 248 DEF_SUBBIT(features, 6, rear_left_right_center); 249 DEF_SUBBIT(features, 5, front_left_right_center); 250 DEF_SUBBIT(features, 4, rear_center); 251 DEF_SUBBIT(features, 3, rear_left_right); 252 DEF_SUBBIT(features, 2, front_center); 253 DEF_SUBBIT(features, 1, lfe); 254 DEF_SUBBIT(features, 0, front_left_right); 255 uint8_t reserved; 256 uint8_t reserved2; 257 }; 258 static_assert(sizeof(ShortSpeakerDescriptor) == 3, "Bad size for ShortSpeakerDescriptor"); 259 260 // Data block from CEA EDID timing extension's data block collection. Although this 261 // struct is 32 bytes long, only the first length+1 bytes are actually valid. 262 struct DataBlock { 263 uint8_t header; 264 DEF_SUBFIELD(header, 7, 5, type); 265 DEF_SUBFIELD(header, 4, 0, length); 266 267 union { 268 ShortAudioDescriptor audio[10]; 269 // Only valid up to the index specified by length; 270 ShortVideoDescriptor video[31]; 271 VendorSpecificBlock vendor; 272 ShortSpeakerDescriptor speaker; 273 } payload; 274 }; 275 static_assert(sizeof(DataBlock) == 32, "Bad size for DataBlock"); 276 277 278 typedef struct ddc_i2c_msg { 279 bool is_read; 280 uint8_t addr; 281 uint8_t* buf; 282 uint8_t length; 283 } ddc_i2c_msg_t; 284 285 typedef bool (*ddc_i2c_transact)(void* ctx, ddc_i2c_msg_t* msgs, uint32_t msg_count); 286 // The I2C address for writing the DDC segment 287 static constexpr uint8_t kDdcSegmentI2cAddress = 0x30; 288 // The I2C address for writing the DDC data offset/reading DDC data 289 static constexpr uint8_t kDdcDataI2cAddress = 0x50; 290 291 class timing_iterator; 292 class audio_data_block_iterator; 293 294 class Edid { 295 public: 296 // Creates an Edid from the EdidDdcSource. Does not retain a reference to the source. 297 bool Init(void* ctx, ddc_i2c_transact edid_source, const char** err_msg); 298 // Creates an Edid from raw bytes. The bytes array must remain valid for the duration 299 // of the Edid object's lifetime. 300 bool Init(const uint8_t* bytes, uint16_t len, const char** err_msg); 301 302 void Print(void (*print_fn)(const char* str)) const; 303 edid_bytes()304 const uint8_t* edid_bytes() const { return bytes_; } edid_length()305 uint16_t edid_length() const { return len_; } 306 product_code()307 uint16_t product_code() const { return base_edid_->product_code; } is_standard_rgb()308 bool is_standard_rgb() const { return base_edid_->standard_srgb(); } 309 bool supports_basic_audio() const; manufacturer_id()310 const char* manufacturer_id() const { return manufacturer_id_; } manufacturer_name()311 const char* manufacturer_name() const { return manufacturer_name_; } monitor_name()312 const char* monitor_name() const { return monitor_name_; } monitor_serial()313 const char* monitor_serial() const { return monitor_serial_; } 314 bool is_hdmi() const; 315 316 private: 317 template<typename T> const T* GetBlock(uint8_t block_num) const; 318 319 class descriptor_iterator { 320 public: descriptor_iterator(const Edid * edid)321 explicit descriptor_iterator(const Edid* edid) : edid_(edid) { ++(*this); } 322 323 descriptor_iterator& operator++(); is_valid()324 bool is_valid() const { return edid_ != nullptr; } 325 block_idx()326 uint8_t block_idx() const { return block_idx_; } 327 const Descriptor* operator->() const { return descriptor_; } get()328 const Descriptor* get() const { return descriptor_; } 329 330 private: 331 // Set to null when the iterator is exhausted. 332 const Edid* edid_; 333 // The block index in which we're looking for descriptors. 334 uint8_t block_idx_ = 0; 335 // The index of the current descriptor in the current block. 336 uint32_t descriptor_idx_ = UINT32_MAX; 337 338 const Descriptor* descriptor_; 339 }; 340 341 class data_block_iterator { 342 public: 343 explicit data_block_iterator(const Edid* edid); 344 345 data_block_iterator& operator++(); is_valid()346 bool is_valid() const { return edid_ != nullptr; } 347 348 // Only valid if |is_valid()| is true cea_revision()349 uint8_t cea_revision() const { return cea_revision_; } 350 351 const DataBlock* operator->() const { return db_; } 352 353 private: 354 // Set to null when the iterator is exhausted. 355 const Edid* edid_; 356 // The block index in which we're looking for descriptors. No dbs in the 1st block. 357 uint8_t block_idx_ = 1; 358 // The index of the current descriptor in the current block. 359 uint32_t db_idx_ = UINT32_MAX; 360 361 const DataBlock* db_; 362 363 uint8_t cea_revision_; 364 }; 365 366 // Edid bytes and length 367 const uint8_t* bytes_; 368 uint16_t len_; 369 370 // Ptr to base edid structure in bytes_ 371 const BaseEdid* base_edid_; 372 373 // Contains the edid bytes if they are owned by this object. |bytes_| should generally 374 // be used, since this will be null if something else owns the edid bytes. 375 fbl::unique_ptr<uint8_t[]> edid_bytes_; 376 377 char manufacturer_id_[sizeof(Descriptor::Monitor::data) + 1]; 378 char monitor_name_[sizeof(Descriptor::Monitor::data) + 1]; 379 char monitor_serial_[sizeof(Descriptor::Monitor::data) + 1]; 380 const char* manufacturer_name_ = nullptr; 381 382 friend timing_iterator; 383 friend audio_data_block_iterator; 384 }; 385 386 // Iterator that returns all of the timing modes of the display. The iterator 387 // **does not** filter out duplicates. 388 class timing_iterator { 389 public: timing_iterator(const Edid * edid)390 explicit timing_iterator(const Edid* edid) 391 : edid_(edid), state_(kDtds), state_index_(0), descriptors_(edid), dbs_(edid) { 392 ++(*this); 393 } 394 395 timing_iterator& operator++(); 396 397 const timing_params& operator*() const { return params_; } 398 const timing_params* operator->() const { return ¶ms_; } 399 is_valid()400 bool is_valid() const { return state_ != kDone; } 401 private: 402 // The order in which timings are returned is: 403 // 1) Detailed timings in order across base EDID and CEA blocks 404 // 2) Short video descriptors in CEA data blocks 405 // 3) Standard timings in base edid 406 // TODO: Standart timings in descriptors 407 // TODO: GTF/CVT timings in descriptors/monitor range limits 408 // TODO: Established timings 409 static constexpr uint8_t kDtds = 0; 410 static constexpr uint8_t kSvds = 1; 411 static constexpr uint8_t kStds = 2; 412 static constexpr uint8_t kDone = 3; 413 414 void Advance(); 415 416 timing_params params_; 417 418 const Edid* edid_; 419 uint8_t state_; 420 uint16_t state_index_; 421 Edid::descriptor_iterator descriptors_; 422 Edid::data_block_iterator dbs_; 423 }; 424 425 class audio_data_block_iterator { 426 public: audio_data_block_iterator(const Edid * edid)427 explicit audio_data_block_iterator(const Edid* edid) 428 : edid_(edid), sad_idx_(UINT8_MAX), dbs_(edid) { 429 ++(*this); 430 } 431 432 audio_data_block_iterator& operator++(); 433 434 const ShortAudioDescriptor& operator*() const { return descriptor_; } 435 const ShortAudioDescriptor* operator->() const { return &descriptor_; } 436 is_valid()437 bool is_valid() const { return edid_ != nullptr; } 438 private: 439 const Edid* edid_; 440 uint8_t sad_idx_; 441 Edid::data_block_iterator dbs_; 442 443 ShortAudioDescriptor descriptor_; 444 }; 445 446 } // namespace edid 447