xref: /linux/drivers/gpu/drm/i915/display/intel_bios.c (revision 0eaca1ed0d2f70e3e573ef103ddbde582b2b3745)
1df0566a6SJani Nikula /*
2df0566a6SJani Nikula  * Copyright © 2006 Intel Corporation
3df0566a6SJani Nikula  *
4df0566a6SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5df0566a6SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6df0566a6SJani Nikula  * to deal in the Software without restriction, including without limitation
7df0566a6SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8df0566a6SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9df0566a6SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10df0566a6SJani Nikula  *
11df0566a6SJani Nikula  * The above copyright notice and this permission notice (including the next
12df0566a6SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13df0566a6SJani Nikula  * Software.
14df0566a6SJani Nikula  *
15df0566a6SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16df0566a6SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17df0566a6SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18df0566a6SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19df0566a6SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20df0566a6SJani Nikula  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21df0566a6SJani Nikula  * SOFTWARE.
22df0566a6SJani Nikula  *
23df0566a6SJani Nikula  * Authors:
24df0566a6SJani Nikula  *    Eric Anholt <eric@anholt.net>
25df0566a6SJani Nikula  *
26df0566a6SJani Nikula  */
27df0566a6SJani Nikula 
28da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h>
292a64b147SThomas Zimmermann #include <drm/display/drm_dsc_helper.h>
30cfc10489SJani Nikula #include <drm/drm_edid.h>
31df0566a6SJani Nikula 
32df0566a6SJani Nikula #include "i915_drv.h"
33ce2fce25SMatt Roper #include "i915_reg.h"
34cfc10489SJani Nikula #include "intel_display.h"
35cfc10489SJani Nikula #include "intel_display_types.h"
36cfc10489SJani Nikula #include "intel_gmbus.h"
37df0566a6SJani Nikula 
38df0566a6SJani Nikula #define _INTEL_BIOS_PRIVATE
39df0566a6SJani Nikula #include "intel_vbt_defs.h"
40df0566a6SJani Nikula 
41df0566a6SJani Nikula /**
42df0566a6SJani Nikula  * DOC: Video BIOS Table (VBT)
43df0566a6SJani Nikula  *
44df0566a6SJani Nikula  * The Video BIOS Table, or VBT, provides platform and board specific
45df0566a6SJani Nikula  * configuration information to the driver that is not discoverable or available
46df0566a6SJani Nikula  * through other means. The configuration is mostly related to display
47df0566a6SJani Nikula  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
48df0566a6SJani Nikula  * the PCI ROM.
49df0566a6SJani Nikula  *
50df0566a6SJani Nikula  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
51df0566a6SJani Nikula  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
52df0566a6SJani Nikula  * contain the actual configuration information. The VBT Header, and thus the
53df0566a6SJani Nikula  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
54df0566a6SJani Nikula  * BDB Header. The data blocks are concatenated after the BDB Header. The data
55df0566a6SJani Nikula  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
56df0566a6SJani Nikula  * data. (Block 53, the MIPI Sequence Block is an exception.)
57df0566a6SJani Nikula  *
58df0566a6SJani Nikula  * The driver parses the VBT during load. The relevant information is stored in
59df0566a6SJani Nikula  * driver private data for ease of use, and the actual VBT is not read after
60df0566a6SJani Nikula  * that.
61df0566a6SJani Nikula  */
62df0566a6SJani Nikula 
630d9ef19bSJani Nikula /* Wrapper for VBT child device config */
643162d057SJani Nikula struct intel_bios_encoder_data {
657371fa34SJani Nikula 	struct drm_i915_private *i915;
667371fa34SJani Nikula 
670d9ef19bSJani Nikula 	struct child_device_config child;
686e0d46e9SJani Nikula 	struct dsc_compression_parameters_entry *dsc;
690d9ef19bSJani Nikula 	struct list_head node;
700d9ef19bSJani Nikula };
710d9ef19bSJani Nikula 
72df0566a6SJani Nikula #define	SLAVE_ADDR1	0x70
73df0566a6SJani Nikula #define	SLAVE_ADDR2	0x72
74df0566a6SJani Nikula 
75df0566a6SJani Nikula /* Get BDB block size given a pointer to Block ID. */
76df0566a6SJani Nikula static u32 _get_blocksize(const u8 *block_base)
77df0566a6SJani Nikula {
78df0566a6SJani Nikula 	/* The MIPI Sequence Block v3+ has a separate size field. */
79df0566a6SJani Nikula 	if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
80df0566a6SJani Nikula 		return *((const u32 *)(block_base + 4));
81df0566a6SJani Nikula 	else
82df0566a6SJani Nikula 		return *((const u16 *)(block_base + 1));
83df0566a6SJani Nikula }
84df0566a6SJani Nikula 
85df0566a6SJani Nikula /* Get BDB block size give a pointer to data after Block ID and Block Size. */
86df0566a6SJani Nikula static u32 get_blocksize(const void *block_data)
87df0566a6SJani Nikula {
88df0566a6SJani Nikula 	return _get_blocksize(block_data - 3);
89df0566a6SJani Nikula }
90df0566a6SJani Nikula 
91df0566a6SJani Nikula static const void *
92e163cfb4SVille Syrjälä find_raw_section(const void *_bdb, enum bdb_block_id section_id)
93df0566a6SJani Nikula {
94df0566a6SJani Nikula 	const struct bdb_header *bdb = _bdb;
95df0566a6SJani Nikula 	const u8 *base = _bdb;
96df0566a6SJani Nikula 	int index = 0;
97df0566a6SJani Nikula 	u32 total, current_size;
98df0566a6SJani Nikula 	enum bdb_block_id current_id;
99df0566a6SJani Nikula 
100df0566a6SJani Nikula 	/* skip to first section */
101df0566a6SJani Nikula 	index += bdb->header_size;
102df0566a6SJani Nikula 	total = bdb->bdb_size;
103df0566a6SJani Nikula 
104df0566a6SJani Nikula 	/* walk the sections looking for section_id */
105df0566a6SJani Nikula 	while (index + 3 < total) {
106df0566a6SJani Nikula 		current_id = *(base + index);
107df0566a6SJani Nikula 		current_size = _get_blocksize(base + index);
108df0566a6SJani Nikula 		index += 3;
109df0566a6SJani Nikula 
110df0566a6SJani Nikula 		if (index + current_size > total)
111df0566a6SJani Nikula 			return NULL;
112df0566a6SJani Nikula 
113df0566a6SJani Nikula 		if (current_id == section_id)
114df0566a6SJani Nikula 			return base + index;
115df0566a6SJani Nikula 
116df0566a6SJani Nikula 		index += current_size;
117df0566a6SJani Nikula 	}
118df0566a6SJani Nikula 
119df0566a6SJani Nikula 	return NULL;
120df0566a6SJani Nikula }
121df0566a6SJani Nikula 
122e163cfb4SVille Syrjälä /*
123e163cfb4SVille Syrjälä  * Offset from the start of BDB to the start of the
124e163cfb4SVille Syrjälä  * block data (just past the block header).
125e163cfb4SVille Syrjälä  */
12639b1bc4bSVille Syrjälä static u32 raw_block_offset(const void *bdb, enum bdb_block_id section_id)
127e163cfb4SVille Syrjälä {
128e163cfb4SVille Syrjälä 	const void *block;
129e163cfb4SVille Syrjälä 
130e163cfb4SVille Syrjälä 	block = find_raw_section(bdb, section_id);
131e163cfb4SVille Syrjälä 	if (!block)
132e163cfb4SVille Syrjälä 		return 0;
133e163cfb4SVille Syrjälä 
134e163cfb4SVille Syrjälä 	return block - bdb;
135e163cfb4SVille Syrjälä }
136e163cfb4SVille Syrjälä 
137e163cfb4SVille Syrjälä struct bdb_block_entry {
138e163cfb4SVille Syrjälä 	struct list_head node;
139e163cfb4SVille Syrjälä 	enum bdb_block_id section_id;
140e163cfb4SVille Syrjälä 	u8 data[];
141e163cfb4SVille Syrjälä };
142e163cfb4SVille Syrjälä 
143e163cfb4SVille Syrjälä static const void *
144e163cfb4SVille Syrjälä find_section(struct drm_i915_private *i915,
145e163cfb4SVille Syrjälä 	     enum bdb_block_id section_id)
146e163cfb4SVille Syrjälä {
147e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry;
148e163cfb4SVille Syrjälä 
149a434689cSJani Nikula 	list_for_each_entry(entry, &i915->display.vbt.bdb_blocks, node) {
150e163cfb4SVille Syrjälä 		if (entry->section_id == section_id)
151e163cfb4SVille Syrjälä 			return entry->data + 3;
152e163cfb4SVille Syrjälä 	}
153e163cfb4SVille Syrjälä 
154e163cfb4SVille Syrjälä 	return NULL;
155e163cfb4SVille Syrjälä }
156e163cfb4SVille Syrjälä 
157e163cfb4SVille Syrjälä static const struct {
158e163cfb4SVille Syrjälä 	enum bdb_block_id section_id;
159e163cfb4SVille Syrjälä 	size_t min_size;
160e163cfb4SVille Syrjälä } bdb_blocks[] = {
161e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERAL_FEATURES,
162e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_general_features), },
163e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERAL_DEFINITIONS,
164e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_general_definitions), },
165e163cfb4SVille Syrjälä 	{ .section_id = BDB_PSR,
166e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_psr), },
167e163cfb4SVille Syrjälä 	{ .section_id = BDB_DRIVER_FEATURES,
168e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_driver_features), },
169e163cfb4SVille Syrjälä 	{ .section_id = BDB_SDVO_LVDS_OPTIONS,
170e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_sdvo_lvds_options), },
171e163cfb4SVille Syrjälä 	{ .section_id = BDB_SDVO_PANEL_DTDS,
172e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_sdvo_panel_dtds), },
173e163cfb4SVille Syrjälä 	{ .section_id = BDB_EDP,
174e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_edp), },
175e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_OPTIONS,
176e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lvds_options), },
177901a0cadSVille Syrjälä 	/*
178901a0cadSVille Syrjälä 	 * BDB_LVDS_LFP_DATA depends on BDB_LVDS_LFP_DATA_PTRS,
179901a0cadSVille Syrjälä 	 * so keep the two ordered.
180901a0cadSVille Syrjälä 	 */
181e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_LFP_DATA_PTRS,
182e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lvds_lfp_data_ptrs), },
183e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_LFP_DATA,
184901a0cadSVille Syrjälä 	  .min_size = 0, /* special case */ },
185e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_BACKLIGHT,
186e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_backlight_data), },
187e163cfb4SVille Syrjälä 	{ .section_id = BDB_LFP_POWER,
188e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_power), },
189e163cfb4SVille Syrjälä 	{ .section_id = BDB_MIPI_CONFIG,
190e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_mipi_config), },
191e163cfb4SVille Syrjälä 	{ .section_id = BDB_MIPI_SEQUENCE,
192e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_mipi_sequence) },
193e163cfb4SVille Syrjälä 	{ .section_id = BDB_COMPRESSION_PARAMETERS,
194e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_compression_parameters), },
195e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERIC_DTD,
196e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_generic_dtd), },
197e163cfb4SVille Syrjälä };
198e163cfb4SVille Syrjälä 
199901a0cadSVille Syrjälä static size_t lfp_data_min_size(struct drm_i915_private *i915)
200901a0cadSVille Syrjälä {
201901a0cadSVille Syrjälä 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
202901a0cadSVille Syrjälä 	size_t size;
203901a0cadSVille Syrjälä 
204901a0cadSVille Syrjälä 	ptrs = find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
205901a0cadSVille Syrjälä 	if (!ptrs)
206901a0cadSVille Syrjälä 		return 0;
207901a0cadSVille Syrjälä 
208901a0cadSVille Syrjälä 	size = sizeof(struct bdb_lvds_lfp_data);
209901a0cadSVille Syrjälä 	if (ptrs->panel_name.table_size)
210901a0cadSVille Syrjälä 		size = max(size, ptrs->panel_name.offset +
211901a0cadSVille Syrjälä 			   sizeof(struct bdb_lvds_lfp_data_tail));
212901a0cadSVille Syrjälä 
213901a0cadSVille Syrjälä 	return size;
214901a0cadSVille Syrjälä }
215901a0cadSVille Syrjälä 
216514003e1SVille Syrjälä static bool validate_lfp_data_ptrs(const void *bdb,
217514003e1SVille Syrjälä 				   const struct bdb_lvds_lfp_data_ptrs *ptrs)
218514003e1SVille Syrjälä {
2195ab58d69SVille Syrjälä 	int fp_timing_size, dvo_timing_size, panel_pnp_id_size, panel_name_size;
220514003e1SVille Syrjälä 	int data_block_size, lfp_data_size;
2214e78d602SVille Syrjälä 	const void *data_block;
222514003e1SVille Syrjälä 	int i;
223514003e1SVille Syrjälä 
2244e78d602SVille Syrjälä 	data_block = find_raw_section(bdb, BDB_LVDS_LFP_DATA);
2254e78d602SVille Syrjälä 	if (!data_block)
2264e78d602SVille Syrjälä 		return false;
2274e78d602SVille Syrjälä 
2284e78d602SVille Syrjälä 	data_block_size = get_blocksize(data_block);
229514003e1SVille Syrjälä 	if (data_block_size == 0)
230514003e1SVille Syrjälä 		return false;
231514003e1SVille Syrjälä 
232514003e1SVille Syrjälä 	/* always 3 indicating the presence of fp_timing+dvo_timing+panel_pnp_id */
233514003e1SVille Syrjälä 	if (ptrs->lvds_entries != 3)
234514003e1SVille Syrjälä 		return false;
235514003e1SVille Syrjälä 
236514003e1SVille Syrjälä 	fp_timing_size = ptrs->ptr[0].fp_timing.table_size;
237514003e1SVille Syrjälä 	dvo_timing_size = ptrs->ptr[0].dvo_timing.table_size;
238514003e1SVille Syrjälä 	panel_pnp_id_size = ptrs->ptr[0].panel_pnp_id.table_size;
2395ab58d69SVille Syrjälä 	panel_name_size = ptrs->panel_name.table_size;
240514003e1SVille Syrjälä 
241514003e1SVille Syrjälä 	/* fp_timing has variable size */
242514003e1SVille Syrjälä 	if (fp_timing_size < 32 ||
243514003e1SVille Syrjälä 	    dvo_timing_size != sizeof(struct lvds_dvo_timing) ||
244514003e1SVille Syrjälä 	    panel_pnp_id_size != sizeof(struct lvds_pnp_id))
245514003e1SVille Syrjälä 		return false;
246514003e1SVille Syrjälä 
2475ab58d69SVille Syrjälä 	/* panel_name is not present in old VBTs */
2485ab58d69SVille Syrjälä 	if (panel_name_size != 0 &&
2495ab58d69SVille Syrjälä 	    panel_name_size != sizeof(struct lvds_lfp_panel_name))
2505ab58d69SVille Syrjälä 		return false;
2515ab58d69SVille Syrjälä 
252514003e1SVille Syrjälä 	lfp_data_size = ptrs->ptr[1].fp_timing.offset - ptrs->ptr[0].fp_timing.offset;
253514003e1SVille Syrjälä 	if (16 * lfp_data_size > data_block_size)
254514003e1SVille Syrjälä 		return false;
255514003e1SVille Syrjälä 
256514003e1SVille Syrjälä 	/* make sure the table entries have uniform size */
257514003e1SVille Syrjälä 	for (i = 1; i < 16; i++) {
258514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.table_size != fp_timing_size ||
259514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.table_size != dvo_timing_size ||
260514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.table_size != panel_pnp_id_size)
261514003e1SVille Syrjälä 			return false;
262514003e1SVille Syrjälä 
263514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset - ptrs->ptr[i-1].fp_timing.offset != lfp_data_size ||
264514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset - ptrs->ptr[i-1].dvo_timing.offset != lfp_data_size ||
265514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset - ptrs->ptr[i-1].panel_pnp_id.offset != lfp_data_size)
266514003e1SVille Syrjälä 			return false;
267514003e1SVille Syrjälä 	}
268514003e1SVille Syrjälä 
2694e78d602SVille Syrjälä 	/*
2704e78d602SVille Syrjälä 	 * Except for vlv/chv machines all real VBTs seem to have 6
2714e78d602SVille Syrjälä 	 * unaccounted bytes in the fp_timing table. And it doesn't
2724e78d602SVille Syrjälä 	 * appear to be a really intentional hole as the fp_timing
2734e78d602SVille Syrjälä 	 * 0xffff terminator is always within those 6 missing bytes.
2744e78d602SVille Syrjälä 	 */
2754e78d602SVille Syrjälä 	if (fp_timing_size + 6 + dvo_timing_size + panel_pnp_id_size == lfp_data_size)
2764e78d602SVille Syrjälä 		fp_timing_size += 6;
2774e78d602SVille Syrjälä 
2784e78d602SVille Syrjälä 	if (fp_timing_size + dvo_timing_size + panel_pnp_id_size != lfp_data_size)
2794e78d602SVille Syrjälä 		return false;
2804e78d602SVille Syrjälä 
2814e78d602SVille Syrjälä 	if (ptrs->ptr[0].fp_timing.offset + fp_timing_size != ptrs->ptr[0].dvo_timing.offset ||
2824e78d602SVille Syrjälä 	    ptrs->ptr[0].dvo_timing.offset + dvo_timing_size != ptrs->ptr[0].panel_pnp_id.offset ||
2834e78d602SVille Syrjälä 	    ptrs->ptr[0].panel_pnp_id.offset + panel_pnp_id_size != lfp_data_size)
2844e78d602SVille Syrjälä 		return false;
2854e78d602SVille Syrjälä 
286514003e1SVille Syrjälä 	/* make sure the tables fit inside the data block */
287514003e1SVille Syrjälä 	for (i = 0; i < 16; i++) {
288514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset + fp_timing_size > data_block_size ||
289514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset + dvo_timing_size > data_block_size ||
290514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset + panel_pnp_id_size > data_block_size)
291514003e1SVille Syrjälä 			return false;
292514003e1SVille Syrjälä 	}
293514003e1SVille Syrjälä 
2945ab58d69SVille Syrjälä 	if (ptrs->panel_name.offset + 16 * panel_name_size > data_block_size)
2955ab58d69SVille Syrjälä 		return false;
2965ab58d69SVille Syrjälä 
2974e78d602SVille Syrjälä 	/* make sure fp_timing terminators are present at expected locations */
2984e78d602SVille Syrjälä 	for (i = 0; i < 16; i++) {
2994e78d602SVille Syrjälä 		const u16 *t = data_block + ptrs->ptr[i].fp_timing.offset +
3004e78d602SVille Syrjälä 			fp_timing_size - 2;
3014e78d602SVille Syrjälä 
3024e78d602SVille Syrjälä 		if (*t != 0xffff)
3034e78d602SVille Syrjälä 			return false;
3044e78d602SVille Syrjälä 	}
3054e78d602SVille Syrjälä 
306514003e1SVille Syrjälä 	return true;
307514003e1SVille Syrjälä }
308514003e1SVille Syrjälä 
309918f3025SVille Syrjälä /* make the data table offsets relative to the data block */
310918f3025SVille Syrjälä static bool fixup_lfp_data_ptrs(const void *bdb, void *ptrs_block)
311918f3025SVille Syrjälä {
312918f3025SVille Syrjälä 	struct bdb_lvds_lfp_data_ptrs *ptrs = ptrs_block;
313918f3025SVille Syrjälä 	u32 offset;
314918f3025SVille Syrjälä 	int i;
315918f3025SVille Syrjälä 
31639b1bc4bSVille Syrjälä 	offset = raw_block_offset(bdb, BDB_LVDS_LFP_DATA);
317918f3025SVille Syrjälä 
318918f3025SVille Syrjälä 	for (i = 0; i < 16; i++) {
319918f3025SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset < offset ||
320918f3025SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset < offset ||
321918f3025SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset < offset)
322918f3025SVille Syrjälä 			return false;
323918f3025SVille Syrjälä 
324918f3025SVille Syrjälä 		ptrs->ptr[i].fp_timing.offset -= offset;
325918f3025SVille Syrjälä 		ptrs->ptr[i].dvo_timing.offset -= offset;
326918f3025SVille Syrjälä 		ptrs->ptr[i].panel_pnp_id.offset -= offset;
327918f3025SVille Syrjälä 	}
328918f3025SVille Syrjälä 
3295ab58d69SVille Syrjälä 	if (ptrs->panel_name.table_size) {
3305ab58d69SVille Syrjälä 		if (ptrs->panel_name.offset < offset)
3315ab58d69SVille Syrjälä 			return false;
3325ab58d69SVille Syrjälä 
3335ab58d69SVille Syrjälä 		ptrs->panel_name.offset -= offset;
3345ab58d69SVille Syrjälä 	}
3355ab58d69SVille Syrjälä 
336514003e1SVille Syrjälä 	return validate_lfp_data_ptrs(bdb, ptrs);
337918f3025SVille Syrjälä }
338918f3025SVille Syrjälä 
339a87d0a84SVille Syrjälä static int make_lfp_data_ptr(struct lvds_lfp_data_ptr_table *table,
340a87d0a84SVille Syrjälä 			     int table_size, int total_size)
341a87d0a84SVille Syrjälä {
342a87d0a84SVille Syrjälä 	if (total_size < table_size)
343a87d0a84SVille Syrjälä 		return total_size;
344a87d0a84SVille Syrjälä 
345a87d0a84SVille Syrjälä 	table->table_size = table_size;
346a87d0a84SVille Syrjälä 	table->offset = total_size - table_size;
347a87d0a84SVille Syrjälä 
348a87d0a84SVille Syrjälä 	return total_size - table_size;
349a87d0a84SVille Syrjälä }
350a87d0a84SVille Syrjälä 
351a87d0a84SVille Syrjälä static void next_lfp_data_ptr(struct lvds_lfp_data_ptr_table *next,
352a87d0a84SVille Syrjälä 			      const struct lvds_lfp_data_ptr_table *prev,
353a87d0a84SVille Syrjälä 			      int size)
354a87d0a84SVille Syrjälä {
355a87d0a84SVille Syrjälä 	next->table_size = prev->table_size;
356a87d0a84SVille Syrjälä 	next->offset = prev->offset + size;
357a87d0a84SVille Syrjälä }
358a87d0a84SVille Syrjälä 
359a87d0a84SVille Syrjälä static void *generate_lfp_data_ptrs(struct drm_i915_private *i915,
360a87d0a84SVille Syrjälä 				    const void *bdb)
361a87d0a84SVille Syrjälä {
362d3a70518SVille Syrjälä 	int i, size, table_size, block_size, offset, fp_timing_size;
363a87d0a84SVille Syrjälä 	struct bdb_lvds_lfp_data_ptrs *ptrs;
364d3a70518SVille Syrjälä 	const void *block;
365a87d0a84SVille Syrjälä 	void *ptrs_block;
366a87d0a84SVille Syrjälä 
367d3a70518SVille Syrjälä 	/*
368d3a70518SVille Syrjälä 	 * The hardcoded fp_timing_size is only valid for
369d3a70518SVille Syrjälä 	 * modernish VBTs. All older VBTs definitely should
370d3a70518SVille Syrjälä 	 * include block 41 and thus we don't need to
371d3a70518SVille Syrjälä 	 * generate one.
372d3a70518SVille Syrjälä 	 */
373d3a70518SVille Syrjälä 	if (i915->display.vbt.version < 155)
374d3a70518SVille Syrjälä 		return NULL;
375d3a70518SVille Syrjälä 
376d3a70518SVille Syrjälä 	fp_timing_size = 38;
377d3a70518SVille Syrjälä 
378a87d0a84SVille Syrjälä 	block = find_raw_section(bdb, BDB_LVDS_LFP_DATA);
379a87d0a84SVille Syrjälä 	if (!block)
380a87d0a84SVille Syrjälä 		return NULL;
381a87d0a84SVille Syrjälä 
382a87d0a84SVille Syrjälä 	drm_dbg_kms(&i915->drm, "Generating LFP data table pointers\n");
383a87d0a84SVille Syrjälä 
384a87d0a84SVille Syrjälä 	block_size = get_blocksize(block);
385a87d0a84SVille Syrjälä 
386d3a70518SVille Syrjälä 	size = fp_timing_size + sizeof(struct lvds_dvo_timing) +
387d3a70518SVille Syrjälä 		sizeof(struct lvds_pnp_id);
388a87d0a84SVille Syrjälä 	if (size * 16 > block_size)
389a87d0a84SVille Syrjälä 		return NULL;
390a87d0a84SVille Syrjälä 
391a87d0a84SVille Syrjälä 	ptrs_block = kzalloc(sizeof(*ptrs) + 3, GFP_KERNEL);
392a87d0a84SVille Syrjälä 	if (!ptrs_block)
393a87d0a84SVille Syrjälä 		return NULL;
394a87d0a84SVille Syrjälä 
395a87d0a84SVille Syrjälä 	*(u8 *)(ptrs_block + 0) = BDB_LVDS_LFP_DATA_PTRS;
396a87d0a84SVille Syrjälä 	*(u16 *)(ptrs_block + 1) = sizeof(*ptrs);
397a87d0a84SVille Syrjälä 	ptrs = ptrs_block + 3;
398a87d0a84SVille Syrjälä 
399a87d0a84SVille Syrjälä 	table_size = sizeof(struct lvds_pnp_id);
400a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].panel_pnp_id, table_size, size);
401a87d0a84SVille Syrjälä 
402a87d0a84SVille Syrjälä 	table_size = sizeof(struct lvds_dvo_timing);
403a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].dvo_timing, table_size, size);
404a87d0a84SVille Syrjälä 
405d3a70518SVille Syrjälä 	table_size = fp_timing_size;
406a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].fp_timing, table_size, size);
407a87d0a84SVille Syrjälä 
408a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].fp_timing.table_size)
409a87d0a84SVille Syrjälä 		ptrs->lvds_entries++;
410a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].dvo_timing.table_size)
411a87d0a84SVille Syrjälä 		ptrs->lvds_entries++;
412a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].panel_pnp_id.table_size)
413a87d0a84SVille Syrjälä 		ptrs->lvds_entries++;
414a87d0a84SVille Syrjälä 
415a87d0a84SVille Syrjälä 	if (size != 0 || ptrs->lvds_entries != 3) {
4167674cd0bSXia Fukun 		kfree(ptrs_block);
417a87d0a84SVille Syrjälä 		return NULL;
418a87d0a84SVille Syrjälä 	}
419a87d0a84SVille Syrjälä 
420d3a70518SVille Syrjälä 	size = fp_timing_size + sizeof(struct lvds_dvo_timing) +
421d3a70518SVille Syrjälä 		sizeof(struct lvds_pnp_id);
422a87d0a84SVille Syrjälä 	for (i = 1; i < 16; i++) {
423a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].fp_timing, &ptrs->ptr[i-1].fp_timing, size);
424a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].dvo_timing, &ptrs->ptr[i-1].dvo_timing, size);
425a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].panel_pnp_id, &ptrs->ptr[i-1].panel_pnp_id, size);
426a87d0a84SVille Syrjälä 	}
427a87d0a84SVille Syrjälä 
428a87d0a84SVille Syrjälä 	table_size = sizeof(struct lvds_lfp_panel_name);
429a87d0a84SVille Syrjälä 
430a87d0a84SVille Syrjälä 	if (16 * (size + table_size) <= block_size) {
431a87d0a84SVille Syrjälä 		ptrs->panel_name.table_size = table_size;
432a87d0a84SVille Syrjälä 		ptrs->panel_name.offset = size * 16;
433a87d0a84SVille Syrjälä 	}
434a87d0a84SVille Syrjälä 
435a87d0a84SVille Syrjälä 	offset = block - bdb;
436a87d0a84SVille Syrjälä 
437a87d0a84SVille Syrjälä 	for (i = 0; i < 16; i++) {
438a87d0a84SVille Syrjälä 		ptrs->ptr[i].fp_timing.offset += offset;
439a87d0a84SVille Syrjälä 		ptrs->ptr[i].dvo_timing.offset += offset;
440a87d0a84SVille Syrjälä 		ptrs->ptr[i].panel_pnp_id.offset += offset;
441a87d0a84SVille Syrjälä 	}
442a87d0a84SVille Syrjälä 
443a87d0a84SVille Syrjälä 	if (ptrs->panel_name.table_size)
444a87d0a84SVille Syrjälä 		ptrs->panel_name.offset += offset;
445a87d0a84SVille Syrjälä 
446a87d0a84SVille Syrjälä 	return ptrs_block;
447a87d0a84SVille Syrjälä }
448a87d0a84SVille Syrjälä 
449e163cfb4SVille Syrjälä static void
450e163cfb4SVille Syrjälä init_bdb_block(struct drm_i915_private *i915,
451e163cfb4SVille Syrjälä 	       const void *bdb, enum bdb_block_id section_id,
452e163cfb4SVille Syrjälä 	       size_t min_size)
453e163cfb4SVille Syrjälä {
454e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry;
455a87d0a84SVille Syrjälä 	void *temp_block = NULL;
456e163cfb4SVille Syrjälä 	const void *block;
457e163cfb4SVille Syrjälä 	size_t block_size;
458e163cfb4SVille Syrjälä 
459e163cfb4SVille Syrjälä 	block = find_raw_section(bdb, section_id);
460a87d0a84SVille Syrjälä 
461a87d0a84SVille Syrjälä 	/* Modern VBTs lack the LFP data table pointers block, make one up */
462a87d0a84SVille Syrjälä 	if (!block && section_id == BDB_LVDS_LFP_DATA_PTRS) {
463a87d0a84SVille Syrjälä 		temp_block = generate_lfp_data_ptrs(i915, bdb);
464a87d0a84SVille Syrjälä 		if (temp_block)
465a87d0a84SVille Syrjälä 			block = temp_block + 3;
466a87d0a84SVille Syrjälä 	}
467e163cfb4SVille Syrjälä 	if (!block)
468e163cfb4SVille Syrjälä 		return;
469e163cfb4SVille Syrjälä 
470e163cfb4SVille Syrjälä 	drm_WARN(&i915->drm, min_size == 0,
471e163cfb4SVille Syrjälä 		 "Block %d min_size is zero\n", section_id);
472e163cfb4SVille Syrjälä 
473e163cfb4SVille Syrjälä 	block_size = get_blocksize(block);
474e163cfb4SVille Syrjälä 
475a06289f3SVille Syrjälä 	/*
476a06289f3SVille Syrjälä 	 * Version number and new block size are considered
477a06289f3SVille Syrjälä 	 * part of the header for MIPI sequenece block v3+.
478a06289f3SVille Syrjälä 	 */
479a06289f3SVille Syrjälä 	if (section_id == BDB_MIPI_SEQUENCE && *(const u8 *)block >= 3)
480a06289f3SVille Syrjälä 		block_size += 5;
481a06289f3SVille Syrjälä 
482e163cfb4SVille Syrjälä 	entry = kzalloc(struct_size(entry, data, max(min_size, block_size) + 3),
483e163cfb4SVille Syrjälä 			GFP_KERNEL);
484a87d0a84SVille Syrjälä 	if (!entry) {
485a87d0a84SVille Syrjälä 		kfree(temp_block);
486e163cfb4SVille Syrjälä 		return;
487a87d0a84SVille Syrjälä 	}
488e163cfb4SVille Syrjälä 
489e163cfb4SVille Syrjälä 	entry->section_id = section_id;
490e163cfb4SVille Syrjälä 	memcpy(entry->data, block - 3, block_size + 3);
491e163cfb4SVille Syrjälä 
492a87d0a84SVille Syrjälä 	kfree(temp_block);
493a87d0a84SVille Syrjälä 
494e163cfb4SVille Syrjälä 	drm_dbg_kms(&i915->drm, "Found BDB block %d (size %zu, min size %zu)\n",
495e163cfb4SVille Syrjälä 		    section_id, block_size, min_size);
496e163cfb4SVille Syrjälä 
497918f3025SVille Syrjälä 	if (section_id == BDB_LVDS_LFP_DATA_PTRS &&
498918f3025SVille Syrjälä 	    !fixup_lfp_data_ptrs(bdb, entry->data + 3)) {
499918f3025SVille Syrjälä 		drm_err(&i915->drm, "VBT has malformed LFP data table pointers\n");
500918f3025SVille Syrjälä 		kfree(entry);
501918f3025SVille Syrjälä 		return;
502918f3025SVille Syrjälä 	}
503918f3025SVille Syrjälä 
504a434689cSJani Nikula 	list_add_tail(&entry->node, &i915->display.vbt.bdb_blocks);
505e163cfb4SVille Syrjälä }
506e163cfb4SVille Syrjälä 
507e163cfb4SVille Syrjälä static void init_bdb_blocks(struct drm_i915_private *i915,
508e163cfb4SVille Syrjälä 			    const void *bdb)
509e163cfb4SVille Syrjälä {
510e163cfb4SVille Syrjälä 	int i;
511e163cfb4SVille Syrjälä 
512e163cfb4SVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(bdb_blocks); i++) {
513e163cfb4SVille Syrjälä 		enum bdb_block_id section_id = bdb_blocks[i].section_id;
514e163cfb4SVille Syrjälä 		size_t min_size = bdb_blocks[i].min_size;
515e163cfb4SVille Syrjälä 
516901a0cadSVille Syrjälä 		if (section_id == BDB_LVDS_LFP_DATA)
517901a0cadSVille Syrjälä 			min_size = lfp_data_min_size(i915);
518901a0cadSVille Syrjälä 
519e163cfb4SVille Syrjälä 		init_bdb_block(i915, bdb, section_id, min_size);
520e163cfb4SVille Syrjälä 	}
521e163cfb4SVille Syrjälä }
522e163cfb4SVille Syrjälä 
523df0566a6SJani Nikula static void
524df0566a6SJani Nikula fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
525df0566a6SJani Nikula 			const struct lvds_dvo_timing *dvo_timing)
526df0566a6SJani Nikula {
527df0566a6SJani Nikula 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
528df0566a6SJani Nikula 		dvo_timing->hactive_lo;
529df0566a6SJani Nikula 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
530df0566a6SJani Nikula 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
531df0566a6SJani Nikula 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
532df0566a6SJani Nikula 		((dvo_timing->hsync_pulse_width_hi << 8) |
533df0566a6SJani Nikula 			dvo_timing->hsync_pulse_width_lo);
534df0566a6SJani Nikula 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
535df0566a6SJani Nikula 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
536df0566a6SJani Nikula 
537df0566a6SJani Nikula 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
538df0566a6SJani Nikula 		dvo_timing->vactive_lo;
539df0566a6SJani Nikula 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
540df0566a6SJani Nikula 		((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
541df0566a6SJani Nikula 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
542df0566a6SJani Nikula 		((dvo_timing->vsync_pulse_width_hi << 4) |
543df0566a6SJani Nikula 			dvo_timing->vsync_pulse_width_lo);
544df0566a6SJani Nikula 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
545df0566a6SJani Nikula 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
546df0566a6SJani Nikula 	panel_fixed_mode->clock = dvo_timing->clock * 10;
547df0566a6SJani Nikula 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
548df0566a6SJani Nikula 
549df0566a6SJani Nikula 	if (dvo_timing->hsync_positive)
550df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
551df0566a6SJani Nikula 	else
552df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
553df0566a6SJani Nikula 
554df0566a6SJani Nikula 	if (dvo_timing->vsync_positive)
555df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
556df0566a6SJani Nikula 	else
557df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
558df0566a6SJani Nikula 
559df0566a6SJani Nikula 	panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
560df0566a6SJani Nikula 		dvo_timing->himage_lo;
561df0566a6SJani Nikula 	panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
562df0566a6SJani Nikula 		dvo_timing->vimage_lo;
563df0566a6SJani Nikula 
564df0566a6SJani Nikula 	/* Some VBTs have bogus h/vtotal values */
565df0566a6SJani Nikula 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
566df0566a6SJani Nikula 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
567df0566a6SJani Nikula 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
568df0566a6SJani Nikula 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
569df0566a6SJani Nikula 
570df0566a6SJani Nikula 	drm_mode_set_name(panel_fixed_mode);
571df0566a6SJani Nikula }
572df0566a6SJani Nikula 
573df0566a6SJani Nikula static const struct lvds_dvo_timing *
57458b2e382SVille Syrjälä get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *data,
57558b2e382SVille Syrjälä 		    const struct bdb_lvds_lfp_data_ptrs *ptrs,
576df0566a6SJani Nikula 		    int index)
577df0566a6SJani Nikula {
57858b2e382SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].dvo_timing.offset;
579df0566a6SJani Nikula }
580df0566a6SJani Nikula 
581df0566a6SJani Nikula static const struct lvds_fp_timing *
582918f3025SVille Syrjälä get_lvds_fp_timing(const struct bdb_lvds_lfp_data *data,
583df0566a6SJani Nikula 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
584df0566a6SJani Nikula 		   int index)
585df0566a6SJani Nikula {
58658b2e382SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].fp_timing.offset;
587df0566a6SJani Nikula }
588df0566a6SJani Nikula 
589c518a775SVille Syrjälä static const struct lvds_pnp_id *
590c518a775SVille Syrjälä get_lvds_pnp_id(const struct bdb_lvds_lfp_data *data,
591c518a775SVille Syrjälä 		const struct bdb_lvds_lfp_data_ptrs *ptrs,
592c518a775SVille Syrjälä 		int index)
593c518a775SVille Syrjälä {
594c518a775SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].panel_pnp_id.offset;
595c518a775SVille Syrjälä }
596c518a775SVille Syrjälä 
597901a0cadSVille Syrjälä static const struct bdb_lvds_lfp_data_tail *
598901a0cadSVille Syrjälä get_lfp_data_tail(const struct bdb_lvds_lfp_data *data,
599901a0cadSVille Syrjälä 		  const struct bdb_lvds_lfp_data_ptrs *ptrs)
600901a0cadSVille Syrjälä {
601901a0cadSVille Syrjälä 	if (ptrs->panel_name.table_size)
602901a0cadSVille Syrjälä 		return (const void *)data + ptrs->panel_name.offset;
603901a0cadSVille Syrjälä 	else
604901a0cadSVille Syrjälä 		return NULL;
605901a0cadSVille Syrjälä }
606901a0cadSVille Syrjälä 
60706bfa86eSVille Syrjälä static void dump_pnp_id(struct drm_i915_private *i915,
60806bfa86eSVille Syrjälä 			const struct lvds_pnp_id *pnp_id,
60906bfa86eSVille Syrjälä 			const char *name)
61006bfa86eSVille Syrjälä {
61106bfa86eSVille Syrjälä 	u16 mfg_name = be16_to_cpu((__force __be16)pnp_id->mfg_name);
61206bfa86eSVille Syrjälä 	char vend[4];
61306bfa86eSVille Syrjälä 
61406bfa86eSVille Syrjälä 	drm_dbg_kms(&i915->drm, "%s PNPID mfg: %s (0x%x), prod: %u, serial: %u, week: %d, year: %d\n",
61506bfa86eSVille Syrjälä 		    name, drm_edid_decode_mfg_id(mfg_name, vend),
61606bfa86eSVille Syrjälä 		    pnp_id->mfg_name, pnp_id->product_code, pnp_id->serial,
61706bfa86eSVille Syrjälä 		    pnp_id->mfg_week, pnp_id->mfg_year + 1990);
61806bfa86eSVille Syrjälä }
61906bfa86eSVille Syrjälä 
620c518a775SVille Syrjälä static int opregion_get_panel_type(struct drm_i915_private *i915,
6216434cf63SAnimesh Manna 				   const struct intel_bios_encoder_data *devdata,
622c36225a1SJani Nikula 				   const struct drm_edid *drm_edid, bool use_fallback)
623cc589f2dSVille Syrjälä {
624cc589f2dSVille Syrjälä 	return intel_opregion_get_panel_type(i915);
625cc589f2dSVille Syrjälä }
626cc589f2dSVille Syrjälä 
627c518a775SVille Syrjälä static int vbt_get_panel_type(struct drm_i915_private *i915,
6286434cf63SAnimesh Manna 			      const struct intel_bios_encoder_data *devdata,
629c36225a1SJani Nikula 			      const struct drm_edid *drm_edid, bool use_fallback)
630719f4c51SVille Syrjälä {
631719f4c51SVille Syrjälä 	const struct bdb_lvds_options *lvds_options;
632719f4c51SVille Syrjälä 
633719f4c51SVille Syrjälä 	lvds_options = find_section(i915, BDB_LVDS_OPTIONS);
634719f4c51SVille Syrjälä 	if (!lvds_options)
635719f4c51SVille Syrjälä 		return -1;
636719f4c51SVille Syrjälä 
637c518a775SVille Syrjälä 	if (lvds_options->panel_type > 0xf &&
638c518a775SVille Syrjälä 	    lvds_options->panel_type != 0xff) {
639719f4c51SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Invalid VBT panel type 0x%x\n",
640719f4c51SVille Syrjälä 			    lvds_options->panel_type);
641719f4c51SVille Syrjälä 		return -1;
642719f4c51SVille Syrjälä 	}
643719f4c51SVille Syrjälä 
6446434cf63SAnimesh Manna 	if (devdata && devdata->child.handle == DEVICE_HANDLE_LFP2)
6456434cf63SAnimesh Manna 		return lvds_options->panel_type2;
6466434cf63SAnimesh Manna 
6476434cf63SAnimesh Manna 	drm_WARN_ON(&i915->drm, devdata && devdata->child.handle != DEVICE_HANDLE_LFP1);
6486434cf63SAnimesh Manna 
649719f4c51SVille Syrjälä 	return lvds_options->panel_type;
650719f4c51SVille Syrjälä }
651719f4c51SVille Syrjälä 
652c518a775SVille Syrjälä static int pnpid_get_panel_type(struct drm_i915_private *i915,
6536434cf63SAnimesh Manna 				const struct intel_bios_encoder_data *devdata,
654c36225a1SJani Nikula 				const struct drm_edid *drm_edid, bool use_fallback)
655c518a775SVille Syrjälä {
656c518a775SVille Syrjälä 	const struct bdb_lvds_lfp_data *data;
657c518a775SVille Syrjälä 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
658c518a775SVille Syrjälä 	const struct lvds_pnp_id *edid_id;
659c518a775SVille Syrjälä 	struct lvds_pnp_id edid_id_nodate;
660c36225a1SJani Nikula 	const struct edid *edid = drm_edid_raw(drm_edid); /* FIXME */
661c518a775SVille Syrjälä 	int i, best = -1;
662c518a775SVille Syrjälä 
663c518a775SVille Syrjälä 	if (!edid)
664c518a775SVille Syrjälä 		return -1;
665c518a775SVille Syrjälä 
666c518a775SVille Syrjälä 	edid_id = (const void *)&edid->mfg_id[0];
667c518a775SVille Syrjälä 
668c518a775SVille Syrjälä 	edid_id_nodate = *edid_id;
669c518a775SVille Syrjälä 	edid_id_nodate.mfg_week = 0;
670c518a775SVille Syrjälä 	edid_id_nodate.mfg_year = 0;
671c518a775SVille Syrjälä 
67206bfa86eSVille Syrjälä 	dump_pnp_id(i915, edid_id, "EDID");
67306bfa86eSVille Syrjälä 
674c518a775SVille Syrjälä 	ptrs = find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
675c518a775SVille Syrjälä 	if (!ptrs)
676c518a775SVille Syrjälä 		return -1;
677c518a775SVille Syrjälä 
678c518a775SVille Syrjälä 	data = find_section(i915, BDB_LVDS_LFP_DATA);
679c518a775SVille Syrjälä 	if (!data)
680c518a775SVille Syrjälä 		return -1;
681c518a775SVille Syrjälä 
682c518a775SVille Syrjälä 	for (i = 0; i < 16; i++) {
683c518a775SVille Syrjälä 		const struct lvds_pnp_id *vbt_id =
684c518a775SVille Syrjälä 			get_lvds_pnp_id(data, ptrs, i);
685c518a775SVille Syrjälä 
686c518a775SVille Syrjälä 		/* full match? */
687c518a775SVille Syrjälä 		if (!memcmp(vbt_id, edid_id, sizeof(*vbt_id)))
688c518a775SVille Syrjälä 			return i;
689c518a775SVille Syrjälä 
690c518a775SVille Syrjälä 		/*
691c518a775SVille Syrjälä 		 * Accept a match w/o date if no full match is found,
692c518a775SVille Syrjälä 		 * and the VBT entry does not specify a date.
693c518a775SVille Syrjälä 		 */
694c518a775SVille Syrjälä 		if (best < 0 &&
695c518a775SVille Syrjälä 		    !memcmp(vbt_id, &edid_id_nodate, sizeof(*vbt_id)))
696c518a775SVille Syrjälä 			best = i;
697c518a775SVille Syrjälä 	}
698c518a775SVille Syrjälä 
699c518a775SVille Syrjälä 	return best;
700c518a775SVille Syrjälä }
701c518a775SVille Syrjälä 
702c518a775SVille Syrjälä static int fallback_get_panel_type(struct drm_i915_private *i915,
7036434cf63SAnimesh Manna 				   const struct intel_bios_encoder_data *devdata,
704c36225a1SJani Nikula 				   const struct drm_edid *drm_edid, bool use_fallback)
705cc589f2dSVille Syrjälä {
7063f9ffce5SVille Syrjälä 	return use_fallback ? 0 : -1;
707cc589f2dSVille Syrjälä }
708cc589f2dSVille Syrjälä 
709cc589f2dSVille Syrjälä enum panel_type {
710cc589f2dSVille Syrjälä 	PANEL_TYPE_OPREGION,
711cc589f2dSVille Syrjälä 	PANEL_TYPE_VBT,
712c518a775SVille Syrjälä 	PANEL_TYPE_PNPID,
713cc589f2dSVille Syrjälä 	PANEL_TYPE_FALLBACK,
714cc589f2dSVille Syrjälä };
715cc589f2dSVille Syrjälä 
716c518a775SVille Syrjälä static int get_panel_type(struct drm_i915_private *i915,
7176434cf63SAnimesh Manna 			  const struct intel_bios_encoder_data *devdata,
718c36225a1SJani Nikula 			  const struct drm_edid *drm_edid, bool use_fallback)
719719f4c51SVille Syrjälä {
720cc589f2dSVille Syrjälä 	struct {
721cc589f2dSVille Syrjälä 		const char *name;
722c518a775SVille Syrjälä 		int (*get_panel_type)(struct drm_i915_private *i915,
7236434cf63SAnimesh Manna 				      const struct intel_bios_encoder_data *devdata,
724c36225a1SJani Nikula 				      const struct drm_edid *drm_edid, bool use_fallback);
725cc589f2dSVille Syrjälä 		int panel_type;
726cc589f2dSVille Syrjälä 	} panel_types[] = {
727cc589f2dSVille Syrjälä 		[PANEL_TYPE_OPREGION] = {
728cc589f2dSVille Syrjälä 			.name = "OpRegion",
729cc589f2dSVille Syrjälä 			.get_panel_type = opregion_get_panel_type,
730cc589f2dSVille Syrjälä 		},
731cc589f2dSVille Syrjälä 		[PANEL_TYPE_VBT] = {
732cc589f2dSVille Syrjälä 			.name = "VBT",
733cc589f2dSVille Syrjälä 			.get_panel_type = vbt_get_panel_type,
734cc589f2dSVille Syrjälä 		},
735c518a775SVille Syrjälä 		[PANEL_TYPE_PNPID] = {
736c518a775SVille Syrjälä 			.name = "PNPID",
737c518a775SVille Syrjälä 			.get_panel_type = pnpid_get_panel_type,
738c518a775SVille Syrjälä 		},
739cc589f2dSVille Syrjälä 		[PANEL_TYPE_FALLBACK] = {
740cc589f2dSVille Syrjälä 			.name = "fallback",
741cc589f2dSVille Syrjälä 			.get_panel_type = fallback_get_panel_type,
742cc589f2dSVille Syrjälä 		},
743cc589f2dSVille Syrjälä 	};
744cc589f2dSVille Syrjälä 	int i;
745719f4c51SVille Syrjälä 
746cc589f2dSVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(panel_types); i++) {
7473f9ffce5SVille Syrjälä 		panel_types[i].panel_type = panel_types[i].get_panel_type(i915, devdata,
748c36225a1SJani Nikula 									  drm_edid, use_fallback);
749cc589f2dSVille Syrjälä 
750c518a775SVille Syrjälä 		drm_WARN_ON(&i915->drm, panel_types[i].panel_type > 0xf &&
751c518a775SVille Syrjälä 			    panel_types[i].panel_type != 0xff);
752cc589f2dSVille Syrjälä 
753cc589f2dSVille Syrjälä 		if (panel_types[i].panel_type >= 0)
754cc589f2dSVille Syrjälä 			drm_dbg_kms(&i915->drm, "Panel type (%s): %d\n",
755cc589f2dSVille Syrjälä 				    panel_types[i].name, panel_types[i].panel_type);
756719f4c51SVille Syrjälä 	}
757719f4c51SVille Syrjälä 
758cc589f2dSVille Syrjälä 	if (panel_types[PANEL_TYPE_OPREGION].panel_type >= 0)
759cc589f2dSVille Syrjälä 		i = PANEL_TYPE_OPREGION;
760c518a775SVille Syrjälä 	else if (panel_types[PANEL_TYPE_VBT].panel_type == 0xff &&
761c518a775SVille Syrjälä 		 panel_types[PANEL_TYPE_PNPID].panel_type >= 0)
762c518a775SVille Syrjälä 		i = PANEL_TYPE_PNPID;
763c518a775SVille Syrjälä 	else if (panel_types[PANEL_TYPE_VBT].panel_type != 0xff &&
764c518a775SVille Syrjälä 		 panel_types[PANEL_TYPE_VBT].panel_type >= 0)
765cc589f2dSVille Syrjälä 		i = PANEL_TYPE_VBT;
766cc589f2dSVille Syrjälä 	else
767cc589f2dSVille Syrjälä 		i = PANEL_TYPE_FALLBACK;
768719f4c51SVille Syrjälä 
769cc589f2dSVille Syrjälä 	drm_dbg_kms(&i915->drm, "Selected panel type (%s): %d\n",
770cc589f2dSVille Syrjälä 		    panel_types[i].name, panel_types[i].panel_type);
771cc589f2dSVille Syrjälä 
772cc589f2dSVille Syrjälä 	return panel_types[i].panel_type;
773719f4c51SVille Syrjälä }
774719f4c51SVille Syrjälä 
775a50cc495SVille Syrjälä static unsigned int panel_bits(unsigned int value, int panel_type, int num_bits)
776a50cc495SVille Syrjälä {
777a50cc495SVille Syrjälä 	return (value >> (panel_type * num_bits)) & (BIT(num_bits) - 1);
778a50cc495SVille Syrjälä }
779a50cc495SVille Syrjälä 
780a50cc495SVille Syrjälä static bool panel_bool(unsigned int value, int panel_type)
781a50cc495SVille Syrjälä {
782a50cc495SVille Syrjälä 	return panel_bits(value, panel_type, 1);
783a50cc495SVille Syrjälä }
784a50cc495SVille Syrjälä 
7859e7ecedfSMatt Roper /* Parse general panel options */
786df0566a6SJani Nikula static void
7873cf05076SVille Syrjälä parse_panel_options(struct drm_i915_private *i915,
7880256ea13SVille Syrjälä 		    struct intel_panel *panel)
789df0566a6SJani Nikula {
790df0566a6SJani Nikula 	const struct bdb_lvds_options *lvds_options;
7910256ea13SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
792df0566a6SJani Nikula 	int drrs_mode;
793df0566a6SJani Nikula 
794e163cfb4SVille Syrjälä 	lvds_options = find_section(i915, BDB_LVDS_OPTIONS);
795df0566a6SJani Nikula 	if (!lvds_options)
796df0566a6SJani Nikula 		return;
797df0566a6SJani Nikula 
7983cf05076SVille Syrjälä 	panel->vbt.lvds_dither = lvds_options->pixel_dither;
799df0566a6SJani Nikula 
8005c9016b2SVille Syrjälä 	/*
8015c9016b2SVille Syrjälä 	 * Empirical evidence indicates the block size can be
8025c9016b2SVille Syrjälä 	 * either 4,14,16,24+ bytes. For older VBTs no clear
8035c9016b2SVille Syrjälä 	 * relationship between the block size vs. BDB version.
8045c9016b2SVille Syrjälä 	 */
8055c9016b2SVille Syrjälä 	if (get_blocksize(lvds_options) < 16)
8065c9016b2SVille Syrjälä 		return;
807df0566a6SJani Nikula 
808a50cc495SVille Syrjälä 	drrs_mode = panel_bits(lvds_options->dps_panel_type_bits,
809a50cc495SVille Syrjälä 			       panel_type, 2);
810df0566a6SJani Nikula 	/*
811df0566a6SJani Nikula 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
812df0566a6SJani Nikula 	 * The below piece of code is required to adjust vbt.drrs_type
813df0566a6SJani Nikula 	 * to match the enum drrs_support_type.
814df0566a6SJani Nikula 	 */
815df0566a6SJani Nikula 	switch (drrs_mode) {
816df0566a6SJani Nikula 	case 0:
8173cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_STATIC;
818dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS supported mode is static\n");
819df0566a6SJani Nikula 		break;
820df0566a6SJani Nikula 	case 2:
8213cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_SEAMLESS;
822dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
823e92cbf38SWambui Karuga 			    "DRRS supported mode is seamless\n");
824df0566a6SJani Nikula 		break;
825df0566a6SJani Nikula 	default:
8263cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_NONE;
827dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
828e92cbf38SWambui Karuga 			    "DRRS not supported (VBT input)\n");
829df0566a6SJani Nikula 		break;
830df0566a6SJani Nikula 	}
8319e7ecedfSMatt Roper }
8329e7ecedfSMatt Roper 
8339e7ecedfSMatt Roper static void
83413367132SVille Syrjälä parse_lfp_panel_dtd(struct drm_i915_private *i915,
8353cf05076SVille Syrjälä 		    struct intel_panel *panel,
83613367132SVille Syrjälä 		    const struct bdb_lvds_lfp_data *lvds_lfp_data,
83713367132SVille Syrjälä 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs)
8389e7ecedfSMatt Roper {
8399e7ecedfSMatt Roper 	const struct lvds_dvo_timing *panel_dvo_timing;
8409e7ecedfSMatt Roper 	const struct lvds_fp_timing *fp_timing;
8419e7ecedfSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
8423cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
843df0566a6SJani Nikula 
844df0566a6SJani Nikula 	panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
845df0566a6SJani Nikula 					       lvds_lfp_data_ptrs,
846df0566a6SJani Nikula 					       panel_type);
847df0566a6SJani Nikula 
848df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
849df0566a6SJani Nikula 	if (!panel_fixed_mode)
850df0566a6SJani Nikula 		return;
851df0566a6SJani Nikula 
852df0566a6SJani Nikula 	fill_detail_timing_data(panel_fixed_mode, panel_dvo_timing);
853df0566a6SJani Nikula 
8543cf05076SVille Syrjälä 	panel->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
855df0566a6SJani Nikula 
856dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
857f01bae2dSVille Syrjälä 		    "Found panel mode in BIOS VBT legacy lfp table: " DRM_MODE_FMT "\n",
858f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
859df0566a6SJani Nikula 
860918f3025SVille Syrjälä 	fp_timing = get_lvds_fp_timing(lvds_lfp_data,
861df0566a6SJani Nikula 				       lvds_lfp_data_ptrs,
862df0566a6SJani Nikula 				       panel_type);
86358b2e382SVille Syrjälä 
864df0566a6SJani Nikula 	/* check the resolution, just to be sure */
865df0566a6SJani Nikula 	if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
866df0566a6SJani Nikula 	    fp_timing->y_res == panel_fixed_mode->vdisplay) {
8673cf05076SVille Syrjälä 		panel->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
868dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
869e92cbf38SWambui Karuga 			    "VBT initial LVDS value %x\n",
8703cf05076SVille Syrjälä 			    panel->vbt.bios_lvds_val);
871df0566a6SJani Nikula 	}
872df0566a6SJani Nikula }
873df0566a6SJani Nikula 
874df0566a6SJani Nikula static void
8753cf05076SVille Syrjälä parse_lfp_data(struct drm_i915_private *i915,
8763cf05076SVille Syrjälä 	       struct intel_panel *panel)
87713367132SVille Syrjälä {
87813367132SVille Syrjälä 	const struct bdb_lvds_lfp_data *data;
879901a0cadSVille Syrjälä 	const struct bdb_lvds_lfp_data_tail *tail;
88013367132SVille Syrjälä 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
88106bfa86eSVille Syrjälä 	const struct lvds_pnp_id *pnp_id;
8823cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
88313367132SVille Syrjälä 
88413367132SVille Syrjälä 	ptrs = find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
88513367132SVille Syrjälä 	if (!ptrs)
88613367132SVille Syrjälä 		return;
88713367132SVille Syrjälä 
88813367132SVille Syrjälä 	data = find_section(i915, BDB_LVDS_LFP_DATA);
88913367132SVille Syrjälä 	if (!data)
89013367132SVille Syrjälä 		return;
89113367132SVille Syrjälä 
8923cf05076SVille Syrjälä 	if (!panel->vbt.lfp_lvds_vbt_mode)
8933cf05076SVille Syrjälä 		parse_lfp_panel_dtd(i915, panel, data, ptrs);
894901a0cadSVille Syrjälä 
89506bfa86eSVille Syrjälä 	pnp_id = get_lvds_pnp_id(data, ptrs, panel_type);
89606bfa86eSVille Syrjälä 	dump_pnp_id(i915, pnp_id, "Panel");
89706bfa86eSVille Syrjälä 
898901a0cadSVille Syrjälä 	tail = get_lfp_data_tail(data, ptrs);
899901a0cadSVille Syrjälä 	if (!tail)
900901a0cadSVille Syrjälä 		return;
901901a0cadSVille Syrjälä 
90206bfa86eSVille Syrjälä 	drm_dbg_kms(&i915->drm, "Panel name: %.*s\n",
90306bfa86eSVille Syrjälä 		    (int)sizeof(tail->panel_name[0].name),
90406bfa86eSVille Syrjälä 		    tail->panel_name[panel_type].name);
90506bfa86eSVille Syrjälä 
906a434689cSJani Nikula 	if (i915->display.vbt.version >= 188) {
9073cf05076SVille Syrjälä 		panel->vbt.seamless_drrs_min_refresh_rate =
908790b45f1SVille Syrjälä 			tail->seamless_drrs_min_refresh_rate[panel_type];
909790b45f1SVille Syrjälä 		drm_dbg_kms(&i915->drm,
910790b45f1SVille Syrjälä 			    "Seamless DRRS min refresh rate: %d Hz\n",
9113cf05076SVille Syrjälä 			    panel->vbt.seamless_drrs_min_refresh_rate);
912790b45f1SVille Syrjälä 	}
91313367132SVille Syrjälä }
91413367132SVille Syrjälä 
91513367132SVille Syrjälä static void
9163cf05076SVille Syrjälä parse_generic_dtd(struct drm_i915_private *i915,
9173cf05076SVille Syrjälä 		  struct intel_panel *panel)
91833ef6d4fSMatt Roper {
91933ef6d4fSMatt Roper 	const struct bdb_generic_dtd *generic_dtd;
92033ef6d4fSMatt Roper 	const struct generic_dtd_entry *dtd;
92133ef6d4fSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
92233ef6d4fSMatt Roper 	int num_dtd;
92333ef6d4fSMatt Roper 
92413367132SVille Syrjälä 	/*
92513367132SVille Syrjälä 	 * Older VBTs provided DTD information for internal displays through
92613367132SVille Syrjälä 	 * the "LFP panel tables" block (42).  As of VBT revision 229 the
92713367132SVille Syrjälä 	 * DTD information should be provided via a newer "generic DTD"
92813367132SVille Syrjälä 	 * block (58).  Just to be safe, we'll try the new generic DTD block
92913367132SVille Syrjälä 	 * first on VBT >= 229, but still fall back to trying the old LFP
93013367132SVille Syrjälä 	 * block if that fails.
93113367132SVille Syrjälä 	 */
932a434689cSJani Nikula 	if (i915->display.vbt.version < 229)
93313367132SVille Syrjälä 		return;
93413367132SVille Syrjälä 
935e163cfb4SVille Syrjälä 	generic_dtd = find_section(i915, BDB_GENERIC_DTD);
93633ef6d4fSMatt Roper 	if (!generic_dtd)
93733ef6d4fSMatt Roper 		return;
93833ef6d4fSMatt Roper 
93933ef6d4fSMatt Roper 	if (generic_dtd->gdtd_size < sizeof(struct generic_dtd_entry)) {
940dbd440d8SJani Nikula 		drm_err(&i915->drm, "GDTD size %u is too small.\n",
94133ef6d4fSMatt Roper 			generic_dtd->gdtd_size);
94233ef6d4fSMatt Roper 		return;
94333ef6d4fSMatt Roper 	} else if (generic_dtd->gdtd_size !=
94433ef6d4fSMatt Roper 		   sizeof(struct generic_dtd_entry)) {
945dbd440d8SJani Nikula 		drm_err(&i915->drm, "Unexpected GDTD size %u\n",
946e92cbf38SWambui Karuga 			generic_dtd->gdtd_size);
94733ef6d4fSMatt Roper 		/* DTD has unknown fields, but keep going */
94833ef6d4fSMatt Roper 	}
94933ef6d4fSMatt Roper 
95033ef6d4fSMatt Roper 	num_dtd = (get_blocksize(generic_dtd) -
95133ef6d4fSMatt Roper 		   sizeof(struct bdb_generic_dtd)) / generic_dtd->gdtd_size;
9523cf05076SVille Syrjälä 	if (panel->vbt.panel_type >= num_dtd) {
953dbd440d8SJani Nikula 		drm_err(&i915->drm,
954e92cbf38SWambui Karuga 			"Panel type %d not found in table of %d DTD's\n",
9553cf05076SVille Syrjälä 			panel->vbt.panel_type, num_dtd);
95633ef6d4fSMatt Roper 		return;
95733ef6d4fSMatt Roper 	}
95833ef6d4fSMatt Roper 
9593cf05076SVille Syrjälä 	dtd = &generic_dtd->dtd[panel->vbt.panel_type];
96033ef6d4fSMatt Roper 
96133ef6d4fSMatt Roper 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
96233ef6d4fSMatt Roper 	if (!panel_fixed_mode)
96333ef6d4fSMatt Roper 		return;
96433ef6d4fSMatt Roper 
96533ef6d4fSMatt Roper 	panel_fixed_mode->hdisplay = dtd->hactive;
96633ef6d4fSMatt Roper 	panel_fixed_mode->hsync_start =
96733ef6d4fSMatt Roper 		panel_fixed_mode->hdisplay + dtd->hfront_porch;
96833ef6d4fSMatt Roper 	panel_fixed_mode->hsync_end =
96933ef6d4fSMatt Roper 		panel_fixed_mode->hsync_start + dtd->hsync;
970ad278f35SVandita Kulkarni 	panel_fixed_mode->htotal =
971ad278f35SVandita Kulkarni 		panel_fixed_mode->hdisplay + dtd->hblank;
97233ef6d4fSMatt Roper 
97333ef6d4fSMatt Roper 	panel_fixed_mode->vdisplay = dtd->vactive;
97433ef6d4fSMatt Roper 	panel_fixed_mode->vsync_start =
97533ef6d4fSMatt Roper 		panel_fixed_mode->vdisplay + dtd->vfront_porch;
97633ef6d4fSMatt Roper 	panel_fixed_mode->vsync_end =
97733ef6d4fSMatt Roper 		panel_fixed_mode->vsync_start + dtd->vsync;
978ad278f35SVandita Kulkarni 	panel_fixed_mode->vtotal =
979ad278f35SVandita Kulkarni 		panel_fixed_mode->vdisplay + dtd->vblank;
98033ef6d4fSMatt Roper 
98133ef6d4fSMatt Roper 	panel_fixed_mode->clock = dtd->pixel_clock;
98233ef6d4fSMatt Roper 	panel_fixed_mode->width_mm = dtd->width_mm;
98333ef6d4fSMatt Roper 	panel_fixed_mode->height_mm = dtd->height_mm;
98433ef6d4fSMatt Roper 
98533ef6d4fSMatt Roper 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
98633ef6d4fSMatt Roper 	drm_mode_set_name(panel_fixed_mode);
98733ef6d4fSMatt Roper 
98833ef6d4fSMatt Roper 	if (dtd->hsync_positive_polarity)
98933ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
99033ef6d4fSMatt Roper 	else
99133ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
99233ef6d4fSMatt Roper 
99333ef6d4fSMatt Roper 	if (dtd->vsync_positive_polarity)
99433ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
99533ef6d4fSMatt Roper 	else
99633ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
99733ef6d4fSMatt Roper 
998dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
999f01bae2dSVille Syrjälä 		    "Found panel mode in BIOS VBT generic dtd table: " DRM_MODE_FMT "\n",
1000f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
100133ef6d4fSMatt Roper 
10023cf05076SVille Syrjälä 	panel->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
100333ef6d4fSMatt Roper }
100433ef6d4fSMatt Roper 
100533ef6d4fSMatt Roper static void
10063cf05076SVille Syrjälä parse_lfp_backlight(struct drm_i915_private *i915,
10073cf05076SVille Syrjälä 		    struct intel_panel *panel)
1008df0566a6SJani Nikula {
1009df0566a6SJani Nikula 	const struct bdb_lfp_backlight_data *backlight_data;
1010df0566a6SJani Nikula 	const struct lfp_backlight_data_entry *entry;
10113cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1012d381baadSJosé Roberto de Souza 	u16 level;
1013df0566a6SJani Nikula 
1014e163cfb4SVille Syrjälä 	backlight_data = find_section(i915, BDB_LVDS_BACKLIGHT);
1015df0566a6SJani Nikula 	if (!backlight_data)
1016df0566a6SJani Nikula 		return;
1017df0566a6SJani Nikula 
1018df0566a6SJani Nikula 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
1019dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1020e92cbf38SWambui Karuga 			    "Unsupported backlight data entry size %u\n",
1021df0566a6SJani Nikula 			    backlight_data->entry_size);
1022df0566a6SJani Nikula 		return;
1023df0566a6SJani Nikula 	}
1024df0566a6SJani Nikula 
1025df0566a6SJani Nikula 	entry = &backlight_data->data[panel_type];
1026df0566a6SJani Nikula 
10273cf05076SVille Syrjälä 	panel->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
10283cf05076SVille Syrjälä 	if (!panel->vbt.backlight.present) {
1029dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1030e92cbf38SWambui Karuga 			    "PWM backlight not present in VBT (type %u)\n",
1031df0566a6SJani Nikula 			    entry->type);
1032df0566a6SJani Nikula 		return;
1033df0566a6SJani Nikula 	}
1034df0566a6SJani Nikula 
10353cf05076SVille Syrjälä 	panel->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
1036a0dcb06dSJani Nikula 	panel->vbt.backlight.controller = 0;
1037a434689cSJani Nikula 	if (i915->display.vbt.version >= 191) {
10384378daf5SLukasz Majczak 		size_t exp_size;
10394378daf5SLukasz Majczak 
1040a434689cSJani Nikula 		if (i915->display.vbt.version >= 236)
10414378daf5SLukasz Majczak 			exp_size = sizeof(struct bdb_lfp_backlight_data);
1042a434689cSJani Nikula 		else if (i915->display.vbt.version >= 234)
10434378daf5SLukasz Majczak 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_234;
10444378daf5SLukasz Majczak 		else
10454378daf5SLukasz Majczak 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_191;
10464378daf5SLukasz Majczak 
10474378daf5SLukasz Majczak 		if (get_blocksize(backlight_data) >= exp_size) {
1048df0566a6SJani Nikula 			const struct lfp_backlight_control_method *method;
1049df0566a6SJani Nikula 
1050df0566a6SJani Nikula 			method = &backlight_data->backlight_control[panel_type];
10513cf05076SVille Syrjälä 			panel->vbt.backlight.type = method->type;
10523cf05076SVille Syrjälä 			panel->vbt.backlight.controller = method->controller;
1053df0566a6SJani Nikula 		}
10544378daf5SLukasz Majczak 	}
1055df0566a6SJani Nikula 
10563cf05076SVille Syrjälä 	panel->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
10573cf05076SVille Syrjälä 	panel->vbt.backlight.active_low_pwm = entry->active_low_pwm;
1058d381baadSJosé Roberto de Souza 
1059a434689cSJani Nikula 	if (i915->display.vbt.version >= 234) {
1060d381baadSJosé Roberto de Souza 		u16 min_level;
1061d381baadSJosé Roberto de Souza 		bool scale;
1062d381baadSJosé Roberto de Souza 
1063d381baadSJosé Roberto de Souza 		level = backlight_data->brightness_level[panel_type].level;
1064d381baadSJosé Roberto de Souza 		min_level = backlight_data->brightness_min_level[panel_type].level;
1065d381baadSJosé Roberto de Souza 
1066a434689cSJani Nikula 		if (i915->display.vbt.version >= 236)
1067d381baadSJosé Roberto de Souza 			scale = backlight_data->brightness_precision_bits[panel_type] == 16;
1068d381baadSJosé Roberto de Souza 		else
1069d381baadSJosé Roberto de Souza 			scale = level > 255;
1070d381baadSJosé Roberto de Souza 
1071d381baadSJosé Roberto de Souza 		if (scale)
1072d381baadSJosé Roberto de Souza 			min_level = min_level / 255;
1073d381baadSJosé Roberto de Souza 
1074d381baadSJosé Roberto de Souza 		if (min_level > 255) {
1075dbd440d8SJani Nikula 			drm_warn(&i915->drm, "Brightness min level > 255\n");
1076d381baadSJosé Roberto de Souza 			level = 255;
1077d381baadSJosé Roberto de Souza 		}
10783cf05076SVille Syrjälä 		panel->vbt.backlight.min_brightness = min_level;
107984d3d71fSLee Shawn C 
10803cf05076SVille Syrjälä 		panel->vbt.backlight.brightness_precision_bits =
108184d3d71fSLee Shawn C 			backlight_data->brightness_precision_bits[panel_type];
1082d381baadSJosé Roberto de Souza 	} else {
1083d381baadSJosé Roberto de Souza 		level = backlight_data->level[panel_type];
10843cf05076SVille Syrjälä 		panel->vbt.backlight.min_brightness = entry->min_brightness;
1085d381baadSJosé Roberto de Souza 	}
1086d381baadSJosé Roberto de Souza 
1087fe82b93fSVille Syrjälä 	if (i915->display.vbt.version >= 239)
1088fe82b93fSVille Syrjälä 		panel->vbt.backlight.hdr_dpcd_refresh_timeout =
1089fe82b93fSVille Syrjälä 			DIV_ROUND_UP(backlight_data->hdr_dpcd_refresh_timeout[panel_type], 100);
1090fe82b93fSVille Syrjälä 	else
1091fe82b93fSVille Syrjälä 		panel->vbt.backlight.hdr_dpcd_refresh_timeout = 30;
1092fe82b93fSVille Syrjälä 
1093dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1094e92cbf38SWambui Karuga 		    "VBT backlight PWM modulation frequency %u Hz, "
1095df0566a6SJani Nikula 		    "active %s, min brightness %u, level %u, controller %u\n",
10963cf05076SVille Syrjälä 		    panel->vbt.backlight.pwm_freq_hz,
10973cf05076SVille Syrjälä 		    panel->vbt.backlight.active_low_pwm ? "low" : "high",
10983cf05076SVille Syrjälä 		    panel->vbt.backlight.min_brightness,
1099d381baadSJosé Roberto de Souza 		    level,
11003cf05076SVille Syrjälä 		    panel->vbt.backlight.controller);
1101df0566a6SJani Nikula }
1102df0566a6SJani Nikula 
1103df0566a6SJani Nikula /* Try to find sdvo panel data */
1104df0566a6SJani Nikula static void
11053cf05076SVille Syrjälä parse_sdvo_panel_data(struct drm_i915_private *i915,
11063cf05076SVille Syrjälä 		      struct intel_panel *panel)
1107df0566a6SJani Nikula {
1108df0566a6SJani Nikula 	const struct bdb_sdvo_panel_dtds *dtds;
1109df0566a6SJani Nikula 	struct drm_display_mode *panel_fixed_mode;
1110df0566a6SJani Nikula 	int index;
1111df0566a6SJani Nikula 
1112dbd440d8SJani Nikula 	index = i915->params.vbt_sdvo_panel_type;
1113df0566a6SJani Nikula 	if (index == -2) {
1114dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1115e92cbf38SWambui Karuga 			    "Ignore SDVO panel mode from BIOS VBT tables.\n");
1116df0566a6SJani Nikula 		return;
1117df0566a6SJani Nikula 	}
1118df0566a6SJani Nikula 
1119df0566a6SJani Nikula 	if (index == -1) {
1120df0566a6SJani Nikula 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
1121df0566a6SJani Nikula 
1122e163cfb4SVille Syrjälä 		sdvo_lvds_options = find_section(i915, BDB_SDVO_LVDS_OPTIONS);
1123df0566a6SJani Nikula 		if (!sdvo_lvds_options)
1124df0566a6SJani Nikula 			return;
1125df0566a6SJani Nikula 
1126df0566a6SJani Nikula 		index = sdvo_lvds_options->panel_type;
1127df0566a6SJani Nikula 	}
1128df0566a6SJani Nikula 
1129e163cfb4SVille Syrjälä 	dtds = find_section(i915, BDB_SDVO_PANEL_DTDS);
1130df0566a6SJani Nikula 	if (!dtds)
1131df0566a6SJani Nikula 		return;
1132df0566a6SJani Nikula 
1133df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
1134df0566a6SJani Nikula 	if (!panel_fixed_mode)
1135df0566a6SJani Nikula 		return;
1136df0566a6SJani Nikula 
1137df0566a6SJani Nikula 	fill_detail_timing_data(panel_fixed_mode, &dtds->dtds[index]);
1138df0566a6SJani Nikula 
11393cf05076SVille Syrjälä 	panel->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
1140df0566a6SJani Nikula 
1141dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1142f01bae2dSVille Syrjälä 		    "Found SDVO panel mode in BIOS VBT tables: " DRM_MODE_FMT "\n",
1143f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
1144df0566a6SJani Nikula }
1145df0566a6SJani Nikula 
1146dbd440d8SJani Nikula static int intel_bios_ssc_frequency(struct drm_i915_private *i915,
1147df0566a6SJani Nikula 				    bool alternate)
1148df0566a6SJani Nikula {
1149005e9537SMatt Roper 	switch (DISPLAY_VER(i915)) {
1150df0566a6SJani Nikula 	case 2:
1151df0566a6SJani Nikula 		return alternate ? 66667 : 48000;
1152df0566a6SJani Nikula 	case 3:
1153df0566a6SJani Nikula 	case 4:
1154df0566a6SJani Nikula 		return alternate ? 100000 : 96000;
1155df0566a6SJani Nikula 	default:
1156df0566a6SJani Nikula 		return alternate ? 100000 : 120000;
1157df0566a6SJani Nikula 	}
1158df0566a6SJani Nikula }
1159df0566a6SJani Nikula 
1160df0566a6SJani Nikula static void
1161e163cfb4SVille Syrjälä parse_general_features(struct drm_i915_private *i915)
1162df0566a6SJani Nikula {
1163df0566a6SJani Nikula 	const struct bdb_general_features *general;
1164df0566a6SJani Nikula 
1165e163cfb4SVille Syrjälä 	general = find_section(i915, BDB_GENERAL_FEATURES);
1166df0566a6SJani Nikula 	if (!general)
1167df0566a6SJani Nikula 		return;
1168df0566a6SJani Nikula 
1169a434689cSJani Nikula 	i915->display.vbt.int_tv_support = general->int_tv_support;
1170df0566a6SJani Nikula 	/* int_crt_support can't be trusted on earlier platforms */
1171a434689cSJani Nikula 	if (i915->display.vbt.version >= 155 &&
1172dbd440d8SJani Nikula 	    (HAS_DDI(i915) || IS_VALLEYVIEW(i915)))
1173a434689cSJani Nikula 		i915->display.vbt.int_crt_support = general->int_crt_support;
1174a434689cSJani Nikula 	i915->display.vbt.lvds_use_ssc = general->enable_ssc;
1175a434689cSJani Nikula 	i915->display.vbt.lvds_ssc_freq =
1176dbd440d8SJani Nikula 		intel_bios_ssc_frequency(i915, general->ssc_freq);
1177a434689cSJani Nikula 	i915->display.vbt.display_clock_mode = general->display_clock_mode;
1178a434689cSJani Nikula 	i915->display.vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
1179a434689cSJani Nikula 	if (i915->display.vbt.version >= 181) {
1180a434689cSJani Nikula 		i915->display.vbt.orientation = general->rotate_180 ?
1181df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
1182df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_NORMAL;
1183df0566a6SJani Nikula 	} else {
1184a434689cSJani Nikula 		i915->display.vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1185df0566a6SJani Nikula 	}
1186b70ad01aSJosé Roberto de Souza 
1187a434689cSJani Nikula 	if (i915->display.vbt.version >= 249 && general->afc_startup_config) {
1188a434689cSJani Nikula 		i915->display.vbt.override_afc_startup = true;
1189a434689cSJani Nikula 		i915->display.vbt.override_afc_startup_val = general->afc_startup_config == 0x1 ? 0x0 : 0x7;
1190b70ad01aSJosé Roberto de Souza 	}
1191b70ad01aSJosé Roberto de Souza 
1192dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1193e92cbf38SWambui Karuga 		    "BDB_GENERAL_FEATURES int_tv_support %d int_crt_support %d lvds_use_ssc %d lvds_ssc_freq %d display_clock_mode %d fdi_rx_polarity_inverted %d\n",
1194a434689cSJani Nikula 		    i915->display.vbt.int_tv_support,
1195a434689cSJani Nikula 		    i915->display.vbt.int_crt_support,
1196a434689cSJani Nikula 		    i915->display.vbt.lvds_use_ssc,
1197a434689cSJani Nikula 		    i915->display.vbt.lvds_ssc_freq,
1198a434689cSJani Nikula 		    i915->display.vbt.display_clock_mode,
1199a434689cSJani Nikula 		    i915->display.vbt.fdi_rx_polarity_inverted);
1200df0566a6SJani Nikula }
1201df0566a6SJani Nikula 
1202df0566a6SJani Nikula static const struct child_device_config *
1203df0566a6SJani Nikula child_device_ptr(const struct bdb_general_definitions *defs, int i)
1204df0566a6SJani Nikula {
1205df0566a6SJani Nikula 	return (const void *) &defs->devices[i * defs->child_dev_size];
1206df0566a6SJani Nikula }
1207df0566a6SJani Nikula 
1208df0566a6SJani Nikula static void
1209ef0096e4SJani Nikula parse_sdvo_device_mapping(struct drm_i915_private *i915)
1210df0566a6SJani Nikula {
12113162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
12120d9ef19bSJani Nikula 	int count = 0;
1213df0566a6SJani Nikula 
1214df0566a6SJani Nikula 	/*
1215df0566a6SJani Nikula 	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
1216df0566a6SJani Nikula 	 * accurate and doesn't have to be, as long as it's not too strict.
1217df0566a6SJani Nikula 	 */
121893e7e61eSLucas De Marchi 	if (!IS_DISPLAY_VER(i915, 3, 7)) {
1219dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Skipping SDVO device mapping\n");
1220df0566a6SJani Nikula 		return;
1221df0566a6SJani Nikula 	}
1222df0566a6SJani Nikula 
1223a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
1224d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
1225d24b3475SVille Syrjälä 		struct sdvo_device_mapping *mapping;
1226df0566a6SJani Nikula 
1227df0566a6SJani Nikula 		if (child->slave_addr != SLAVE_ADDR1 &&
1228df0566a6SJani Nikula 		    child->slave_addr != SLAVE_ADDR2) {
1229df0566a6SJani Nikula 			/*
1230df0566a6SJani Nikula 			 * If the slave address is neither 0x70 nor 0x72,
1231df0566a6SJani Nikula 			 * it is not a SDVO device. Skip it.
1232df0566a6SJani Nikula 			 */
1233df0566a6SJani Nikula 			continue;
1234df0566a6SJani Nikula 		}
1235df0566a6SJani Nikula 		if (child->dvo_port != DEVICE_PORT_DVOB &&
1236df0566a6SJani Nikula 		    child->dvo_port != DEVICE_PORT_DVOC) {
1237df0566a6SJani Nikula 			/* skip the incorrect SDVO port */
1238dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1239e92cbf38SWambui Karuga 				    "Incorrect SDVO port. Skip it\n");
1240df0566a6SJani Nikula 			continue;
1241df0566a6SJani Nikula 		}
1242dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1243e92cbf38SWambui Karuga 			    "the SDVO device with slave addr %2x is found on"
1244df0566a6SJani Nikula 			    " %s port\n",
1245df0566a6SJani Nikula 			    child->slave_addr,
1246df0566a6SJani Nikula 			    (child->dvo_port == DEVICE_PORT_DVOB) ?
1247df0566a6SJani Nikula 			    "SDVOB" : "SDVOC");
1248a434689cSJani Nikula 		mapping = &i915->display.vbt.sdvo_mappings[child->dvo_port - 1];
1249df0566a6SJani Nikula 		if (!mapping->initialized) {
1250df0566a6SJani Nikula 			mapping->dvo_port = child->dvo_port;
1251df0566a6SJani Nikula 			mapping->slave_addr = child->slave_addr;
1252df0566a6SJani Nikula 			mapping->dvo_wiring = child->dvo_wiring;
1253df0566a6SJani Nikula 			mapping->ddc_pin = child->ddc_pin;
1254df0566a6SJani Nikula 			mapping->i2c_pin = child->i2c_pin;
1255df0566a6SJani Nikula 			mapping->initialized = 1;
1256dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1257e92cbf38SWambui Karuga 				    "SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
1258e92cbf38SWambui Karuga 				    mapping->dvo_port, mapping->slave_addr,
1259e92cbf38SWambui Karuga 				    mapping->dvo_wiring, mapping->ddc_pin,
1260df0566a6SJani Nikula 				    mapping->i2c_pin);
1261df0566a6SJani Nikula 		} else {
1262dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1263e92cbf38SWambui Karuga 				    "Maybe one SDVO port is shared by "
1264df0566a6SJani Nikula 				    "two SDVO device.\n");
1265df0566a6SJani Nikula 		}
1266df0566a6SJani Nikula 		if (child->slave2_addr) {
1267df0566a6SJani Nikula 			/* Maybe this is a SDVO device with multiple inputs */
1268df0566a6SJani Nikula 			/* And the mapping info is not added */
1269dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1270e92cbf38SWambui Karuga 				    "there exists the slave2_addr. Maybe this"
1271df0566a6SJani Nikula 				    " is a SDVO device with multiple inputs.\n");
1272df0566a6SJani Nikula 		}
1273df0566a6SJani Nikula 		count++;
1274df0566a6SJani Nikula 	}
1275df0566a6SJani Nikula 
1276df0566a6SJani Nikula 	if (!count) {
1277df0566a6SJani Nikula 		/* No SDVO device info is found */
1278dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1279e92cbf38SWambui Karuga 			    "No SDVO device info is found in VBT\n");
1280df0566a6SJani Nikula 	}
1281df0566a6SJani Nikula }
1282df0566a6SJani Nikula 
1283df0566a6SJani Nikula static void
1284e163cfb4SVille Syrjälä parse_driver_features(struct drm_i915_private *i915)
1285df0566a6SJani Nikula {
1286df0566a6SJani Nikula 	const struct bdb_driver_features *driver;
1287df0566a6SJani Nikula 
1288e163cfb4SVille Syrjälä 	driver = find_section(i915, BDB_DRIVER_FEATURES);
1289df0566a6SJani Nikula 	if (!driver)
1290df0566a6SJani Nikula 		return;
1291df0566a6SJani Nikula 
1292005e9537SMatt Roper 	if (DISPLAY_VER(i915) >= 5) {
1293df0566a6SJani Nikula 		/*
1294df0566a6SJani Nikula 		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
1295df0566a6SJani Nikula 		 * to mean "eDP". The VBT spec doesn't agree with that
1296df0566a6SJani Nikula 		 * interpretation, but real world VBTs seem to.
1297df0566a6SJani Nikula 		 */
1298df0566a6SJani Nikula 		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
1299a434689cSJani Nikula 			i915->display.vbt.int_lvds_support = 0;
1300df0566a6SJani Nikula 	} else {
1301df0566a6SJani Nikula 		/*
1302df0566a6SJani Nikula 		 * FIXME it's not clear which BDB version has the LVDS config
1303df0566a6SJani Nikula 		 * bits defined. Revision history in the VBT spec says:
1304df0566a6SJani Nikula 		 * "0.92 | Add two definitions for VBT value of LVDS Active
1305df0566a6SJani Nikula 		 *  Config (00b and 11b values defined) | 06/13/2005"
1306df0566a6SJani Nikula 		 * but does not the specify the BDB version.
1307df0566a6SJani Nikula 		 *
1308df0566a6SJani Nikula 		 * So far version 134 (on i945gm) is the oldest VBT observed
1309df0566a6SJani Nikula 		 * in the wild with the bits correctly populated. Version
1310df0566a6SJani Nikula 		 * 108 (on i85x) does not have the bits correctly populated.
1311df0566a6SJani Nikula 		 */
1312a434689cSJani Nikula 		if (i915->display.vbt.version >= 134 &&
1313df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
1314df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
1315a434689cSJani Nikula 			i915->display.vbt.int_lvds_support = 0;
1316df0566a6SJani Nikula 	}
1317c3fbcf60SVille Syrjälä }
1318c3fbcf60SVille Syrjälä 
1319c3fbcf60SVille Syrjälä static void
13203cf05076SVille Syrjälä parse_panel_driver_features(struct drm_i915_private *i915,
13213cf05076SVille Syrjälä 			    struct intel_panel *panel)
1322c3fbcf60SVille Syrjälä {
1323c3fbcf60SVille Syrjälä 	const struct bdb_driver_features *driver;
1324c3fbcf60SVille Syrjälä 
1325c3fbcf60SVille Syrjälä 	driver = find_section(i915, BDB_DRIVER_FEATURES);
1326c3fbcf60SVille Syrjälä 	if (!driver)
1327c3fbcf60SVille Syrjälä 		return;
1328df0566a6SJani Nikula 
1329a434689cSJani Nikula 	if (i915->display.vbt.version < 228) {
1330dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS State Enabled:%d\n",
1331e92cbf38SWambui Karuga 			    driver->drrs_enabled);
1332df0566a6SJani Nikula 		/*
1333df0566a6SJani Nikula 		 * If DRRS is not supported, drrs_type has to be set to 0.
1334df0566a6SJani Nikula 		 * This is because, VBT is configured in such a way that
1335df0566a6SJani Nikula 		 * static DRRS is 0 and DRRS not supported is represented by
1336df0566a6SJani Nikula 		 * driver->drrs_enabled=false
1337df0566a6SJani Nikula 		 */
13385a18db2eSVille Syrjälä 		if (!driver->drrs_enabled && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
13395a18db2eSVille Syrjälä 			/*
13405a18db2eSVille Syrjälä 			 * FIXME Should DMRRS perhaps be treated as seamless
13415a18db2eSVille Syrjälä 			 * but without the automatic downclocking?
13425a18db2eSVille Syrjälä 			 */
13435a18db2eSVille Syrjälä 			if (driver->dmrrs_enabled)
13445a18db2eSVille Syrjälä 				panel->vbt.drrs_type = DRRS_TYPE_STATIC;
13455a18db2eSVille Syrjälä 			else
13463cf05076SVille Syrjälä 				panel->vbt.drrs_type = DRRS_TYPE_NONE;
13475a18db2eSVille Syrjälä 		}
1348551fb93dSJosé Roberto de Souza 
13493cf05076SVille Syrjälä 		panel->vbt.psr.enable = driver->psr_enabled;
1350df0566a6SJani Nikula 	}
1351551fb93dSJosé Roberto de Souza }
1352551fb93dSJosé Roberto de Souza 
1353551fb93dSJosé Roberto de Souza static void
13543cf05076SVille Syrjälä parse_power_conservation_features(struct drm_i915_private *i915,
13553cf05076SVille Syrjälä 				  struct intel_panel *panel)
1356551fb93dSJosé Roberto de Souza {
1357551fb93dSJosé Roberto de Souza 	const struct bdb_lfp_power *power;
13583cf05076SVille Syrjälä 	u8 panel_type = panel->vbt.panel_type;
1359551fb93dSJosé Roberto de Souza 
1360fba99b1aSVille Syrjälä 	panel->vbt.vrr = true; /* matches Windows behaviour */
1361551fb93dSJosé Roberto de Souza 
1362a434689cSJani Nikula 	if (i915->display.vbt.version < 228)
1363551fb93dSJosé Roberto de Souza 		return;
1364551fb93dSJosé Roberto de Souza 
1365e163cfb4SVille Syrjälä 	power = find_section(i915, BDB_LFP_POWER);
1366551fb93dSJosé Roberto de Souza 	if (!power)
1367551fb93dSJosé Roberto de Souza 		return;
1368551fb93dSJosé Roberto de Souza 
1369a50cc495SVille Syrjälä 	panel->vbt.psr.enable = panel_bool(power->psr, panel_type);
1370551fb93dSJosé Roberto de Souza 
1371551fb93dSJosé Roberto de Souza 	/*
1372551fb93dSJosé Roberto de Souza 	 * If DRRS is not supported, drrs_type has to be set to 0.
1373551fb93dSJosé Roberto de Souza 	 * This is because, VBT is configured in such a way that
1374551fb93dSJosé Roberto de Souza 	 * static DRRS is 0 and DRRS not supported is represented by
1375551fb93dSJosé Roberto de Souza 	 * power->drrs & BIT(panel_type)=false
1376551fb93dSJosé Roberto de Souza 	 */
1377a50cc495SVille Syrjälä 	if (!panel_bool(power->drrs, panel_type) && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
13785a18db2eSVille Syrjälä 		/*
13795a18db2eSVille Syrjälä 		 * FIXME Should DMRRS perhaps be treated as seamless
13805a18db2eSVille Syrjälä 		 * but without the automatic downclocking?
13815a18db2eSVille Syrjälä 		 */
1382a50cc495SVille Syrjälä 		if (panel_bool(power->dmrrs, panel_type))
13835a18db2eSVille Syrjälä 			panel->vbt.drrs_type = DRRS_TYPE_STATIC;
13845a18db2eSVille Syrjälä 		else
13853cf05076SVille Syrjälä 			panel->vbt.drrs_type = DRRS_TYPE_NONE;
13865a18db2eSVille Syrjälä 	}
1387f615cb6aSJosé Roberto de Souza 
1388a434689cSJani Nikula 	if (i915->display.vbt.version >= 232)
1389a50cc495SVille Syrjälä 		panel->vbt.edp.hobl = panel_bool(power->hobl, panel_type);
1390fba99b1aSVille Syrjälä 
1391a434689cSJani Nikula 	if (i915->display.vbt.version >= 233)
1392a50cc495SVille Syrjälä 		panel->vbt.vrr = panel_bool(power->vrr_feature_enabled,
1393a50cc495SVille Syrjälä 					    panel_type);
1394551fb93dSJosé Roberto de Souza }
1395df0566a6SJani Nikula 
1396df0566a6SJani Nikula static void
13973cf05076SVille Syrjälä parse_edp(struct drm_i915_private *i915,
13983cf05076SVille Syrjälä 	  struct intel_panel *panel)
1399df0566a6SJani Nikula {
1400df0566a6SJani Nikula 	const struct bdb_edp *edp;
1401df0566a6SJani Nikula 	const struct edp_power_seq *edp_pps;
1402df0566a6SJani Nikula 	const struct edp_fast_link_params *edp_link_params;
14033cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1404df0566a6SJani Nikula 
1405e163cfb4SVille Syrjälä 	edp = find_section(i915, BDB_EDP);
1406df0566a6SJani Nikula 	if (!edp)
1407df0566a6SJani Nikula 		return;
1408df0566a6SJani Nikula 
1409a50cc495SVille Syrjälä 	switch (panel_bits(edp->color_depth, panel_type, 2)) {
1410df0566a6SJani Nikula 	case EDP_18BPP:
14113cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 18;
1412df0566a6SJani Nikula 		break;
1413df0566a6SJani Nikula 	case EDP_24BPP:
14143cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 24;
1415df0566a6SJani Nikula 		break;
1416df0566a6SJani Nikula 	case EDP_30BPP:
14173cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 30;
1418df0566a6SJani Nikula 		break;
1419df0566a6SJani Nikula 	}
1420df0566a6SJani Nikula 
1421df0566a6SJani Nikula 	/* Get the eDP sequencing and link info */
1422df0566a6SJani Nikula 	edp_pps = &edp->power_seqs[panel_type];
1423df0566a6SJani Nikula 	edp_link_params = &edp->fast_link_params[panel_type];
1424df0566a6SJani Nikula 
14253cf05076SVille Syrjälä 	panel->vbt.edp.pps = *edp_pps;
1426df0566a6SJani Nikula 
1427a434689cSJani Nikula 	if (i915->display.vbt.version >= 224) {
1428f06d1d66SVille Syrjälä 		panel->vbt.edp.rate =
1429f06d1d66SVille Syrjälä 			edp->edp_fast_link_training_rate[panel_type] * 20;
1430f06d1d66SVille Syrjälä 	} else {
1431df0566a6SJani Nikula 		switch (edp_link_params->rate) {
1432df0566a6SJani Nikula 		case EDP_RATE_1_62:
1433f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 162000;
1434df0566a6SJani Nikula 			break;
1435df0566a6SJani Nikula 		case EDP_RATE_2_7:
1436f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 270000;
1437f06d1d66SVille Syrjälä 			break;
1438f06d1d66SVille Syrjälä 		case EDP_RATE_5_4:
1439f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 540000;
1440df0566a6SJani Nikula 			break;
1441df0566a6SJani Nikula 		default:
1442dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1443e92cbf38SWambui Karuga 				    "VBT has unknown eDP link rate value %u\n",
1444df0566a6SJani Nikula 				    edp_link_params->rate);
1445df0566a6SJani Nikula 			break;
1446df0566a6SJani Nikula 		}
1447f06d1d66SVille Syrjälä 	}
1448df0566a6SJani Nikula 
1449df0566a6SJani Nikula 	switch (edp_link_params->lanes) {
1450df0566a6SJani Nikula 	case EDP_LANE_1:
14513cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 1;
1452df0566a6SJani Nikula 		break;
1453df0566a6SJani Nikula 	case EDP_LANE_2:
14543cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 2;
1455df0566a6SJani Nikula 		break;
1456df0566a6SJani Nikula 	case EDP_LANE_4:
14573cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 4;
1458df0566a6SJani Nikula 		break;
1459df0566a6SJani Nikula 	default:
1460dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1461e92cbf38SWambui Karuga 			    "VBT has unknown eDP lane count value %u\n",
1462df0566a6SJani Nikula 			    edp_link_params->lanes);
1463df0566a6SJani Nikula 		break;
1464df0566a6SJani Nikula 	}
1465df0566a6SJani Nikula 
1466df0566a6SJani Nikula 	switch (edp_link_params->preemphasis) {
1467df0566a6SJani Nikula 	case EDP_PREEMPHASIS_NONE:
14683cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
1469df0566a6SJani Nikula 		break;
1470df0566a6SJani Nikula 	case EDP_PREEMPHASIS_3_5dB:
14713cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
1472df0566a6SJani Nikula 		break;
1473df0566a6SJani Nikula 	case EDP_PREEMPHASIS_6dB:
14743cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
1475df0566a6SJani Nikula 		break;
1476df0566a6SJani Nikula 	case EDP_PREEMPHASIS_9_5dB:
14773cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
1478df0566a6SJani Nikula 		break;
1479df0566a6SJani Nikula 	default:
1480dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1481e92cbf38SWambui Karuga 			    "VBT has unknown eDP pre-emphasis value %u\n",
1482df0566a6SJani Nikula 			    edp_link_params->preemphasis);
1483df0566a6SJani Nikula 		break;
1484df0566a6SJani Nikula 	}
1485df0566a6SJani Nikula 
1486df0566a6SJani Nikula 	switch (edp_link_params->vswing) {
1487df0566a6SJani Nikula 	case EDP_VSWING_0_4V:
14883cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
1489df0566a6SJani Nikula 		break;
1490df0566a6SJani Nikula 	case EDP_VSWING_0_6V:
14913cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
1492df0566a6SJani Nikula 		break;
1493df0566a6SJani Nikula 	case EDP_VSWING_0_8V:
14943cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
1495df0566a6SJani Nikula 		break;
1496df0566a6SJani Nikula 	case EDP_VSWING_1_2V:
14973cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
1498df0566a6SJani Nikula 		break;
1499df0566a6SJani Nikula 	default:
1500dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1501e92cbf38SWambui Karuga 			    "VBT has unknown eDP voltage swing value %u\n",
1502df0566a6SJani Nikula 			    edp_link_params->vswing);
1503df0566a6SJani Nikula 		break;
1504df0566a6SJani Nikula 	}
1505df0566a6SJani Nikula 
1506a434689cSJani Nikula 	if (i915->display.vbt.version >= 173) {
1507df0566a6SJani Nikula 		u8 vswing;
1508df0566a6SJani Nikula 
1509df0566a6SJani Nikula 		/* Don't read from VBT if module parameter has valid value*/
1510dbd440d8SJani Nikula 		if (i915->params.edp_vswing) {
15113cf05076SVille Syrjälä 			panel->vbt.edp.low_vswing =
1512dbd440d8SJani Nikula 				i915->params.edp_vswing == 1;
1513df0566a6SJani Nikula 		} else {
1514df0566a6SJani Nikula 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
15153cf05076SVille Syrjälä 			panel->vbt.edp.low_vswing = vswing == 0;
1516df0566a6SJani Nikula 		}
1517df0566a6SJani Nikula 	}
1518b395c29aSVille Syrjälä 
15193cf05076SVille Syrjälä 	panel->vbt.edp.drrs_msa_timing_delay =
1520a50cc495SVille Syrjälä 		panel_bits(edp->sdrrs_msa_timing_delay, panel_type, 2);
152124b8b74eSVille Syrjälä 
1522a434689cSJani Nikula 	if (i915->display.vbt.version >= 244)
152324b8b74eSVille Syrjälä 		panel->vbt.edp.max_link_rate =
152424b8b74eSVille Syrjälä 			edp->edp_max_port_link_rate[panel_type] * 20;
1525df0566a6SJani Nikula }
1526df0566a6SJani Nikula 
1527df0566a6SJani Nikula static void
15283cf05076SVille Syrjälä parse_psr(struct drm_i915_private *i915,
15293cf05076SVille Syrjälä 	  struct intel_panel *panel)
1530df0566a6SJani Nikula {
1531df0566a6SJani Nikula 	const struct bdb_psr *psr;
1532df0566a6SJani Nikula 	const struct psr_table *psr_table;
15333cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1534df0566a6SJani Nikula 
1535e163cfb4SVille Syrjälä 	psr = find_section(i915, BDB_PSR);
1536df0566a6SJani Nikula 	if (!psr) {
1537dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No PSR BDB found.\n");
1538df0566a6SJani Nikula 		return;
1539df0566a6SJani Nikula 	}
1540df0566a6SJani Nikula 
1541df0566a6SJani Nikula 	psr_table = &psr->psr_table[panel_type];
1542df0566a6SJani Nikula 
15433cf05076SVille Syrjälä 	panel->vbt.psr.full_link = psr_table->full_link;
15443cf05076SVille Syrjälä 	panel->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
1545df0566a6SJani Nikula 
1546df0566a6SJani Nikula 	/* Allowed VBT values goes from 0 to 15 */
15473cf05076SVille Syrjälä 	panel->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
1548df0566a6SJani Nikula 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
1549df0566a6SJani Nikula 
1550df0566a6SJani Nikula 	/*
1551df0566a6SJani Nikula 	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
1552df0566a6SJani Nikula 	 * Old decimal value is wake up time in multiples of 100 us.
1553df0566a6SJani Nikula 	 */
1554a434689cSJani Nikula 	if (i915->display.vbt.version >= 205 &&
15552446e1d6SMatt Roper 	    (DISPLAY_VER(i915) >= 9 && !IS_BROXTON(i915))) {
1556df0566a6SJani Nikula 		switch (psr_table->tp1_wakeup_time) {
1557df0566a6SJani Nikula 		case 0:
15583cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 500;
1559df0566a6SJani Nikula 			break;
1560df0566a6SJani Nikula 		case 1:
15613cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 100;
1562df0566a6SJani Nikula 			break;
1563df0566a6SJani Nikula 		case 3:
15643cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 0;
1565df0566a6SJani Nikula 			break;
1566df0566a6SJani Nikula 		default:
1567dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1568e92cbf38SWambui Karuga 				    "VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1569df0566a6SJani Nikula 				    psr_table->tp1_wakeup_time);
1570df561f66SGustavo A. R. Silva 			fallthrough;
1571df0566a6SJani Nikula 		case 2:
15723cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 2500;
1573df0566a6SJani Nikula 			break;
1574df0566a6SJani Nikula 		}
1575df0566a6SJani Nikula 
1576df0566a6SJani Nikula 		switch (psr_table->tp2_tp3_wakeup_time) {
1577df0566a6SJani Nikula 		case 0:
15783cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 500;
1579df0566a6SJani Nikula 			break;
1580df0566a6SJani Nikula 		case 1:
15813cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 100;
1582df0566a6SJani Nikula 			break;
1583df0566a6SJani Nikula 		case 3:
15843cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 0;
1585df0566a6SJani Nikula 			break;
1586df0566a6SJani Nikula 		default:
1587dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1588e92cbf38SWambui Karuga 				    "VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1589df0566a6SJani Nikula 				    psr_table->tp2_tp3_wakeup_time);
1590df561f66SGustavo A. R. Silva 			fallthrough;
1591df0566a6SJani Nikula 		case 2:
15923cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
1593df0566a6SJani Nikula 		break;
1594df0566a6SJani Nikula 		}
1595df0566a6SJani Nikula 	} else {
15963cf05076SVille Syrjälä 		panel->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
15973cf05076SVille Syrjälä 		panel->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
1598df0566a6SJani Nikula 	}
1599df0566a6SJani Nikula 
1600a434689cSJani Nikula 	if (i915->display.vbt.version >= 226) {
1601b5ea9c93SDhinakaran Pandiyan 		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
1602df0566a6SJani Nikula 
1603a50cc495SVille Syrjälä 		wakeup_time = panel_bits(wakeup_time, panel_type, 2);
1604df0566a6SJani Nikula 		switch (wakeup_time) {
1605df0566a6SJani Nikula 		case 0:
1606df0566a6SJani Nikula 			wakeup_time = 500;
1607df0566a6SJani Nikula 			break;
1608df0566a6SJani Nikula 		case 1:
1609df0566a6SJani Nikula 			wakeup_time = 100;
1610df0566a6SJani Nikula 			break;
1611df0566a6SJani Nikula 		case 3:
1612df0566a6SJani Nikula 			wakeup_time = 50;
1613df0566a6SJani Nikula 			break;
1614df0566a6SJani Nikula 		default:
1615df0566a6SJani Nikula 		case 2:
1616df0566a6SJani Nikula 			wakeup_time = 2500;
1617df0566a6SJani Nikula 			break;
1618df0566a6SJani Nikula 		}
16193cf05076SVille Syrjälä 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
1620df0566a6SJani Nikula 	} else {
1621df0566a6SJani Nikula 		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
16223cf05076SVille Syrjälä 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = panel->vbt.psr.tp2_tp3_wakeup_time_us;
1623df0566a6SJani Nikula 	}
1624df0566a6SJani Nikula }
1625df0566a6SJani Nikula 
1626dbd440d8SJani Nikula static void parse_dsi_backlight_ports(struct drm_i915_private *i915,
16273cf05076SVille Syrjälä 				      struct intel_panel *panel,
16283cf05076SVille Syrjälä 				      enum port port)
1629df0566a6SJani Nikula {
1630ab55165dSJani Nikula 	enum port port_bc = DISPLAY_VER(i915) >= 11 ? PORT_B : PORT_C;
1631ab55165dSJani Nikula 
1632a434689cSJani Nikula 	if (!panel->vbt.dsi.config->dual_link || i915->display.vbt.version < 197) {
16333cf05076SVille Syrjälä 		panel->vbt.dsi.bl_ports = BIT(port);
16343cf05076SVille Syrjälä 		if (panel->vbt.dsi.config->cabc_supported)
16353cf05076SVille Syrjälä 			panel->vbt.dsi.cabc_ports = BIT(port);
1636df0566a6SJani Nikula 
1637df0566a6SJani Nikula 		return;
1638df0566a6SJani Nikula 	}
1639df0566a6SJani Nikula 
16403cf05076SVille Syrjälä 	switch (panel->vbt.dsi.config->dl_dcs_backlight_ports) {
1641df0566a6SJani Nikula 	case DL_DCS_PORT_A:
16423cf05076SVille Syrjälä 		panel->vbt.dsi.bl_ports = BIT(PORT_A);
1643df0566a6SJani Nikula 		break;
1644df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1645ab55165dSJani Nikula 		panel->vbt.dsi.bl_ports = BIT(port_bc);
1646df0566a6SJani Nikula 		break;
1647df0566a6SJani Nikula 	default:
1648df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
1649ab55165dSJani Nikula 		panel->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(port_bc);
1650df0566a6SJani Nikula 		break;
1651df0566a6SJani Nikula 	}
1652df0566a6SJani Nikula 
16533cf05076SVille Syrjälä 	if (!panel->vbt.dsi.config->cabc_supported)
1654df0566a6SJani Nikula 		return;
1655df0566a6SJani Nikula 
16563cf05076SVille Syrjälä 	switch (panel->vbt.dsi.config->dl_dcs_cabc_ports) {
1657df0566a6SJani Nikula 	case DL_DCS_PORT_A:
16583cf05076SVille Syrjälä 		panel->vbt.dsi.cabc_ports = BIT(PORT_A);
1659df0566a6SJani Nikula 		break;
1660df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1661ab55165dSJani Nikula 		panel->vbt.dsi.cabc_ports = BIT(port_bc);
1662df0566a6SJani Nikula 		break;
1663df0566a6SJani Nikula 	default:
1664df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
16653cf05076SVille Syrjälä 		panel->vbt.dsi.cabc_ports =
1666ab55165dSJani Nikula 					BIT(PORT_A) | BIT(port_bc);
1667df0566a6SJani Nikula 		break;
1668df0566a6SJani Nikula 	}
1669df0566a6SJani Nikula }
1670df0566a6SJani Nikula 
1671df0566a6SJani Nikula static void
16723cf05076SVille Syrjälä parse_mipi_config(struct drm_i915_private *i915,
16733cf05076SVille Syrjälä 		  struct intel_panel *panel)
1674df0566a6SJani Nikula {
1675df0566a6SJani Nikula 	const struct bdb_mipi_config *start;
1676df0566a6SJani Nikula 	const struct mipi_config *config;
1677df0566a6SJani Nikula 	const struct mipi_pps_data *pps;
16783cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1679df0566a6SJani Nikula 	enum port port;
1680df0566a6SJani Nikula 
1681df0566a6SJani Nikula 	/* parse MIPI blocks only if LFP type is MIPI */
1682dbd440d8SJani Nikula 	if (!intel_bios_is_dsi_present(i915, &port))
1683df0566a6SJani Nikula 		return;
1684df0566a6SJani Nikula 
1685df0566a6SJani Nikula 	/* Initialize this to undefined indicating no generic MIPI support */
16863cf05076SVille Syrjälä 	panel->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
1687df0566a6SJani Nikula 
1688df0566a6SJani Nikula 	/* Block #40 is already parsed and panel_fixed_mode is
1689dbd440d8SJani Nikula 	 * stored in i915->lfp_lvds_vbt_mode
1690df0566a6SJani Nikula 	 * resuse this when needed
1691df0566a6SJani Nikula 	 */
1692df0566a6SJani Nikula 
1693df0566a6SJani Nikula 	/* Parse #52 for panel index used from panel_type already
1694df0566a6SJani Nikula 	 * parsed
1695df0566a6SJani Nikula 	 */
1696e163cfb4SVille Syrjälä 	start = find_section(i915, BDB_MIPI_CONFIG);
1697df0566a6SJani Nikula 	if (!start) {
1698dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No MIPI config BDB found");
1699df0566a6SJani Nikula 		return;
1700df0566a6SJani Nikula 	}
1701df0566a6SJani Nikula 
1702dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI Config block, panel index = %d\n",
1703df0566a6SJani Nikula 		panel_type);
1704df0566a6SJani Nikula 
1705df0566a6SJani Nikula 	/*
1706df0566a6SJani Nikula 	 * get hold of the correct configuration block and pps data as per
1707df0566a6SJani Nikula 	 * the panel_type as index
1708df0566a6SJani Nikula 	 */
1709df0566a6SJani Nikula 	config = &start->config[panel_type];
1710df0566a6SJani Nikula 	pps = &start->pps[panel_type];
1711df0566a6SJani Nikula 
1712df0566a6SJani Nikula 	/* store as of now full data. Trim when we realise all is not needed */
17133cf05076SVille Syrjälä 	panel->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
17143cf05076SVille Syrjälä 	if (!panel->vbt.dsi.config)
1715df0566a6SJani Nikula 		return;
1716df0566a6SJani Nikula 
17173cf05076SVille Syrjälä 	panel->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
17183cf05076SVille Syrjälä 	if (!panel->vbt.dsi.pps) {
17193cf05076SVille Syrjälä 		kfree(panel->vbt.dsi.config);
1720df0566a6SJani Nikula 		return;
1721df0566a6SJani Nikula 	}
1722df0566a6SJani Nikula 
17233cf05076SVille Syrjälä 	parse_dsi_backlight_ports(i915, panel, port);
1724df0566a6SJani Nikula 
1725df0566a6SJani Nikula 	/* FIXME is the 90 vs. 270 correct? */
1726df0566a6SJani Nikula 	switch (config->rotation) {
1727df0566a6SJani Nikula 	case ENABLE_ROTATION_0:
1728df0566a6SJani Nikula 		/*
1729df0566a6SJani Nikula 		 * Most (all?) VBTs claim 0 degrees despite having
1730df0566a6SJani Nikula 		 * an upside down panel, thus we do not trust this.
1731df0566a6SJani Nikula 		 */
17323cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1733df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1734df0566a6SJani Nikula 		break;
1735df0566a6SJani Nikula 	case ENABLE_ROTATION_90:
17363cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1737df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1738df0566a6SJani Nikula 		break;
1739df0566a6SJani Nikula 	case ENABLE_ROTATION_180:
17403cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1741df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1742df0566a6SJani Nikula 		break;
1743df0566a6SJani Nikula 	case ENABLE_ROTATION_270:
17443cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1745df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1746df0566a6SJani Nikula 		break;
1747df0566a6SJani Nikula 	}
1748df0566a6SJani Nikula 
1749df0566a6SJani Nikula 	/* We have mandatory mipi config blocks. Initialize as generic panel */
17503cf05076SVille Syrjälä 	panel->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
1751df0566a6SJani Nikula }
1752df0566a6SJani Nikula 
1753df0566a6SJani Nikula /* Find the sequence block and size for the given panel. */
1754df0566a6SJani Nikula static const u8 *
1755df0566a6SJani Nikula find_panel_sequence_block(const struct bdb_mipi_sequence *sequence,
1756df0566a6SJani Nikula 			  u16 panel_id, u32 *seq_size)
1757df0566a6SJani Nikula {
1758df0566a6SJani Nikula 	u32 total = get_blocksize(sequence);
1759df0566a6SJani Nikula 	const u8 *data = &sequence->data[0];
1760df0566a6SJani Nikula 	u8 current_id;
1761df0566a6SJani Nikula 	u32 current_size;
1762df0566a6SJani Nikula 	int header_size = sequence->version >= 3 ? 5 : 3;
1763df0566a6SJani Nikula 	int index = 0;
1764df0566a6SJani Nikula 	int i;
1765df0566a6SJani Nikula 
1766df0566a6SJani Nikula 	/* skip new block size */
1767df0566a6SJani Nikula 	if (sequence->version >= 3)
1768df0566a6SJani Nikula 		data += 4;
1769df0566a6SJani Nikula 
1770df0566a6SJani Nikula 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
1771df0566a6SJani Nikula 		if (index + header_size > total) {
1772df0566a6SJani Nikula 			DRM_ERROR("Invalid sequence block (header)\n");
1773df0566a6SJani Nikula 			return NULL;
1774df0566a6SJani Nikula 		}
1775df0566a6SJani Nikula 
1776df0566a6SJani Nikula 		current_id = *(data + index);
1777df0566a6SJani Nikula 		if (sequence->version >= 3)
1778df0566a6SJani Nikula 			current_size = *((const u32 *)(data + index + 1));
1779df0566a6SJani Nikula 		else
1780df0566a6SJani Nikula 			current_size = *((const u16 *)(data + index + 1));
1781df0566a6SJani Nikula 
1782df0566a6SJani Nikula 		index += header_size;
1783df0566a6SJani Nikula 
1784df0566a6SJani Nikula 		if (index + current_size > total) {
1785df0566a6SJani Nikula 			DRM_ERROR("Invalid sequence block\n");
1786df0566a6SJani Nikula 			return NULL;
1787df0566a6SJani Nikula 		}
1788df0566a6SJani Nikula 
1789df0566a6SJani Nikula 		if (current_id == panel_id) {
1790df0566a6SJani Nikula 			*seq_size = current_size;
1791df0566a6SJani Nikula 			return data + index;
1792df0566a6SJani Nikula 		}
1793df0566a6SJani Nikula 
1794df0566a6SJani Nikula 		index += current_size;
1795df0566a6SJani Nikula 	}
1796df0566a6SJani Nikula 
1797df0566a6SJani Nikula 	DRM_ERROR("Sequence block detected but no valid configuration\n");
1798df0566a6SJani Nikula 
1799df0566a6SJani Nikula 	return NULL;
1800df0566a6SJani Nikula }
1801df0566a6SJani Nikula 
1802df0566a6SJani Nikula static int goto_next_sequence(const u8 *data, int index, int total)
1803df0566a6SJani Nikula {
1804df0566a6SJani Nikula 	u16 len;
1805df0566a6SJani Nikula 
1806df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1807df0566a6SJani Nikula 	for (index = index + 1; index < total; index += len) {
1808df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1809df0566a6SJani Nikula 		index++;
1810df0566a6SJani Nikula 
1811df0566a6SJani Nikula 		switch (operation_byte) {
1812df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_END:
1813df0566a6SJani Nikula 			return index;
1814df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1815df0566a6SJani Nikula 			if (index + 4 > total)
1816df0566a6SJani Nikula 				return 0;
1817df0566a6SJani Nikula 
1818df0566a6SJani Nikula 			len = *((const u16 *)(data + index + 2)) + 4;
1819df0566a6SJani Nikula 			break;
1820df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1821df0566a6SJani Nikula 			len = 4;
1822df0566a6SJani Nikula 			break;
1823df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1824df0566a6SJani Nikula 			len = 2;
1825df0566a6SJani Nikula 			break;
1826df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1827df0566a6SJani Nikula 			if (index + 7 > total)
1828df0566a6SJani Nikula 				return 0;
1829df0566a6SJani Nikula 			len = *(data + index + 6) + 7;
1830df0566a6SJani Nikula 			break;
1831df0566a6SJani Nikula 		default:
1832df0566a6SJani Nikula 			DRM_ERROR("Unknown operation byte\n");
1833df0566a6SJani Nikula 			return 0;
1834df0566a6SJani Nikula 		}
1835df0566a6SJani Nikula 	}
1836df0566a6SJani Nikula 
1837df0566a6SJani Nikula 	return 0;
1838df0566a6SJani Nikula }
1839df0566a6SJani Nikula 
1840df0566a6SJani Nikula static int goto_next_sequence_v3(const u8 *data, int index, int total)
1841df0566a6SJani Nikula {
1842df0566a6SJani Nikula 	int seq_end;
1843df0566a6SJani Nikula 	u16 len;
1844df0566a6SJani Nikula 	u32 size_of_sequence;
1845df0566a6SJani Nikula 
1846df0566a6SJani Nikula 	/*
1847df0566a6SJani Nikula 	 * Could skip sequence based on Size of Sequence alone, but also do some
1848df0566a6SJani Nikula 	 * checking on the structure.
1849df0566a6SJani Nikula 	 */
1850df0566a6SJani Nikula 	if (total < 5) {
1851df0566a6SJani Nikula 		DRM_ERROR("Too small sequence size\n");
1852df0566a6SJani Nikula 		return 0;
1853df0566a6SJani Nikula 	}
1854df0566a6SJani Nikula 
1855df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1856df0566a6SJani Nikula 	index++;
1857df0566a6SJani Nikula 
1858df0566a6SJani Nikula 	/*
1859df0566a6SJani Nikula 	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
1860df0566a6SJani Nikula 	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
1861df0566a6SJani Nikula 	 * byte.
1862df0566a6SJani Nikula 	 */
1863df0566a6SJani Nikula 	size_of_sequence = *((const u32 *)(data + index));
1864df0566a6SJani Nikula 	index += 4;
1865df0566a6SJani Nikula 
1866df0566a6SJani Nikula 	seq_end = index + size_of_sequence;
1867df0566a6SJani Nikula 	if (seq_end > total) {
1868df0566a6SJani Nikula 		DRM_ERROR("Invalid sequence size\n");
1869df0566a6SJani Nikula 		return 0;
1870df0566a6SJani Nikula 	}
1871df0566a6SJani Nikula 
1872df0566a6SJani Nikula 	for (; index < total; index += len) {
1873df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1874df0566a6SJani Nikula 		index++;
1875df0566a6SJani Nikula 
1876df0566a6SJani Nikula 		if (operation_byte == MIPI_SEQ_ELEM_END) {
1877df0566a6SJani Nikula 			if (index != seq_end) {
1878df0566a6SJani Nikula 				DRM_ERROR("Invalid element structure\n");
1879df0566a6SJani Nikula 				return 0;
1880df0566a6SJani Nikula 			}
1881df0566a6SJani Nikula 			return index;
1882df0566a6SJani Nikula 		}
1883df0566a6SJani Nikula 
1884df0566a6SJani Nikula 		len = *(data + index);
1885df0566a6SJani Nikula 		index++;
1886df0566a6SJani Nikula 
1887df0566a6SJani Nikula 		/*
1888df0566a6SJani Nikula 		 * FIXME: Would be nice to check elements like for v1/v2 in
1889df0566a6SJani Nikula 		 * goto_next_sequence() above.
1890df0566a6SJani Nikula 		 */
1891df0566a6SJani Nikula 		switch (operation_byte) {
1892df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1893df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1894df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1895df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1896df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SPI:
1897df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_PMIC:
1898df0566a6SJani Nikula 			break;
1899df0566a6SJani Nikula 		default:
1900df0566a6SJani Nikula 			DRM_ERROR("Unknown operation byte %u\n",
1901df0566a6SJani Nikula 				  operation_byte);
1902df0566a6SJani Nikula 			break;
1903df0566a6SJani Nikula 		}
1904df0566a6SJani Nikula 	}
1905df0566a6SJani Nikula 
1906df0566a6SJani Nikula 	return 0;
1907df0566a6SJani Nikula }
1908df0566a6SJani Nikula 
1909df0566a6SJani Nikula /*
1910df0566a6SJani Nikula  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
1911df0566a6SJani Nikula  * skip all delay + gpio operands and stop at the first DSI packet op.
1912df0566a6SJani Nikula  */
19133cf05076SVille Syrjälä static int get_init_otp_deassert_fragment_len(struct drm_i915_private *i915,
19143cf05076SVille Syrjälä 					      struct intel_panel *panel)
1915df0566a6SJani Nikula {
19163cf05076SVille Syrjälä 	const u8 *data = panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1917df0566a6SJani Nikula 	int index, len;
1918df0566a6SJani Nikula 
1919dbd440d8SJani Nikula 	if (drm_WARN_ON(&i915->drm,
19203cf05076SVille Syrjälä 			!data || panel->vbt.dsi.seq_version != 1))
1921df0566a6SJani Nikula 		return 0;
1922df0566a6SJani Nikula 
1923df0566a6SJani Nikula 	/* index = 1 to skip sequence byte */
1924df0566a6SJani Nikula 	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1925df0566a6SJani Nikula 		switch (data[index]) {
1926df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1927df0566a6SJani Nikula 			return index == 1 ? 0 : index;
1928df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1929df0566a6SJani Nikula 			len = 5; /* 1 byte for operand + uint32 */
1930df0566a6SJani Nikula 			break;
1931df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1932df0566a6SJani Nikula 			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1933df0566a6SJani Nikula 			break;
1934df0566a6SJani Nikula 		default:
1935df0566a6SJani Nikula 			return 0;
1936df0566a6SJani Nikula 		}
1937df0566a6SJani Nikula 	}
1938df0566a6SJani Nikula 
1939df0566a6SJani Nikula 	return 0;
1940df0566a6SJani Nikula }
1941df0566a6SJani Nikula 
1942df0566a6SJani Nikula /*
1943df0566a6SJani Nikula  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1944df0566a6SJani Nikula  * The deassert must be done before calling intel_dsi_device_ready, so for
1945df0566a6SJani Nikula  * these devices we split the init OTP sequence into a deassert sequence and
1946df0566a6SJani Nikula  * the actual init OTP part.
1947df0566a6SJani Nikula  */
19483cf05076SVille Syrjälä static void fixup_mipi_sequences(struct drm_i915_private *i915,
19493cf05076SVille Syrjälä 				 struct intel_panel *panel)
1950df0566a6SJani Nikula {
1951df0566a6SJani Nikula 	u8 *init_otp;
1952df0566a6SJani Nikula 	int len;
1953df0566a6SJani Nikula 
1954df0566a6SJani Nikula 	/* Limit this to VLV for now. */
1955dbd440d8SJani Nikula 	if (!IS_VALLEYVIEW(i915))
1956df0566a6SJani Nikula 		return;
1957df0566a6SJani Nikula 
1958df0566a6SJani Nikula 	/* Limit this to v1 vid-mode sequences */
19593cf05076SVille Syrjälä 	if (panel->vbt.dsi.config->is_cmd_mode ||
19603cf05076SVille Syrjälä 	    panel->vbt.dsi.seq_version != 1)
1961df0566a6SJani Nikula 		return;
1962df0566a6SJani Nikula 
1963df0566a6SJani Nikula 	/* Only do this if there are otp and assert seqs and no deassert seq */
19643cf05076SVille Syrjälä 	if (!panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
19653cf05076SVille Syrjälä 	    !panel->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
19663cf05076SVille Syrjälä 	    panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1967df0566a6SJani Nikula 		return;
1968df0566a6SJani Nikula 
1969df0566a6SJani Nikula 	/* The deassert-sequence ends at the first DSI packet */
19703cf05076SVille Syrjälä 	len = get_init_otp_deassert_fragment_len(i915, panel);
1971df0566a6SJani Nikula 	if (!len)
1972df0566a6SJani Nikula 		return;
1973df0566a6SJani Nikula 
1974dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1975e92cbf38SWambui Karuga 		    "Using init OTP fragment to deassert reset\n");
1976df0566a6SJani Nikula 
1977df0566a6SJani Nikula 	/* Copy the fragment, update seq byte and terminate it */
19783cf05076SVille Syrjälä 	init_otp = (u8 *)panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
19793cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
19803cf05076SVille Syrjälä 	if (!panel->vbt.dsi.deassert_seq)
1981df0566a6SJani Nikula 		return;
19823cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
19833cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1984df0566a6SJani Nikula 	/* Use the copy for deassert */
19853cf05076SVille Syrjälä 	panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
19863cf05076SVille Syrjälä 		panel->vbt.dsi.deassert_seq;
1987df0566a6SJani Nikula 	/* Replace the last byte of the fragment with init OTP seq byte */
1988df0566a6SJani Nikula 	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1989df0566a6SJani Nikula 	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
19903cf05076SVille Syrjälä 	panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
1991df0566a6SJani Nikula }
1992df0566a6SJani Nikula 
1993df0566a6SJani Nikula static void
19943cf05076SVille Syrjälä parse_mipi_sequence(struct drm_i915_private *i915,
19953cf05076SVille Syrjälä 		    struct intel_panel *panel)
1996df0566a6SJani Nikula {
19973cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1998df0566a6SJani Nikula 	const struct bdb_mipi_sequence *sequence;
1999df0566a6SJani Nikula 	const u8 *seq_data;
2000df0566a6SJani Nikula 	u32 seq_size;
2001df0566a6SJani Nikula 	u8 *data;
2002df0566a6SJani Nikula 	int index = 0;
2003df0566a6SJani Nikula 
2004df0566a6SJani Nikula 	/* Only our generic panel driver uses the sequence block. */
20053cf05076SVille Syrjälä 	if (panel->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
2006df0566a6SJani Nikula 		return;
2007df0566a6SJani Nikula 
2008e163cfb4SVille Syrjälä 	sequence = find_section(i915, BDB_MIPI_SEQUENCE);
2009df0566a6SJani Nikula 	if (!sequence) {
2010dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2011e92cbf38SWambui Karuga 			    "No MIPI Sequence found, parsing complete\n");
2012df0566a6SJani Nikula 		return;
2013df0566a6SJani Nikula 	}
2014df0566a6SJani Nikula 
2015df0566a6SJani Nikula 	/* Fail gracefully for forward incompatible sequence block. */
2016df0566a6SJani Nikula 	if (sequence->version >= 4) {
2017dbd440d8SJani Nikula 		drm_err(&i915->drm,
2018e92cbf38SWambui Karuga 			"Unable to parse MIPI Sequence Block v%u\n",
2019df0566a6SJani Nikula 			sequence->version);
2020df0566a6SJani Nikula 		return;
2021df0566a6SJani Nikula 	}
2022df0566a6SJani Nikula 
2023dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI sequence block v%u\n",
2024e92cbf38SWambui Karuga 		sequence->version);
2025df0566a6SJani Nikula 
2026df0566a6SJani Nikula 	seq_data = find_panel_sequence_block(sequence, panel_type, &seq_size);
2027df0566a6SJani Nikula 	if (!seq_data)
2028df0566a6SJani Nikula 		return;
2029df0566a6SJani Nikula 
2030df0566a6SJani Nikula 	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
2031df0566a6SJani Nikula 	if (!data)
2032df0566a6SJani Nikula 		return;
2033df0566a6SJani Nikula 
2034df0566a6SJani Nikula 	/* Parse the sequences, store pointers to each sequence. */
2035df0566a6SJani Nikula 	for (;;) {
2036df0566a6SJani Nikula 		u8 seq_id = *(data + index);
2037df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_END)
2038df0566a6SJani Nikula 			break;
2039df0566a6SJani Nikula 
2040df0566a6SJani Nikula 		if (seq_id >= MIPI_SEQ_MAX) {
2041dbd440d8SJani Nikula 			drm_err(&i915->drm, "Unknown sequence %u\n",
2042e92cbf38SWambui Karuga 				seq_id);
2043df0566a6SJani Nikula 			goto err;
2044df0566a6SJani Nikula 		}
2045df0566a6SJani Nikula 
2046df0566a6SJani Nikula 		/* Log about presence of sequences we won't run. */
2047df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
2048dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
2049e92cbf38SWambui Karuga 				    "Unsupported sequence %u\n", seq_id);
2050df0566a6SJani Nikula 
20513cf05076SVille Syrjälä 		panel->vbt.dsi.sequence[seq_id] = data + index;
2052df0566a6SJani Nikula 
2053df0566a6SJani Nikula 		if (sequence->version >= 3)
2054df0566a6SJani Nikula 			index = goto_next_sequence_v3(data, index, seq_size);
2055df0566a6SJani Nikula 		else
2056df0566a6SJani Nikula 			index = goto_next_sequence(data, index, seq_size);
2057df0566a6SJani Nikula 		if (!index) {
2058dbd440d8SJani Nikula 			drm_err(&i915->drm, "Invalid sequence %u\n",
2059e92cbf38SWambui Karuga 				seq_id);
2060df0566a6SJani Nikula 			goto err;
2061df0566a6SJani Nikula 		}
2062df0566a6SJani Nikula 	}
2063df0566a6SJani Nikula 
20643cf05076SVille Syrjälä 	panel->vbt.dsi.data = data;
20653cf05076SVille Syrjälä 	panel->vbt.dsi.size = seq_size;
20663cf05076SVille Syrjälä 	panel->vbt.dsi.seq_version = sequence->version;
2067df0566a6SJani Nikula 
20683cf05076SVille Syrjälä 	fixup_mipi_sequences(i915, panel);
2069df0566a6SJani Nikula 
2070dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "MIPI related VBT parsing complete\n");
2071df0566a6SJani Nikula 	return;
2072df0566a6SJani Nikula 
2073df0566a6SJani Nikula err:
2074df0566a6SJani Nikula 	kfree(data);
20753cf05076SVille Syrjälä 	memset(panel->vbt.dsi.sequence, 0, sizeof(panel->vbt.dsi.sequence));
2076df0566a6SJani Nikula }
2077df0566a6SJani Nikula 
20786e0d46e9SJani Nikula static void
2079e163cfb4SVille Syrjälä parse_compression_parameters(struct drm_i915_private *i915)
20806e0d46e9SJani Nikula {
20816e0d46e9SJani Nikula 	const struct bdb_compression_parameters *params;
20823162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
20836e0d46e9SJani Nikula 	u16 block_size;
20846e0d46e9SJani Nikula 	int index;
20856e0d46e9SJani Nikula 
2086a434689cSJani Nikula 	if (i915->display.vbt.version < 198)
20876e0d46e9SJani Nikula 		return;
20886e0d46e9SJani Nikula 
2089e163cfb4SVille Syrjälä 	params = find_section(i915, BDB_COMPRESSION_PARAMETERS);
20906e0d46e9SJani Nikula 	if (params) {
20916e0d46e9SJani Nikula 		/* Sanity checks */
20926e0d46e9SJani Nikula 		if (params->entry_size != sizeof(params->data[0])) {
2093e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2094e92cbf38SWambui Karuga 				    "VBT: unsupported compression param entry size\n");
20956e0d46e9SJani Nikula 			return;
20966e0d46e9SJani Nikula 		}
20976e0d46e9SJani Nikula 
20986e0d46e9SJani Nikula 		block_size = get_blocksize(params);
20996e0d46e9SJani Nikula 		if (block_size < sizeof(*params)) {
2100e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2101e92cbf38SWambui Karuga 				    "VBT: expected 16 compression param entries\n");
21026e0d46e9SJani Nikula 			return;
21036e0d46e9SJani Nikula 		}
21046e0d46e9SJani Nikula 	}
21056e0d46e9SJani Nikula 
2106a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
2107d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
21086e0d46e9SJani Nikula 
21096e0d46e9SJani Nikula 		if (!child->compression_enable)
21106e0d46e9SJani Nikula 			continue;
21116e0d46e9SJani Nikula 
21126e0d46e9SJani Nikula 		if (!params) {
2113e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2114e92cbf38SWambui Karuga 				    "VBT: compression params not available\n");
21156e0d46e9SJani Nikula 			continue;
21166e0d46e9SJani Nikula 		}
21176e0d46e9SJani Nikula 
21186e0d46e9SJani Nikula 		if (child->compression_method_cps) {
2119e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2120e92cbf38SWambui Karuga 				    "VBT: CPS compression not supported\n");
21216e0d46e9SJani Nikula 			continue;
21226e0d46e9SJani Nikula 		}
21236e0d46e9SJani Nikula 
21246e0d46e9SJani Nikula 		index = child->compression_structure_index;
21256e0d46e9SJani Nikula 
21266e0d46e9SJani Nikula 		devdata->dsc = kmemdup(&params->data[index],
21276e0d46e9SJani Nikula 				       sizeof(*devdata->dsc), GFP_KERNEL);
21286e0d46e9SJani Nikula 	}
21296e0d46e9SJani Nikula }
21306e0d46e9SJani Nikula 
2131df0566a6SJani Nikula static u8 translate_iboost(u8 val)
2132df0566a6SJani Nikula {
2133df0566a6SJani Nikula 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
2134df0566a6SJani Nikula 
2135df0566a6SJani Nikula 	if (val >= ARRAY_SIZE(mapping)) {
2136df0566a6SJani Nikula 		DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
2137df0566a6SJani Nikula 		return 0;
2138df0566a6SJani Nikula 	}
2139df0566a6SJani Nikula 	return mapping[val];
2140df0566a6SJani Nikula }
2141df0566a6SJani Nikula 
21429e1dbc1aSJani Nikula static const u8 cnp_ddc_pin_map[] = {
21439e1dbc1aSJani Nikula 	[0] = 0, /* N/A */
21449e1dbc1aSJani Nikula 	[DDC_BUS_DDI_B] = GMBUS_PIN_1_BXT,
21459e1dbc1aSJani Nikula 	[DDC_BUS_DDI_C] = GMBUS_PIN_2_BXT,
21469e1dbc1aSJani Nikula 	[DDC_BUS_DDI_D] = GMBUS_PIN_4_CNP, /* sic */
21479e1dbc1aSJani Nikula 	[DDC_BUS_DDI_F] = GMBUS_PIN_3_BXT, /* sic */
21489e1dbc1aSJani Nikula };
21499e1dbc1aSJani Nikula 
21509e1dbc1aSJani Nikula static const u8 icp_ddc_pin_map[] = {
21519e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
21529e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
21539e1dbc1aSJani Nikula 	[TGL_DDC_BUS_DDI_C] = GMBUS_PIN_3_BXT,
21549e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_1] = GMBUS_PIN_9_TC1_ICP,
21559e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_2] = GMBUS_PIN_10_TC2_ICP,
21569e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_3] = GMBUS_PIN_11_TC3_ICP,
21579e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_4] = GMBUS_PIN_12_TC4_ICP,
21589e1dbc1aSJani Nikula 	[TGL_DDC_BUS_PORT_5] = GMBUS_PIN_13_TC5_TGP,
21599e1dbc1aSJani Nikula 	[TGL_DDC_BUS_PORT_6] = GMBUS_PIN_14_TC6_TGP,
21609e1dbc1aSJani Nikula };
21619e1dbc1aSJani Nikula 
21629e1dbc1aSJani Nikula static const u8 rkl_pch_tgp_ddc_pin_map[] = {
21639e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
21649e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
21659e1dbc1aSJani Nikula 	[RKL_DDC_BUS_DDI_D] = GMBUS_PIN_9_TC1_ICP,
21669e1dbc1aSJani Nikula 	[RKL_DDC_BUS_DDI_E] = GMBUS_PIN_10_TC2_ICP,
21679e1dbc1aSJani Nikula };
21689e1dbc1aSJani Nikula 
21699e1dbc1aSJani Nikula static const u8 adls_ddc_pin_map[] = {
21709e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
21719e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC1] = GMBUS_PIN_9_TC1_ICP,
21729e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC2] = GMBUS_PIN_10_TC2_ICP,
21739e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC3] = GMBUS_PIN_11_TC3_ICP,
21749e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC4] = GMBUS_PIN_12_TC4_ICP,
21759e1dbc1aSJani Nikula };
21769e1dbc1aSJani Nikula 
21779e1dbc1aSJani Nikula static const u8 gen9bc_tgp_ddc_pin_map[] = {
21789e1dbc1aSJani Nikula 	[DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
21799e1dbc1aSJani Nikula 	[DDC_BUS_DDI_C] = GMBUS_PIN_9_TC1_ICP,
21809e1dbc1aSJani Nikula 	[DDC_BUS_DDI_D] = GMBUS_PIN_10_TC2_ICP,
21819e1dbc1aSJani Nikula };
21829e1dbc1aSJani Nikula 
2183af10ec31STejas Upadhyay static const u8 adlp_ddc_pin_map[] = {
2184af10ec31STejas Upadhyay 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
2185af10ec31STejas Upadhyay 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
2186af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC1] = GMBUS_PIN_9_TC1_ICP,
2187af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC2] = GMBUS_PIN_10_TC2_ICP,
2188af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC3] = GMBUS_PIN_11_TC3_ICP,
2189af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC4] = GMBUS_PIN_12_TC4_ICP,
2190af10ec31STejas Upadhyay };
2191af10ec31STejas Upadhyay 
21929e1dbc1aSJani Nikula static u8 map_ddc_pin(struct drm_i915_private *i915, u8 vbt_pin)
21939e1dbc1aSJani Nikula {
21949e1dbc1aSJani Nikula 	const u8 *ddc_pin_map;
21959e1dbc1aSJani Nikula 	int n_entries;
21969e1dbc1aSJani Nikula 
2197cf867d6aSRadhakrishna Sripada 	if (HAS_PCH_MTP(i915) || IS_ALDERLAKE_P(i915)) {
2198af10ec31STejas Upadhyay 		ddc_pin_map = adlp_ddc_pin_map;
2199af10ec31STejas Upadhyay 		n_entries = ARRAY_SIZE(adlp_ddc_pin_map);
2200af10ec31STejas Upadhyay 	} else if (IS_ALDERLAKE_S(i915)) {
22019e1dbc1aSJani Nikula 		ddc_pin_map = adls_ddc_pin_map;
22029e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(adls_ddc_pin_map);
22039e1dbc1aSJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_DG1) {
22049e1dbc1aSJani Nikula 		return vbt_pin;
22059e1dbc1aSJani Nikula 	} else if (IS_ROCKETLAKE(i915) && INTEL_PCH_TYPE(i915) == PCH_TGP) {
22069e1dbc1aSJani Nikula 		ddc_pin_map = rkl_pch_tgp_ddc_pin_map;
22079e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(rkl_pch_tgp_ddc_pin_map);
22089e1dbc1aSJani Nikula 	} else if (HAS_PCH_TGP(i915) && DISPLAY_VER(i915) == 9) {
22099e1dbc1aSJani Nikula 		ddc_pin_map = gen9bc_tgp_ddc_pin_map;
22109e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(gen9bc_tgp_ddc_pin_map);
22119e1dbc1aSJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_ICP) {
22129e1dbc1aSJani Nikula 		ddc_pin_map = icp_ddc_pin_map;
22139e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
22149e1dbc1aSJani Nikula 	} else if (HAS_PCH_CNP(i915)) {
22159e1dbc1aSJani Nikula 		ddc_pin_map = cnp_ddc_pin_map;
22169e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
22179e1dbc1aSJani Nikula 	} else {
22189e1dbc1aSJani Nikula 		/* Assuming direct map */
22199e1dbc1aSJani Nikula 		return vbt_pin;
22209e1dbc1aSJani Nikula 	}
22219e1dbc1aSJani Nikula 
22229e1dbc1aSJani Nikula 	if (vbt_pin < n_entries && ddc_pin_map[vbt_pin] != 0)
22239e1dbc1aSJani Nikula 		return ddc_pin_map[vbt_pin];
22249e1dbc1aSJani Nikula 
22259e1dbc1aSJani Nikula 	drm_dbg_kms(&i915->drm,
22269e1dbc1aSJani Nikula 		    "Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
22279e1dbc1aSJani Nikula 		    vbt_pin);
22289e1dbc1aSJani Nikula 	return 0;
22299e1dbc1aSJani Nikula }
22309e1dbc1aSJani Nikula 
2231df0566a6SJani Nikula static enum port get_port_by_ddc_pin(struct drm_i915_private *i915, u8 ddc_pin)
2232df0566a6SJani Nikula {
2233df0566a6SJani Nikula 	enum port port;
2234df0566a6SJani Nikula 
223595bbede5SJani Nikula 	if (!ddc_pin)
223695bbede5SJani Nikula 		return PORT_NONE;
223795bbede5SJani Nikula 
2238c4a774c4SJani Nikula 	for_each_port(port) {
2239d24b3475SVille Syrjälä 		const struct intel_bios_encoder_data *devdata =
2240d24b3475SVille Syrjälä 			i915->display.vbt.ports[port];
2241df0566a6SJani Nikula 
22425a449e58SJani Nikula 		if (devdata && ddc_pin == devdata->child.ddc_pin)
2243df0566a6SJani Nikula 			return port;
2244df0566a6SJani Nikula 	}
2245df0566a6SJani Nikula 
2246df0566a6SJani Nikula 	return PORT_NONE;
2247df0566a6SJani Nikula }
2248df0566a6SJani Nikula 
2249dab8477bSJani Nikula static void sanitize_ddc_pin(struct intel_bios_encoder_data *devdata,
2250df0566a6SJani Nikula 			     enum port port)
2251df0566a6SJani Nikula {
2252dab8477bSJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
225345c0673aSJani Nikula 	struct child_device_config *child;
2254dab8477bSJani Nikula 	u8 mapped_ddc_pin;
2255df0566a6SJani Nikula 	enum port p;
2256df0566a6SJani Nikula 
2257dab8477bSJani Nikula 	if (!devdata->child.ddc_pin)
2258dab8477bSJani Nikula 		return;
2259dab8477bSJani Nikula 
2260dab8477bSJani Nikula 	mapped_ddc_pin = map_ddc_pin(i915, devdata->child.ddc_pin);
2261dab8477bSJani Nikula 	if (!intel_gmbus_is_valid_pin(i915, mapped_ddc_pin)) {
2262dab8477bSJani Nikula 		drm_dbg_kms(&i915->drm,
2263dab8477bSJani Nikula 			    "Port %c has invalid DDC pin %d, "
2264dab8477bSJani Nikula 			    "sticking to defaults\n",
2265dab8477bSJani Nikula 			    port_name(port), mapped_ddc_pin);
2266dab8477bSJani Nikula 		devdata->child.ddc_pin = 0;
2267dab8477bSJani Nikula 		return;
2268dab8477bSJani Nikula 	}
2269dab8477bSJani Nikula 
2270dab8477bSJani Nikula 	p = get_port_by_ddc_pin(i915, devdata->child.ddc_pin);
2271894d1739SJani Nikula 	if (p == PORT_NONE)
2272894d1739SJani Nikula 		return;
2273894d1739SJani Nikula 
2274dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
2275e92cbf38SWambui Karuga 		    "port %c trying to use the same DDC pin (0x%x) as port %c, "
2276df0566a6SJani Nikula 		    "disabling port %c DVI/HDMI support\n",
2277dab8477bSJani Nikula 		    port_name(port), mapped_ddc_pin,
227841e35ffbSVille Syrjälä 		    port_name(p), port_name(p));
2279df0566a6SJani Nikula 
2280df0566a6SJani Nikula 	/*
2281894d1739SJani Nikula 	 * If we have multiple ports supposedly sharing the pin, then dvi/hdmi
2282894d1739SJani Nikula 	 * couldn't exist on the shared port. Otherwise they share the same ddc
2283894d1739SJani Nikula 	 * pin and system couldn't communicate with them separately.
2284df0566a6SJani Nikula 	 *
2285894d1739SJani Nikula 	 * Give inverse child device order the priority, last one wins. Yes,
2286894d1739SJani Nikula 	 * there are real machines (eg. Asrock B250M-HDV) where VBT has both
2287894d1739SJani Nikula 	 * port A and port E with the same AUX ch and we must pick port E :(
2288df0566a6SJani Nikula 	 */
2289a434689cSJani Nikula 	child = &i915->display.vbt.ports[p]->child;
229041e35ffbSVille Syrjälä 
229145c0673aSJani Nikula 	child->device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
229245c0673aSJani Nikula 	child->device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
229345c0673aSJani Nikula 
2294dab8477bSJani Nikula 	child->ddc_pin = 0;
2295df0566a6SJani Nikula }
2296df0566a6SJani Nikula 
2297df0566a6SJani Nikula static enum port get_port_by_aux_ch(struct drm_i915_private *i915, u8 aux_ch)
2298df0566a6SJani Nikula {
2299df0566a6SJani Nikula 	enum port port;
2300df0566a6SJani Nikula 
230195bbede5SJani Nikula 	if (!aux_ch)
230295bbede5SJani Nikula 		return PORT_NONE;
230395bbede5SJani Nikula 
2304c4a774c4SJani Nikula 	for_each_port(port) {
2305d24b3475SVille Syrjälä 		const struct intel_bios_encoder_data *devdata =
2306d24b3475SVille Syrjälä 			i915->display.vbt.ports[port];
2307df0566a6SJani Nikula 
23085a449e58SJani Nikula 		if (devdata && aux_ch == devdata->child.aux_channel)
2309df0566a6SJani Nikula 			return port;
2310df0566a6SJani Nikula 	}
2311df0566a6SJani Nikula 
2312df0566a6SJani Nikula 	return PORT_NONE;
2313df0566a6SJani Nikula }
2314df0566a6SJani Nikula 
231511182986SJani Nikula static void sanitize_aux_ch(struct intel_bios_encoder_data *devdata,
2316df0566a6SJani Nikula 			    enum port port)
2317df0566a6SJani Nikula {
231811182986SJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
231945c0673aSJani Nikula 	struct child_device_config *child;
2320df0566a6SJani Nikula 	enum port p;
2321df0566a6SJani Nikula 
232211182986SJani Nikula 	p = get_port_by_aux_ch(i915, devdata->child.aux_channel);
2323894d1739SJani Nikula 	if (p == PORT_NONE)
2324894d1739SJani Nikula 		return;
2325894d1739SJani Nikula 
2326dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
2327e92cbf38SWambui Karuga 		    "port %c trying to use the same AUX CH (0x%x) as port %c, "
2328df0566a6SJani Nikula 		    "disabling port %c DP support\n",
232911182986SJani Nikula 		    port_name(port), devdata->child.aux_channel,
233041e35ffbSVille Syrjälä 		    port_name(p), port_name(p));
2331df0566a6SJani Nikula 
2332df0566a6SJani Nikula 	/*
2333894d1739SJani Nikula 	 * If we have multiple ports supposedly sharing the aux channel, then DP
2334894d1739SJani Nikula 	 * couldn't exist on the shared port. Otherwise they share the same aux
2335894d1739SJani Nikula 	 * channel and system couldn't communicate with them separately.
2336df0566a6SJani Nikula 	 *
2337894d1739SJani Nikula 	 * Give inverse child device order the priority, last one wins. Yes,
2338894d1739SJani Nikula 	 * there are real machines (eg. Asrock B250M-HDV) where VBT has both
2339894d1739SJani Nikula 	 * port A and port E with the same AUX ch and we must pick port E :(
2340df0566a6SJani Nikula 	 */
2341a434689cSJani Nikula 	child = &i915->display.vbt.ports[p]->child;
234241e35ffbSVille Syrjälä 
234345c0673aSJani Nikula 	child->device_type &= ~DEVICE_TYPE_DISPLAYPORT_OUTPUT;
234411182986SJani Nikula 	child->aux_channel = 0;
2345df0566a6SJani Nikula }
2346df0566a6SJani Nikula 
234732c2bc89SVille Syrjälä static u8 dvo_port_type(u8 dvo_port)
234832c2bc89SVille Syrjälä {
234932c2bc89SVille Syrjälä 	switch (dvo_port) {
235032c2bc89SVille Syrjälä 	case DVO_PORT_HDMIA:
235132c2bc89SVille Syrjälä 	case DVO_PORT_HDMIB:
235232c2bc89SVille Syrjälä 	case DVO_PORT_HDMIC:
235332c2bc89SVille Syrjälä 	case DVO_PORT_HDMID:
235432c2bc89SVille Syrjälä 	case DVO_PORT_HDMIE:
235532c2bc89SVille Syrjälä 	case DVO_PORT_HDMIF:
235632c2bc89SVille Syrjälä 	case DVO_PORT_HDMIG:
235732c2bc89SVille Syrjälä 	case DVO_PORT_HDMIH:
235832c2bc89SVille Syrjälä 	case DVO_PORT_HDMII:
235932c2bc89SVille Syrjälä 		return DVO_PORT_HDMIA;
236032c2bc89SVille Syrjälä 	case DVO_PORT_DPA:
236132c2bc89SVille Syrjälä 	case DVO_PORT_DPB:
236232c2bc89SVille Syrjälä 	case DVO_PORT_DPC:
236332c2bc89SVille Syrjälä 	case DVO_PORT_DPD:
236432c2bc89SVille Syrjälä 	case DVO_PORT_DPE:
236532c2bc89SVille Syrjälä 	case DVO_PORT_DPF:
236632c2bc89SVille Syrjälä 	case DVO_PORT_DPG:
236732c2bc89SVille Syrjälä 	case DVO_PORT_DPH:
236832c2bc89SVille Syrjälä 	case DVO_PORT_DPI:
236932c2bc89SVille Syrjälä 		return DVO_PORT_DPA;
237032c2bc89SVille Syrjälä 	case DVO_PORT_MIPIA:
237132c2bc89SVille Syrjälä 	case DVO_PORT_MIPIB:
237232c2bc89SVille Syrjälä 	case DVO_PORT_MIPIC:
237332c2bc89SVille Syrjälä 	case DVO_PORT_MIPID:
237432c2bc89SVille Syrjälä 		return DVO_PORT_MIPIA;
237532c2bc89SVille Syrjälä 	default:
237632c2bc89SVille Syrjälä 		return dvo_port;
237732c2bc89SVille Syrjälä 	}
237832c2bc89SVille Syrjälä }
237932c2bc89SVille Syrjälä 
23804628142aSLucas De Marchi static enum port __dvo_port_to_port(int n_ports, int n_dvo,
23814628142aSLucas De Marchi 				    const int port_mapping[][3], u8 dvo_port)
2382df0566a6SJani Nikula {
2383df0566a6SJani Nikula 	enum port port;
2384df0566a6SJani Nikula 	int i;
2385df0566a6SJani Nikula 
23864628142aSLucas De Marchi 	for (port = PORT_A; port < n_ports; port++) {
23874628142aSLucas De Marchi 		for (i = 0; i < n_dvo; i++) {
23884628142aSLucas De Marchi 			if (port_mapping[port][i] == -1)
2389df0566a6SJani Nikula 				break;
2390df0566a6SJani Nikula 
23914628142aSLucas De Marchi 			if (dvo_port == port_mapping[port][i])
2392df0566a6SJani Nikula 				return port;
2393df0566a6SJani Nikula 		}
2394df0566a6SJani Nikula 	}
2395df0566a6SJani Nikula 
2396df0566a6SJani Nikula 	return PORT_NONE;
2397df0566a6SJani Nikula }
2398df0566a6SJani Nikula 
2399dbd440d8SJani Nikula static enum port dvo_port_to_port(struct drm_i915_private *i915,
24004628142aSLucas De Marchi 				  u8 dvo_port)
24014628142aSLucas De Marchi {
24024628142aSLucas De Marchi 	/*
24034628142aSLucas De Marchi 	 * Each DDI port can have more than one value on the "DVO Port" field,
24044628142aSLucas De Marchi 	 * so look for all the possible values for each port.
24054628142aSLucas De Marchi 	 */
24064628142aSLucas De Marchi 	static const int port_mapping[][3] = {
24074628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
24084628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
24094628142aSLucas De Marchi 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
24104628142aSLucas De Marchi 		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
24118c1a8f12SMatt Roper 		[PORT_E] = { DVO_PORT_HDMIE, DVO_PORT_DPE, DVO_PORT_CRT },
24124628142aSLucas De Marchi 		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
24134628142aSLucas De Marchi 		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2414176430ccSVille Syrjälä 		[PORT_H] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2415176430ccSVille Syrjälä 		[PORT_I] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
24164628142aSLucas De Marchi 	};
24174628142aSLucas De Marchi 	/*
24181d8ca002SVille Syrjälä 	 * RKL VBT uses PHY based mapping. Combo PHYs A,B,C,D
24191d8ca002SVille Syrjälä 	 * map to DDI A,B,TC1,TC2 respectively.
24204628142aSLucas De Marchi 	 */
24214628142aSLucas De Marchi 	static const int rkl_port_mapping[][3] = {
24224628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
24234628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
24244628142aSLucas De Marchi 		[PORT_C] = { -1 },
24251d8ca002SVille Syrjälä 		[PORT_TC1] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
24261d8ca002SVille Syrjälä 		[PORT_TC2] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
24274628142aSLucas De Marchi 	};
242818c283dfSAditya Swarup 	/*
242918c283dfSAditya Swarup 	 * Alderlake S ports used in the driver are PORT_A, PORT_D, PORT_E,
243018c283dfSAditya Swarup 	 * PORT_F and PORT_G, we need to map that to correct VBT sections.
243118c283dfSAditya Swarup 	 */
243218c283dfSAditya Swarup 	static const int adls_port_mapping[][3] = {
243318c283dfSAditya Swarup 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
243418c283dfSAditya Swarup 		[PORT_B] = { -1 },
243518c283dfSAditya Swarup 		[PORT_C] = { -1 },
243618c283dfSAditya Swarup 		[PORT_TC1] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
243718c283dfSAditya Swarup 		[PORT_TC2] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
243818c283dfSAditya Swarup 		[PORT_TC3] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
243918c283dfSAditya Swarup 		[PORT_TC4] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
244018c283dfSAditya Swarup 	};
2441eeb63c54SJosé Roberto de Souza 	static const int xelpd_port_mapping[][3] = {
2442eeb63c54SJosé Roberto de Souza 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2443eeb63c54SJosé Roberto de Souza 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2444eeb63c54SJosé Roberto de Souza 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2445eeb63c54SJosé Roberto de Souza 		[PORT_D_XELPD] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2446eeb63c54SJosé Roberto de Souza 		[PORT_E_XELPD] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
2447eeb63c54SJosé Roberto de Souza 		[PORT_TC1] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
2448eeb63c54SJosé Roberto de Souza 		[PORT_TC2] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2449eeb63c54SJosé Roberto de Souza 		[PORT_TC3] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2450eeb63c54SJosé Roberto de Souza 		[PORT_TC4] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
2451eeb63c54SJosé Roberto de Souza 	};
24524628142aSLucas De Marchi 
2453612dc414SImre Deak 	if (DISPLAY_VER(i915) >= 13)
2454eeb63c54SJosé Roberto de Souza 		return __dvo_port_to_port(ARRAY_SIZE(xelpd_port_mapping),
2455eeb63c54SJosé Roberto de Souza 					  ARRAY_SIZE(xelpd_port_mapping[0]),
2456eeb63c54SJosé Roberto de Souza 					  xelpd_port_mapping,
2457eeb63c54SJosé Roberto de Souza 					  dvo_port);
2458eeb63c54SJosé Roberto de Souza 	else if (IS_ALDERLAKE_S(i915))
245918c283dfSAditya Swarup 		return __dvo_port_to_port(ARRAY_SIZE(adls_port_mapping),
246018c283dfSAditya Swarup 					  ARRAY_SIZE(adls_port_mapping[0]),
246118c283dfSAditya Swarup 					  adls_port_mapping,
246218c283dfSAditya Swarup 					  dvo_port);
2463dbd440d8SJani Nikula 	else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
24644628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(rkl_port_mapping),
24654628142aSLucas De Marchi 					  ARRAY_SIZE(rkl_port_mapping[0]),
24664628142aSLucas De Marchi 					  rkl_port_mapping,
24674628142aSLucas De Marchi 					  dvo_port);
24684628142aSLucas De Marchi 	else
24694628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(port_mapping),
24704628142aSLucas De Marchi 					  ARRAY_SIZE(port_mapping[0]),
24714628142aSLucas De Marchi 					  port_mapping,
24724628142aSLucas De Marchi 					  dvo_port);
24734628142aSLucas De Marchi }
24744628142aSLucas De Marchi 
2475118b5c13SVille Syrjälä static enum port
2476118b5c13SVille Syrjälä dsi_dvo_port_to_port(struct drm_i915_private *i915, u8 dvo_port)
2477118b5c13SVille Syrjälä {
2478118b5c13SVille Syrjälä 	switch (dvo_port) {
2479118b5c13SVille Syrjälä 	case DVO_PORT_MIPIA:
2480118b5c13SVille Syrjälä 		return PORT_A;
2481118b5c13SVille Syrjälä 	case DVO_PORT_MIPIC:
2482118b5c13SVille Syrjälä 		if (DISPLAY_VER(i915) >= 11)
2483118b5c13SVille Syrjälä 			return PORT_B;
2484118b5c13SVille Syrjälä 		else
2485118b5c13SVille Syrjälä 			return PORT_C;
2486118b5c13SVille Syrjälä 	default:
2487118b5c13SVille Syrjälä 		return PORT_NONE;
2488118b5c13SVille Syrjälä 	}
2489118b5c13SVille Syrjälä }
2490118b5c13SVille Syrjälä 
2491b60e320bSLee Shawn C static int parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)
2492b60e320bSLee Shawn C {
2493b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
2494b60e320bSLee Shawn C 	default:
2495b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_DEF:
2496b60e320bSLee Shawn C 		return 0;
2497b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR20:
2498b60e320bSLee Shawn C 		return 2000000;
2499b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR13P5:
2500b60e320bSLee Shawn C 		return 1350000;
2501b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR10:
2502b60e320bSLee Shawn C 		return 1000000;
2503b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR3:
2504b60e320bSLee Shawn C 		return 810000;
2505b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR2:
2506b60e320bSLee Shawn C 		return 540000;
2507b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR:
2508b60e320bSLee Shawn C 		return 270000;
2509b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_LBR:
2510b60e320bSLee Shawn C 		return 162000;
2511b60e320bSLee Shawn C 	}
2512b60e320bSLee Shawn C }
2513b60e320bSLee Shawn C 
2514b60e320bSLee Shawn C static int parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)
2515b60e320bSLee Shawn C {
2516b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
2517b60e320bSLee Shawn C 	default:
2518b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR3:
2519b60e320bSLee Shawn C 		return 810000;
2520b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR2:
2521b60e320bSLee Shawn C 		return 540000;
2522b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR:
2523b60e320bSLee Shawn C 		return 270000;
2524b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_LBR:
2525b60e320bSLee Shawn C 		return 162000;
2526b60e320bSLee Shawn C 	}
2527b60e320bSLee Shawn C }
2528b60e320bSLee Shawn C 
252902107ef1SVille Syrjälä int intel_bios_dp_max_link_rate(const struct intel_bios_encoder_data *devdata)
253072337aacSJani Nikula {
2531a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 216)
253272337aacSJani Nikula 		return 0;
253372337aacSJani Nikula 
2534a434689cSJani Nikula 	if (devdata->i915->display.vbt.version >= 230)
253572337aacSJani Nikula 		return parse_bdb_230_dp_max_link_rate(devdata->child.dp_max_link_rate);
253672337aacSJani Nikula 	else
253772337aacSJani Nikula 		return parse_bdb_216_dp_max_link_rate(devdata->child.dp_max_link_rate);
253872337aacSJani Nikula }
253972337aacSJani Nikula 
254002107ef1SVille Syrjälä int intel_bios_dp_max_lane_count(const struct intel_bios_encoder_data *devdata)
25414182a311SVille Syrjälä {
25424182a311SVille Syrjälä 	if (!devdata || devdata->i915->display.vbt.version < 244)
25434182a311SVille Syrjälä 		return 0;
25444182a311SVille Syrjälä 
25454182a311SVille Syrjälä 	return devdata->child.dp_max_lane_count + 1;
25464182a311SVille Syrjälä }
25474182a311SVille Syrjälä 
2548d0ab409dSJani Nikula static void sanitize_device_type(struct intel_bios_encoder_data *devdata,
2549d0ab409dSJani Nikula 				 enum port port)
2550d0ab409dSJani Nikula {
2551d0ab409dSJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
2552d0ab409dSJani Nikula 	bool is_hdmi;
2553d0ab409dSJani Nikula 
2554005e9537SMatt Roper 	if (port != PORT_A || DISPLAY_VER(i915) >= 12)
2555d0ab409dSJani Nikula 		return;
2556d0ab409dSJani Nikula 
255786996822SJani Nikula 	if (!intel_bios_encoder_supports_dvi(devdata))
2558d0ab409dSJani Nikula 		return;
2559d0ab409dSJani Nikula 
256086996822SJani Nikula 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2561d0ab409dSJani Nikula 
2562d0ab409dSJani Nikula 	drm_dbg_kms(&i915->drm, "VBT claims port A supports DVI%s, ignoring\n",
2563d0ab409dSJani Nikula 		    is_hdmi ? "/HDMI" : "");
2564d0ab409dSJani Nikula 
2565d0ab409dSJani Nikula 	devdata->child.device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
2566d0ab409dSJani Nikula 	devdata->child.device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
2567d0ab409dSJani Nikula }
2568d0ab409dSJani Nikula 
2569d0ab409dSJani Nikula static bool
2570d0ab409dSJani Nikula intel_bios_encoder_supports_crt(const struct intel_bios_encoder_data *devdata)
2571d0ab409dSJani Nikula {
2572d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
2573d0ab409dSJani Nikula }
2574d0ab409dSJani Nikula 
257545c0673aSJani Nikula bool
2576d0ab409dSJani Nikula intel_bios_encoder_supports_dvi(const struct intel_bios_encoder_data *devdata)
2577d0ab409dSJani Nikula {
2578d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
2579d0ab409dSJani Nikula }
2580d0ab409dSJani Nikula 
258145c0673aSJani Nikula bool
2582d0ab409dSJani Nikula intel_bios_encoder_supports_hdmi(const struct intel_bios_encoder_data *devdata)
2583d0ab409dSJani Nikula {
2584d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dvi(devdata) &&
2585d0ab409dSJani Nikula 		(devdata->child.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
2586d0ab409dSJani Nikula }
2587d0ab409dSJani Nikula 
258845c0673aSJani Nikula bool
2589d0ab409dSJani Nikula intel_bios_encoder_supports_dp(const struct intel_bios_encoder_data *devdata)
2590d0ab409dSJani Nikula {
2591d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2592d0ab409dSJani Nikula }
2593d0ab409dSJani Nikula 
25949d4b7af5SVille Syrjälä bool
2595d0ab409dSJani Nikula intel_bios_encoder_supports_edp(const struct intel_bios_encoder_data *devdata)
2596d0ab409dSJani Nikula {
2597d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dp(devdata) &&
2598d0ab409dSJani Nikula 		devdata->child.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR;
2599d0ab409dSJani Nikula }
2600d0ab409dSJani Nikula 
2601ba00eb6aSVille Syrjälä static bool
2602ba00eb6aSVille Syrjälä intel_bios_encoder_supports_dsi(const struct intel_bios_encoder_data *devdata)
2603ba00eb6aSVille Syrjälä {
2604ba00eb6aSVille Syrjälä 	return devdata->child.device_type & DEVICE_TYPE_MIPI_OUTPUT;
2605ba00eb6aSVille Syrjälä }
2606ba00eb6aSVille Syrjälä 
2607db5d650fSVille Syrjälä bool
2608db5d650fSVille Syrjälä intel_bios_encoder_is_lspcon(const struct intel_bios_encoder_data *devdata)
2609db5d650fSVille Syrjälä {
2610db5d650fSVille Syrjälä 	return devdata && HAS_LSPCON(devdata->i915) && devdata->child.lspcon;
2611db5d650fSVille Syrjälä }
2612db5d650fSVille Syrjälä 
261302107ef1SVille Syrjälä /* This is an index in the HDMI/DVI DDI buffer translation table, or -1 */
261402107ef1SVille Syrjälä int intel_bios_hdmi_level_shift(const struct intel_bios_encoder_data *devdata)
2615a9a56e76SJani Nikula {
2616a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 158)
2617a9a56e76SJani Nikula 		return -1;
2618a9a56e76SJani Nikula 
2619a9a56e76SJani Nikula 	return devdata->child.hdmi_level_shifter_value;
2620a9a56e76SJani Nikula }
2621a9a56e76SJani Nikula 
262202107ef1SVille Syrjälä int intel_bios_hdmi_max_tmds_clock(const struct intel_bios_encoder_data *devdata)
26236ba69981SJani Nikula {
2624a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 204)
26256ba69981SJani Nikula 		return 0;
26266ba69981SJani Nikula 
26276ba69981SJani Nikula 	switch (devdata->child.hdmi_max_data_rate) {
26286ba69981SJani Nikula 	default:
26296ba69981SJani Nikula 		MISSING_CASE(devdata->child.hdmi_max_data_rate);
26306ba69981SJani Nikula 		fallthrough;
26316ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_PLATFORM:
26326ba69981SJani Nikula 		return 0;
26335708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_594:
26345708fe0dSLee Shawn C 		return 594000;
26355708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_340:
26365708fe0dSLee Shawn C 		return 340000;
26375708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_300:
26385708fe0dSLee Shawn C 		return 300000;
26396ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_297:
26406ba69981SJani Nikula 		return 297000;
26416ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_165:
26426ba69981SJani Nikula 		return 165000;
26436ba69981SJani Nikula 	}
26446ba69981SJani Nikula }
26456ba69981SJani Nikula 
26465a9d38b2SLucas De Marchi static bool is_port_valid(struct drm_i915_private *i915, enum port port)
26475a9d38b2SLucas De Marchi {
26485a9d38b2SLucas De Marchi 	/*
2649cad83b40SLucas De Marchi 	 * On some ICL SKUs port F is not present, but broken VBTs mark
26505a9d38b2SLucas De Marchi 	 * the port as present. Only try to initialize port F for the
26515a9d38b2SLucas De Marchi 	 * SKUs that may actually have it.
26525a9d38b2SLucas De Marchi 	 */
2653cad83b40SLucas De Marchi 	if (port == PORT_F && IS_ICELAKE(i915))
2654cad83b40SLucas De Marchi 		return IS_ICL_WITH_PORT_F(i915);
26555a9d38b2SLucas De Marchi 
26565a9d38b2SLucas De Marchi 	return true;
26575a9d38b2SLucas De Marchi }
26585a9d38b2SLucas De Marchi 
26598d2ba05bSJani Nikula static void print_ddi_port(const struct intel_bios_encoder_data *devdata,
26608d2ba05bSJani Nikula 			   enum port port)
2661df0566a6SJani Nikula {
2662c78783f3SJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
2663d1dad6f4SJani Nikula 	const struct child_device_config *child = &devdata->child;
2664ba00eb6aSVille Syrjälä 	bool is_dvi, is_hdmi, is_dp, is_edp, is_dsi, is_crt, supports_typec_usb, supports_tbt;
266572337aacSJani Nikula 	int dp_boost_level, dp_max_link_rate, hdmi_boost_level, hdmi_level_shift, max_tmds_clock;
2666df0566a6SJani Nikula 
2667d0ab409dSJani Nikula 	is_dvi = intel_bios_encoder_supports_dvi(devdata);
2668d0ab409dSJani Nikula 	is_dp = intel_bios_encoder_supports_dp(devdata);
2669d0ab409dSJani Nikula 	is_crt = intel_bios_encoder_supports_crt(devdata);
2670d0ab409dSJani Nikula 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2671d0ab409dSJani Nikula 	is_edp = intel_bios_encoder_supports_edp(devdata);
2672ba00eb6aSVille Syrjälä 	is_dsi = intel_bios_encoder_supports_dsi(devdata);
2673df0566a6SJani Nikula 
2674f08fbe6aSJani Nikula 	supports_typec_usb = intel_bios_encoder_supports_typec_usb(devdata);
2675f08fbe6aSJani Nikula 	supports_tbt = intel_bios_encoder_supports_tbt(devdata);
2676df0566a6SJani Nikula 
2677dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
2678ba00eb6aSVille Syrjälä 		    "Port %c VBT info: CRT:%d DVI:%d HDMI:%d DP:%d eDP:%d DSI:%d LSPCON:%d USB-Type-C:%d TBT:%d DSC:%d\n",
2679ba00eb6aSVille Syrjälä 		    port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp, is_dsi,
2680db5d650fSVille Syrjälä 		    intel_bios_encoder_is_lspcon(devdata),
2681f08fbe6aSJani Nikula 		    supports_typec_usb, supports_tbt,
26826e0d46e9SJani Nikula 		    devdata->dsc != NULL);
2683df0566a6SJani Nikula 
268402107ef1SVille Syrjälä 	hdmi_level_shift = intel_bios_hdmi_level_shift(devdata);
2685a9a56e76SJani Nikula 	if (hdmi_level_shift >= 0) {
2686dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
26876ee8d381SJani Nikula 			    "Port %c VBT HDMI level shift: %d\n",
2688a9a56e76SJani Nikula 			    port_name(port), hdmi_level_shift);
2689df0566a6SJani Nikula 	}
2690df0566a6SJani Nikula 
269102107ef1SVille Syrjälä 	max_tmds_clock = intel_bios_hdmi_max_tmds_clock(devdata);
2692df0566a6SJani Nikula 	if (max_tmds_clock)
2693dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
26946ee8d381SJani Nikula 			    "Port %c VBT HDMI max TMDS clock: %d kHz\n",
2695df0566a6SJani Nikula 			    port_name(port), max_tmds_clock);
2696df0566a6SJani Nikula 
2697c0a950d1SJani Nikula 	/* I_boost config for SKL and above */
269802107ef1SVille Syrjälä 	dp_boost_level = intel_bios_dp_boost_level(devdata);
2699c0a950d1SJani Nikula 	if (dp_boost_level)
2700dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
27016ee8d381SJani Nikula 			    "Port %c VBT (e)DP boost level: %d\n",
2702c0a950d1SJani Nikula 			    port_name(port), dp_boost_level);
2703c0a950d1SJani Nikula 
270402107ef1SVille Syrjälä 	hdmi_boost_level = intel_bios_hdmi_boost_level(devdata);
2705c0a950d1SJani Nikula 	if (hdmi_boost_level)
2706dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
27076ee8d381SJani Nikula 			    "Port %c VBT HDMI boost level: %d\n",
2708c0a950d1SJani Nikula 			    port_name(port), hdmi_boost_level);
2709df0566a6SJani Nikula 
271002107ef1SVille Syrjälä 	dp_max_link_rate = intel_bios_dp_max_link_rate(devdata);
271172337aacSJani Nikula 	if (dp_max_link_rate)
2712dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
27136ee8d381SJani Nikula 			    "Port %c VBT DP max link rate: %d\n",
271472337aacSJani Nikula 			    port_name(port), dp_max_link_rate);
2715429a0955SVille Syrjälä 
2716429a0955SVille Syrjälä 	/*
2717429a0955SVille Syrjälä 	 * FIXME need to implement support for VBT
2718429a0955SVille Syrjälä 	 * vswing/preemph tables should this ever trigger.
2719429a0955SVille Syrjälä 	 */
2720429a0955SVille Syrjälä 	drm_WARN(&i915->drm, child->use_vbt_vswing,
2721429a0955SVille Syrjälä 		 "Port %c asks to use VBT vswing/preemph tables\n",
2722429a0955SVille Syrjälä 		 port_name(port));
27238d2ba05bSJani Nikula }
27248d2ba05bSJani Nikula 
27258d2ba05bSJani Nikula static void parse_ddi_port(struct intel_bios_encoder_data *devdata)
27268d2ba05bSJani Nikula {
27278d2ba05bSJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
27288d2ba05bSJani Nikula 	const struct child_device_config *child = &devdata->child;
27298d2ba05bSJani Nikula 	enum port port;
27308d2ba05bSJani Nikula 
27318d2ba05bSJani Nikula 	port = dvo_port_to_port(i915, child->dvo_port);
2732ba00eb6aSVille Syrjälä 	if (port == PORT_NONE && DISPLAY_VER(i915) >= 11)
2733ba00eb6aSVille Syrjälä 		port = dsi_dvo_port_to_port(i915, child->dvo_port);
27348d2ba05bSJani Nikula 	if (port == PORT_NONE)
27358d2ba05bSJani Nikula 		return;
27368d2ba05bSJani Nikula 
27378d2ba05bSJani Nikula 	if (!is_port_valid(i915, port)) {
27388d2ba05bSJani Nikula 		drm_dbg_kms(&i915->drm,
27398d2ba05bSJani Nikula 			    "VBT reports port %c as supported, but that can't be true: skipping\n",
27408d2ba05bSJani Nikula 			    port_name(port));
27418d2ba05bSJani Nikula 		return;
27428d2ba05bSJani Nikula 	}
27438d2ba05bSJani Nikula 
2744a434689cSJani Nikula 	if (i915->display.vbt.ports[port]) {
27458d2ba05bSJani Nikula 		drm_dbg_kms(&i915->drm,
27468d2ba05bSJani Nikula 			    "More than one child device for port %c in VBT, using the first.\n",
27478d2ba05bSJani Nikula 			    port_name(port));
27488d2ba05bSJani Nikula 		return;
27498d2ba05bSJani Nikula 	}
27508d2ba05bSJani Nikula 
27518d2ba05bSJani Nikula 	sanitize_device_type(devdata, port);
27528d2ba05bSJani Nikula 
27538d2ba05bSJani Nikula 	if (intel_bios_encoder_supports_dvi(devdata))
27548d2ba05bSJani Nikula 		sanitize_ddc_pin(devdata, port);
27558d2ba05bSJani Nikula 
27568d2ba05bSJani Nikula 	if (intel_bios_encoder_supports_dp(devdata))
27578d2ba05bSJani Nikula 		sanitize_aux_ch(devdata, port);
2758df0566a6SJani Nikula 
2759a434689cSJani Nikula 	i915->display.vbt.ports[port] = devdata;
2760df0566a6SJani Nikula }
2761df0566a6SJani Nikula 
2762b90b6e41SVille Syrjälä static bool has_ddi_port_info(struct drm_i915_private *i915)
2763b90b6e41SVille Syrjälä {
2764594c504dSVille Syrjälä 	return DISPLAY_VER(i915) >= 5 || IS_G4X(i915);
2765b90b6e41SVille Syrjälä }
2766b90b6e41SVille Syrjälä 
2767ef0096e4SJani Nikula static void parse_ddi_ports(struct drm_i915_private *i915)
2768df0566a6SJani Nikula {
27693162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2770e61f294cSJani Nikula 	enum port port;
2771df0566a6SJani Nikula 
2772eb9fcf63SVille Syrjälä 	if (!has_ddi_port_info(i915))
2773df0566a6SJani Nikula 		return;
2774df0566a6SJani Nikula 
2775a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
2776c78783f3SJani Nikula 		parse_ddi_port(devdata);
2777e61f294cSJani Nikula 
2778e61f294cSJani Nikula 	for_each_port(port) {
2779a434689cSJani Nikula 		if (i915->display.vbt.ports[port])
2780a434689cSJani Nikula 			print_ddi_port(i915->display.vbt.ports[port], port);
2781e61f294cSJani Nikula 	}
2782df0566a6SJani Nikula }
2783df0566a6SJani Nikula 
2784df0566a6SJani Nikula static void
2785e163cfb4SVille Syrjälä parse_general_definitions(struct drm_i915_private *i915)
2786df0566a6SJani Nikula {
2787df0566a6SJani Nikula 	const struct bdb_general_definitions *defs;
27883162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2789df0566a6SJani Nikula 	const struct child_device_config *child;
27900d9ef19bSJani Nikula 	int i, child_device_num;
2791df0566a6SJani Nikula 	u8 expected_size;
2792df0566a6SJani Nikula 	u16 block_size;
2793df0566a6SJani Nikula 	int bus_pin;
2794df0566a6SJani Nikula 
2795e163cfb4SVille Syrjälä 	defs = find_section(i915, BDB_GENERAL_DEFINITIONS);
2796df0566a6SJani Nikula 	if (!defs) {
2797dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2798e92cbf38SWambui Karuga 			    "No general definition block is found, no devices defined.\n");
2799df0566a6SJani Nikula 		return;
2800df0566a6SJani Nikula 	}
2801df0566a6SJani Nikula 
2802df0566a6SJani Nikula 	block_size = get_blocksize(defs);
2803df0566a6SJani Nikula 	if (block_size < sizeof(*defs)) {
2804dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2805e92cbf38SWambui Karuga 			    "General definitions block too small (%u)\n",
2806df0566a6SJani Nikula 			    block_size);
2807df0566a6SJani Nikula 		return;
2808df0566a6SJani Nikula 	}
2809df0566a6SJani Nikula 
2810df0566a6SJani Nikula 	bus_pin = defs->crt_ddc_gmbus_pin;
2811dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "crt_ddc_bus_pin: %d\n", bus_pin);
2812dbd440d8SJani Nikula 	if (intel_gmbus_is_valid_pin(i915, bus_pin))
2813a434689cSJani Nikula 		i915->display.vbt.crt_ddc_pin = bus_pin;
2814df0566a6SJani Nikula 
2815a434689cSJani Nikula 	if (i915->display.vbt.version < 106) {
2816df0566a6SJani Nikula 		expected_size = 22;
2817a434689cSJani Nikula 	} else if (i915->display.vbt.version < 111) {
2818df0566a6SJani Nikula 		expected_size = 27;
2819a434689cSJani Nikula 	} else if (i915->display.vbt.version < 195) {
2820df0566a6SJani Nikula 		expected_size = LEGACY_CHILD_DEVICE_CONFIG_SIZE;
2821a434689cSJani Nikula 	} else if (i915->display.vbt.version == 195) {
2822df0566a6SJani Nikula 		expected_size = 37;
2823a434689cSJani Nikula 	} else if (i915->display.vbt.version <= 215) {
2824df0566a6SJani Nikula 		expected_size = 38;
2825*0eaca1edSVille Syrjälä 	} else if (i915->display.vbt.version <= 250) {
2826df0566a6SJani Nikula 		expected_size = 39;
2827df0566a6SJani Nikula 	} else {
2828df0566a6SJani Nikula 		expected_size = sizeof(*child);
2829df0566a6SJani Nikula 		BUILD_BUG_ON(sizeof(*child) < 39);
2830dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
2831e92cbf38SWambui Karuga 			"Expected child device config size for VBT version %u not known; assuming %u\n",
2832a434689cSJani Nikula 			i915->display.vbt.version, expected_size);
2833df0566a6SJani Nikula 	}
2834df0566a6SJani Nikula 
2835df0566a6SJani Nikula 	/* Flag an error for unexpected size, but continue anyway. */
2836df0566a6SJani Nikula 	if (defs->child_dev_size != expected_size)
2837dbd440d8SJani Nikula 		drm_err(&i915->drm,
2838e92cbf38SWambui Karuga 			"Unexpected child device config size %u (expected %u for VBT version %u)\n",
2839a434689cSJani Nikula 			defs->child_dev_size, expected_size, i915->display.vbt.version);
2840df0566a6SJani Nikula 
2841df0566a6SJani Nikula 	/* The legacy sized child device config is the minimum we need. */
2842df0566a6SJani Nikula 	if (defs->child_dev_size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
2843dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2844e92cbf38SWambui Karuga 			    "Child device config size %u is too small.\n",
2845df0566a6SJani Nikula 			    defs->child_dev_size);
2846df0566a6SJani Nikula 		return;
2847df0566a6SJani Nikula 	}
2848df0566a6SJani Nikula 
2849df0566a6SJani Nikula 	/* get the number of child device */
2850df0566a6SJani Nikula 	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
2851df0566a6SJani Nikula 
2852df0566a6SJani Nikula 	for (i = 0; i < child_device_num; i++) {
2853df0566a6SJani Nikula 		child = child_device_ptr(defs, i);
2854df0566a6SJani Nikula 		if (!child->device_type)
2855df0566a6SJani Nikula 			continue;
2856df0566a6SJani Nikula 
2857dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2858e92cbf38SWambui Karuga 			    "Found VBT child device with type 0x%x\n",
2859bdeb18dbSMatt Roper 			    child->device_type);
2860bdeb18dbSMatt Roper 
28610d9ef19bSJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
28620d9ef19bSJani Nikula 		if (!devdata)
28630d9ef19bSJani Nikula 			break;
28640d9ef19bSJani Nikula 
28657371fa34SJani Nikula 		devdata->i915 = i915;
28667371fa34SJani Nikula 
2867df0566a6SJani Nikula 		/*
2868df0566a6SJani Nikula 		 * Copy as much as we know (sizeof) and is available
28690d9ef19bSJani Nikula 		 * (child_dev_size) of the child device config. Accessing the
28700d9ef19bSJani Nikula 		 * data must depend on VBT version.
2871df0566a6SJani Nikula 		 */
28720d9ef19bSJani Nikula 		memcpy(&devdata->child, child,
2873df0566a6SJani Nikula 		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
28740d9ef19bSJani Nikula 
2875a434689cSJani Nikula 		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
2876df0566a6SJani Nikula 	}
28770d9ef19bSJani Nikula 
2878a434689cSJani Nikula 	if (list_empty(&i915->display.vbt.display_devices))
2879dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2880e92cbf38SWambui Karuga 			    "no child dev is parsed from VBT\n");
2881df0566a6SJani Nikula }
2882df0566a6SJani Nikula 
2883df0566a6SJani Nikula /* Common defaults which may be overridden by VBT. */
2884df0566a6SJani Nikula static void
2885dbd440d8SJani Nikula init_vbt_defaults(struct drm_i915_private *i915)
2886df0566a6SJani Nikula {
2887a434689cSJani Nikula 	i915->display.vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
2888df0566a6SJani Nikula 
2889df0566a6SJani Nikula 	/* general features */
2890a434689cSJani Nikula 	i915->display.vbt.int_tv_support = 1;
2891a434689cSJani Nikula 	i915->display.vbt.int_crt_support = 1;
2892df0566a6SJani Nikula 
2893df0566a6SJani Nikula 	/* driver features */
2894a434689cSJani Nikula 	i915->display.vbt.int_lvds_support = 1;
2895df0566a6SJani Nikula 
2896df0566a6SJani Nikula 	/* Default to using SSC */
2897a434689cSJani Nikula 	i915->display.vbt.lvds_use_ssc = 1;
2898df0566a6SJani Nikula 	/*
2899df0566a6SJani Nikula 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
2900df0566a6SJani Nikula 	 * clock for LVDS.
2901df0566a6SJani Nikula 	 */
2902a434689cSJani Nikula 	i915->display.vbt.lvds_ssc_freq = intel_bios_ssc_frequency(i915,
2903dbd440d8SJani Nikula 								   !HAS_PCH_SPLIT(i915));
2904dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "Set default to SSC at %d kHz\n",
2905a434689cSJani Nikula 		    i915->display.vbt.lvds_ssc_freq);
2906df0566a6SJani Nikula }
2907df0566a6SJani Nikula 
29083cf05076SVille Syrjälä /* Common defaults which may be overridden by VBT. */
29093cf05076SVille Syrjälä static void
29103cf05076SVille Syrjälä init_vbt_panel_defaults(struct intel_panel *panel)
29113cf05076SVille Syrjälä {
29123cf05076SVille Syrjälä 	/* Default to having backlight */
29133cf05076SVille Syrjälä 	panel->vbt.backlight.present = true;
29143cf05076SVille Syrjälä 
29153cf05076SVille Syrjälä 	/* LFP panel data */
29163cf05076SVille Syrjälä 	panel->vbt.lvds_dither = true;
29173cf05076SVille Syrjälä }
29183cf05076SVille Syrjälä 
2919df0566a6SJani Nikula /* Defaults to initialize only if there is no VBT. */
2920df0566a6SJani Nikula static void
2921dbd440d8SJani Nikula init_vbt_missing_defaults(struct drm_i915_private *i915)
2922df0566a6SJani Nikula {
2923df0566a6SJani Nikula 	enum port port;
29249b52aa72SRodrigo Vivi 	int ports = BIT(PORT_A) | BIT(PORT_B) | BIT(PORT_C) |
29259b52aa72SRodrigo Vivi 		    BIT(PORT_D) | BIT(PORT_E) | BIT(PORT_F);
2926df0566a6SJani Nikula 
2927e20e4037SJani Nikula 	if (!HAS_DDI(i915) && !IS_CHERRYVIEW(i915))
2928e20e4037SJani Nikula 		return;
2929e20e4037SJani Nikula 
29303ae04c0cSJani Nikula 	for_each_port_masked(port, ports) {
29313162d057SJani Nikula 		struct intel_bios_encoder_data *devdata;
293251f57481SJani Nikula 		struct child_device_config *child;
2933dbd440d8SJani Nikula 		enum phy phy = intel_port_to_phy(i915, port);
2934df0566a6SJani Nikula 
2935df0566a6SJani Nikula 		/*
2936df0566a6SJani Nikula 		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
2937df0566a6SJani Nikula 		 * to detect it.
2938df0566a6SJani Nikula 		 */
2939dbd440d8SJani Nikula 		if (intel_phy_is_tc(i915, phy))
2940df0566a6SJani Nikula 			continue;
2941df0566a6SJani Nikula 
294251f57481SJani Nikula 		/* Create fake child device config */
294351f57481SJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
294451f57481SJani Nikula 		if (!devdata)
294551f57481SJani Nikula 			break;
294651f57481SJani Nikula 
29477371fa34SJani Nikula 		devdata->i915 = i915;
294851f57481SJani Nikula 		child = &devdata->child;
294951f57481SJani Nikula 
295051f57481SJani Nikula 		if (port == PORT_F)
295151f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIF;
295251f57481SJani Nikula 		else if (port == PORT_E)
295351f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIE;
295451f57481SJani Nikula 		else
295551f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIA + port;
295651f57481SJani Nikula 
295751f57481SJani Nikula 		if (port != PORT_A && port != PORT_E)
295851f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_TMDS_DVI_SIGNALING;
295951f57481SJani Nikula 
296051f57481SJani Nikula 		if (port != PORT_E)
296151f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_DISPLAYPORT_OUTPUT;
296251f57481SJani Nikula 
296351f57481SJani Nikula 		if (port == PORT_A)
296451f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_INTERNAL_CONNECTOR;
296551f57481SJani Nikula 
2966a434689cSJani Nikula 		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
296751f57481SJani Nikula 
296851f57481SJani Nikula 		drm_dbg_kms(&i915->drm,
296951f57481SJani Nikula 			    "Generating default VBT child device with type 0x04%x on port %c\n",
297051f57481SJani Nikula 			    child->device_type, port_name(port));
2971df0566a6SJani Nikula 	}
297251f57481SJani Nikula 
297351f57481SJani Nikula 	/* Bypass some minimum baseline VBT version checks */
2974a434689cSJani Nikula 	i915->display.vbt.version = 155;
2975df0566a6SJani Nikula }
2976df0566a6SJani Nikula 
2977df0566a6SJani Nikula static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
2978df0566a6SJani Nikula {
2979df0566a6SJani Nikula 	const void *_vbt = vbt;
2980df0566a6SJani Nikula 
2981df0566a6SJani Nikula 	return _vbt + vbt->bdb_offset;
2982df0566a6SJani Nikula }
2983df0566a6SJani Nikula 
2984df0566a6SJani Nikula /**
2985df0566a6SJani Nikula  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
2986df0566a6SJani Nikula  * @buf:	pointer to a buffer to validate
2987df0566a6SJani Nikula  * @size:	size of the buffer
2988df0566a6SJani Nikula  *
2989df0566a6SJani Nikula  * Returns true on valid VBT.
2990df0566a6SJani Nikula  */
2991df0566a6SJani Nikula bool intel_bios_is_valid_vbt(const void *buf, size_t size)
2992df0566a6SJani Nikula {
2993df0566a6SJani Nikula 	const struct vbt_header *vbt = buf;
2994df0566a6SJani Nikula 	const struct bdb_header *bdb;
2995df0566a6SJani Nikula 
2996df0566a6SJani Nikula 	if (!vbt)
2997df0566a6SJani Nikula 		return false;
2998df0566a6SJani Nikula 
2999df0566a6SJani Nikula 	if (sizeof(struct vbt_header) > size) {
3000df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
3001df0566a6SJani Nikula 		return false;
3002df0566a6SJani Nikula 	}
3003df0566a6SJani Nikula 
3004df0566a6SJani Nikula 	if (memcmp(vbt->signature, "$VBT", 4)) {
3005df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("VBT invalid signature\n");
3006df0566a6SJani Nikula 		return false;
3007df0566a6SJani Nikula 	}
3008df0566a6SJani Nikula 
3009ff00ff96SLucas De Marchi 	if (vbt->vbt_size > size) {
3010ff00ff96SLucas De Marchi 		DRM_DEBUG_DRIVER("VBT incomplete (vbt_size overflows)\n");
3011ff00ff96SLucas De Marchi 		return false;
3012ff00ff96SLucas De Marchi 	}
3013ff00ff96SLucas De Marchi 
3014ff00ff96SLucas De Marchi 	size = vbt->vbt_size;
3015ff00ff96SLucas De Marchi 
3016df0566a6SJani Nikula 	if (range_overflows_t(size_t,
3017df0566a6SJani Nikula 			      vbt->bdb_offset,
3018df0566a6SJani Nikula 			      sizeof(struct bdb_header),
3019df0566a6SJani Nikula 			      size)) {
3020df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("BDB header incomplete\n");
3021df0566a6SJani Nikula 		return false;
3022df0566a6SJani Nikula 	}
3023df0566a6SJani Nikula 
3024df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
3025df0566a6SJani Nikula 	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
3026df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("BDB incomplete\n");
3027df0566a6SJani Nikula 		return false;
3028df0566a6SJani Nikula 	}
3029df0566a6SJani Nikula 
3030df0566a6SJani Nikula 	return vbt;
3031df0566a6SJani Nikula }
3032df0566a6SJani Nikula 
3033a36e7dc0SClint Taylor static struct vbt_header *spi_oprom_get_vbt(struct drm_i915_private *i915)
3034a36e7dc0SClint Taylor {
3035a36e7dc0SClint Taylor 	u32 count, data, found, store = 0;
3036a36e7dc0SClint Taylor 	u32 static_region, oprom_offset;
3037a36e7dc0SClint Taylor 	u32 oprom_size = 0x200000;
3038a36e7dc0SClint Taylor 	u16 vbt_size;
3039a36e7dc0SClint Taylor 	u32 *vbt;
3040a36e7dc0SClint Taylor 
3041a36e7dc0SClint Taylor 	static_region = intel_uncore_read(&i915->uncore, SPI_STATIC_REGIONS);
3042a36e7dc0SClint Taylor 	static_region &= OPTIONROM_SPI_REGIONID_MASK;
3043a36e7dc0SClint Taylor 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_REGIONID, static_region);
3044a36e7dc0SClint Taylor 
3045a36e7dc0SClint Taylor 	oprom_offset = intel_uncore_read(&i915->uncore, OROM_OFFSET);
3046a36e7dc0SClint Taylor 	oprom_offset &= OROM_OFFSET_MASK;
3047a36e7dc0SClint Taylor 
3048a36e7dc0SClint Taylor 	for (count = 0; count < oprom_size; count += 4) {
3049a36e7dc0SClint Taylor 		intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, oprom_offset + count);
3050a36e7dc0SClint Taylor 		data = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
3051a36e7dc0SClint Taylor 
3052a36e7dc0SClint Taylor 		if (data == *((const u32 *)"$VBT")) {
3053a36e7dc0SClint Taylor 			found = oprom_offset + count;
3054a36e7dc0SClint Taylor 			break;
3055a36e7dc0SClint Taylor 		}
3056a36e7dc0SClint Taylor 	}
3057a36e7dc0SClint Taylor 
3058a36e7dc0SClint Taylor 	if (count >= oprom_size)
3059a36e7dc0SClint Taylor 		goto err_not_found;
3060a36e7dc0SClint Taylor 
3061a36e7dc0SClint Taylor 	/* Get VBT size and allocate space for the VBT */
3062a36e7dc0SClint Taylor 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, found +
3063a36e7dc0SClint Taylor 		   offsetof(struct vbt_header, vbt_size));
3064a36e7dc0SClint Taylor 	vbt_size = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
3065a36e7dc0SClint Taylor 	vbt_size &= 0xffff;
3066a36e7dc0SClint Taylor 
3067980f42e7SJani Nikula 	vbt = kzalloc(round_up(vbt_size, 4), GFP_KERNEL);
3068a36e7dc0SClint Taylor 	if (!vbt)
3069a36e7dc0SClint Taylor 		goto err_not_found;
3070a36e7dc0SClint Taylor 
3071a36e7dc0SClint Taylor 	for (count = 0; count < vbt_size; count += 4) {
3072a36e7dc0SClint Taylor 		intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, found + count);
3073a36e7dc0SClint Taylor 		data = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
3074a36e7dc0SClint Taylor 		*(vbt + store++) = data;
3075a36e7dc0SClint Taylor 	}
3076a36e7dc0SClint Taylor 
3077a36e7dc0SClint Taylor 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
3078a36e7dc0SClint Taylor 		goto err_free_vbt;
3079a36e7dc0SClint Taylor 
3080a36e7dc0SClint Taylor 	drm_dbg_kms(&i915->drm, "Found valid VBT in SPI flash\n");
3081a36e7dc0SClint Taylor 
3082a36e7dc0SClint Taylor 	return (struct vbt_header *)vbt;
3083a36e7dc0SClint Taylor 
3084a36e7dc0SClint Taylor err_free_vbt:
3085a36e7dc0SClint Taylor 	kfree(vbt);
3086a36e7dc0SClint Taylor err_not_found:
3087a36e7dc0SClint Taylor 	return NULL;
3088a36e7dc0SClint Taylor }
3089a36e7dc0SClint Taylor 
3090dbd440d8SJani Nikula static struct vbt_header *oprom_get_vbt(struct drm_i915_private *i915)
3091df0566a6SJani Nikula {
3092dbd440d8SJani Nikula 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
30932cded152SLucas De Marchi 	void __iomem *p = NULL, *oprom;
3094fd0186ceSLucas De Marchi 	struct vbt_header *vbt;
3095fd0186ceSLucas De Marchi 	u16 vbt_size;
30962cded152SLucas De Marchi 	size_t i, size;
30972cded152SLucas De Marchi 
30982cded152SLucas De Marchi 	oprom = pci_map_rom(pdev, &size);
30992cded152SLucas De Marchi 	if (!oprom)
31002cded152SLucas De Marchi 		return NULL;
3101df0566a6SJani Nikula 
3102df0566a6SJani Nikula 	/* Scour memory looking for the VBT signature. */
310398cf5c9aSLucas De Marchi 	for (i = 0; i + 4 < size; i += 4) {
3104496f50a6SLucas De Marchi 		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
3105df0566a6SJani Nikula 			continue;
3106df0566a6SJani Nikula 
3107fd0186ceSLucas De Marchi 		p = oprom + i;
3108fd0186ceSLucas De Marchi 		size -= i;
3109df0566a6SJani Nikula 		break;
3110df0566a6SJani Nikula 	}
3111df0566a6SJani Nikula 
3112fd0186ceSLucas De Marchi 	if (!p)
31132cded152SLucas De Marchi 		goto err_unmap_oprom;
3114fd0186ceSLucas De Marchi 
3115fd0186ceSLucas De Marchi 	if (sizeof(struct vbt_header) > size) {
3116dbd440d8SJani Nikula 		drm_dbg(&i915->drm, "VBT header incomplete\n");
31172cded152SLucas De Marchi 		goto err_unmap_oprom;
3118fd0186ceSLucas De Marchi 	}
3119fd0186ceSLucas De Marchi 
3120fd0186ceSLucas De Marchi 	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
3121fd0186ceSLucas De Marchi 	if (vbt_size > size) {
3122dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
3123e92cbf38SWambui Karuga 			"VBT incomplete (vbt_size overflows)\n");
31242cded152SLucas De Marchi 		goto err_unmap_oprom;
3125fd0186ceSLucas De Marchi 	}
3126fd0186ceSLucas De Marchi 
3127fd0186ceSLucas De Marchi 	/* The rest will be validated by intel_bios_is_valid_vbt() */
3128fd0186ceSLucas De Marchi 	vbt = kmalloc(vbt_size, GFP_KERNEL);
3129fd0186ceSLucas De Marchi 	if (!vbt)
31302cded152SLucas De Marchi 		goto err_unmap_oprom;
3131fd0186ceSLucas De Marchi 
3132fd0186ceSLucas De Marchi 	memcpy_fromio(vbt, p, vbt_size);
3133fd0186ceSLucas De Marchi 
3134fd0186ceSLucas De Marchi 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
3135fd0186ceSLucas De Marchi 		goto err_free_vbt;
3136fd0186ceSLucas De Marchi 
31372cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
31382cded152SLucas De Marchi 
3139a36e7dc0SClint Taylor 	drm_dbg_kms(&i915->drm, "Found valid VBT in PCI ROM\n");
3140a36e7dc0SClint Taylor 
3141fd0186ceSLucas De Marchi 	return vbt;
3142fd0186ceSLucas De Marchi 
3143fd0186ceSLucas De Marchi err_free_vbt:
3144fd0186ceSLucas De Marchi 	kfree(vbt);
31452cded152SLucas De Marchi err_unmap_oprom:
31462cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
3147fd0186ceSLucas De Marchi 
3148df0566a6SJani Nikula 	return NULL;
3149df0566a6SJani Nikula }
3150df0566a6SJani Nikula 
3151df0566a6SJani Nikula /**
3152df0566a6SJani Nikula  * intel_bios_init - find VBT and initialize settings from the BIOS
3153dbd440d8SJani Nikula  * @i915: i915 device instance
3154df0566a6SJani Nikula  *
3155df0566a6SJani Nikula  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
3156df0566a6SJani Nikula  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
3157df0566a6SJani Nikula  * initialize some defaults if the VBT is not present at all.
3158df0566a6SJani Nikula  */
3159dbd440d8SJani Nikula void intel_bios_init(struct drm_i915_private *i915)
3160df0566a6SJani Nikula {
31617249dfcbSJani Nikula 	const struct vbt_header *vbt = i915->display.opregion.vbt;
31622cded152SLucas De Marchi 	struct vbt_header *oprom_vbt = NULL;
3163df0566a6SJani Nikula 	const struct bdb_header *bdb;
3164df0566a6SJani Nikula 
3165a434689cSJani Nikula 	INIT_LIST_HEAD(&i915->display.vbt.display_devices);
3166a434689cSJani Nikula 	INIT_LIST_HEAD(&i915->display.vbt.bdb_blocks);
31670d9ef19bSJani Nikula 
3168dbd440d8SJani Nikula 	if (!HAS_DISPLAY(i915)) {
3169dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
3170e92cbf38SWambui Karuga 			    "Skipping VBT init due to disabled display.\n");
3171df0566a6SJani Nikula 		return;
3172df0566a6SJani Nikula 	}
3173df0566a6SJani Nikula 
3174dbd440d8SJani Nikula 	init_vbt_defaults(i915);
3175df0566a6SJani Nikula 
3176a36e7dc0SClint Taylor 	/*
3177a36e7dc0SClint Taylor 	 * If the OpRegion does not have VBT, look in SPI flash through MMIO or
3178a36e7dc0SClint Taylor 	 * PCI mapping
3179a36e7dc0SClint Taylor 	 */
3180a36e7dc0SClint Taylor 	if (!vbt && IS_DGFX(i915)) {
3181a36e7dc0SClint Taylor 		oprom_vbt = spi_oprom_get_vbt(i915);
3182a36e7dc0SClint Taylor 		vbt = oprom_vbt;
3183a36e7dc0SClint Taylor 	}
3184a36e7dc0SClint Taylor 
3185df0566a6SJani Nikula 	if (!vbt) {
3186dbd440d8SJani Nikula 		oprom_vbt = oprom_get_vbt(i915);
31872cded152SLucas De Marchi 		vbt = oprom_vbt;
3188df0566a6SJani Nikula 	}
3189df0566a6SJani Nikula 
3190a36e7dc0SClint Taylor 	if (!vbt)
3191a36e7dc0SClint Taylor 		goto out;
3192a36e7dc0SClint Taylor 
3193df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
3194a434689cSJani Nikula 	i915->display.vbt.version = bdb->version;
3195df0566a6SJani Nikula 
3196dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
3197e92cbf38SWambui Karuga 		    "VBT signature \"%.*s\", BDB version %d\n",
3198a434689cSJani Nikula 		    (int)sizeof(vbt->signature), vbt->signature, i915->display.vbt.version);
3199df0566a6SJani Nikula 
3200e163cfb4SVille Syrjälä 	init_bdb_blocks(i915, bdb);
3201e163cfb4SVille Syrjälä 
3202df0566a6SJani Nikula 	/* Grab useful general definitions */
3203e163cfb4SVille Syrjälä 	parse_general_features(i915);
3204e163cfb4SVille Syrjälä 	parse_general_definitions(i915);
3205e163cfb4SVille Syrjälä 	parse_driver_features(i915);
3206df0566a6SJani Nikula 
32076e0d46e9SJani Nikula 	/* Depends on child device list */
3208e163cfb4SVille Syrjälä 	parse_compression_parameters(i915);
32096e0d46e9SJani Nikula 
3210df0566a6SJani Nikula out:
3211df0566a6SJani Nikula 	if (!vbt) {
3212dbd440d8SJani Nikula 		drm_info(&i915->drm,
3213e92cbf38SWambui Karuga 			 "Failed to find VBIOS tables (VBT)\n");
3214dbd440d8SJani Nikula 		init_vbt_missing_defaults(i915);
3215df0566a6SJani Nikula 	}
3216df0566a6SJani Nikula 
321751f57481SJani Nikula 	/* Further processing on pre-parsed or generated child device data */
321851f57481SJani Nikula 	parse_sdvo_device_mapping(i915);
321951f57481SJani Nikula 	parse_ddi_ports(i915);
322051f57481SJani Nikula 
32212cded152SLucas De Marchi 	kfree(oprom_vbt);
3222df0566a6SJani Nikula }
3223df0566a6SJani Nikula 
32243f9ffce5SVille Syrjälä static void intel_bios_init_panel(struct drm_i915_private *i915,
3225c518a775SVille Syrjälä 				  struct intel_panel *panel,
32266434cf63SAnimesh Manna 				  const struct intel_bios_encoder_data *devdata,
3227c36225a1SJani Nikula 				  const struct drm_edid *drm_edid,
32283f9ffce5SVille Syrjälä 				  bool use_fallback)
3229c2fdb424SVille Syrjälä {
32303f9ffce5SVille Syrjälä 	/* already have it? */
32313f9ffce5SVille Syrjälä 	if (panel->vbt.panel_type >= 0) {
32323f9ffce5SVille Syrjälä 		drm_WARN_ON(&i915->drm, !use_fallback);
32333f9ffce5SVille Syrjälä 		return;
32343f9ffce5SVille Syrjälä 	}
32353cf05076SVille Syrjälä 
32363f9ffce5SVille Syrjälä 	panel->vbt.panel_type = get_panel_type(i915, devdata,
3237c36225a1SJani Nikula 					       drm_edid, use_fallback);
32383f9ffce5SVille Syrjälä 	if (panel->vbt.panel_type < 0) {
32393f9ffce5SVille Syrjälä 		drm_WARN_ON(&i915->drm, use_fallback);
32403f9ffce5SVille Syrjälä 		return;
32413f9ffce5SVille Syrjälä 	}
32423f9ffce5SVille Syrjälä 
32433f9ffce5SVille Syrjälä 	init_vbt_panel_defaults(panel);
32440256ea13SVille Syrjälä 
32450256ea13SVille Syrjälä 	parse_panel_options(i915, panel);
32463cf05076SVille Syrjälä 	parse_generic_dtd(i915, panel);
32473cf05076SVille Syrjälä 	parse_lfp_data(i915, panel);
32483cf05076SVille Syrjälä 	parse_lfp_backlight(i915, panel);
32493cf05076SVille Syrjälä 	parse_sdvo_panel_data(i915, panel);
32503cf05076SVille Syrjälä 	parse_panel_driver_features(i915, panel);
32513cf05076SVille Syrjälä 	parse_power_conservation_features(i915, panel);
32523cf05076SVille Syrjälä 	parse_edp(i915, panel);
32533cf05076SVille Syrjälä 	parse_psr(i915, panel);
32543cf05076SVille Syrjälä 	parse_mipi_config(i915, panel);
32553cf05076SVille Syrjälä 	parse_mipi_sequence(i915, panel);
3256c2fdb424SVille Syrjälä }
3257c2fdb424SVille Syrjälä 
32583f9ffce5SVille Syrjälä void intel_bios_init_panel_early(struct drm_i915_private *i915,
32593f9ffce5SVille Syrjälä 				 struct intel_panel *panel,
32603f9ffce5SVille Syrjälä 				 const struct intel_bios_encoder_data *devdata)
32613f9ffce5SVille Syrjälä {
32623f9ffce5SVille Syrjälä 	intel_bios_init_panel(i915, panel, devdata, NULL, false);
32633f9ffce5SVille Syrjälä }
32643f9ffce5SVille Syrjälä 
32653f9ffce5SVille Syrjälä void intel_bios_init_panel_late(struct drm_i915_private *i915,
32663f9ffce5SVille Syrjälä 				struct intel_panel *panel,
32673f9ffce5SVille Syrjälä 				const struct intel_bios_encoder_data *devdata,
3268c36225a1SJani Nikula 				const struct drm_edid *drm_edid)
32693f9ffce5SVille Syrjälä {
3270c36225a1SJani Nikula 	intel_bios_init_panel(i915, panel, devdata, drm_edid, true);
32713f9ffce5SVille Syrjälä }
32723f9ffce5SVille Syrjälä 
3273df0566a6SJani Nikula /**
327478dae1acSJanusz Krzysztofik  * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
3275dbd440d8SJani Nikula  * @i915: i915 device instance
3276df0566a6SJani Nikula  */
3277dbd440d8SJani Nikula void intel_bios_driver_remove(struct drm_i915_private *i915)
3278df0566a6SJani Nikula {
3279e163cfb4SVille Syrjälä 	struct intel_bios_encoder_data *devdata, *nd;
3280e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry, *ne;
32810d9ef19bSJani Nikula 
3282a434689cSJani Nikula 	list_for_each_entry_safe(devdata, nd, &i915->display.vbt.display_devices, node) {
32830d9ef19bSJani Nikula 		list_del(&devdata->node);
32846e0d46e9SJani Nikula 		kfree(devdata->dsc);
32850d9ef19bSJani Nikula 		kfree(devdata);
32860d9ef19bSJani Nikula 	}
32870d9ef19bSJani Nikula 
3288a434689cSJani Nikula 	list_for_each_entry_safe(entry, ne, &i915->display.vbt.bdb_blocks, node) {
3289e163cfb4SVille Syrjälä 		list_del(&entry->node);
3290e163cfb4SVille Syrjälä 		kfree(entry);
3291e163cfb4SVille Syrjälä 	}
32923cf05076SVille Syrjälä }
3293e163cfb4SVille Syrjälä 
32943cf05076SVille Syrjälä void intel_bios_fini_panel(struct intel_panel *panel)
32953cf05076SVille Syrjälä {
32963cf05076SVille Syrjälä 	kfree(panel->vbt.sdvo_lvds_vbt_mode);
32973cf05076SVille Syrjälä 	panel->vbt.sdvo_lvds_vbt_mode = NULL;
32983cf05076SVille Syrjälä 	kfree(panel->vbt.lfp_lvds_vbt_mode);
32993cf05076SVille Syrjälä 	panel->vbt.lfp_lvds_vbt_mode = NULL;
33003cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.data);
33013cf05076SVille Syrjälä 	panel->vbt.dsi.data = NULL;
33023cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.pps);
33033cf05076SVille Syrjälä 	panel->vbt.dsi.pps = NULL;
33043cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.config);
33053cf05076SVille Syrjälä 	panel->vbt.dsi.config = NULL;
33063cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.deassert_seq);
33073cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq = NULL;
3308df0566a6SJani Nikula }
3309df0566a6SJani Nikula 
3310df0566a6SJani Nikula /**
3311df0566a6SJani Nikula  * intel_bios_is_tv_present - is integrated TV present in VBT
3312dbd440d8SJani Nikula  * @i915: i915 device instance
3313df0566a6SJani Nikula  *
3314df0566a6SJani Nikula  * Return true if TV is present. If no child devices were parsed from VBT,
3315df0566a6SJani Nikula  * assume TV is present.
3316df0566a6SJani Nikula  */
3317dbd440d8SJani Nikula bool intel_bios_is_tv_present(struct drm_i915_private *i915)
3318df0566a6SJani Nikula {
33193162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3320df0566a6SJani Nikula 
3321a434689cSJani Nikula 	if (!i915->display.vbt.int_tv_support)
3322df0566a6SJani Nikula 		return false;
3323df0566a6SJani Nikula 
3324a434689cSJani Nikula 	if (list_empty(&i915->display.vbt.display_devices))
3325df0566a6SJani Nikula 		return true;
3326df0566a6SJani Nikula 
3327a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3328d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
33290d9ef19bSJani Nikula 
3330df0566a6SJani Nikula 		/*
3331df0566a6SJani Nikula 		 * If the device type is not TV, continue.
3332df0566a6SJani Nikula 		 */
3333df0566a6SJani Nikula 		switch (child->device_type) {
3334df0566a6SJani Nikula 		case DEVICE_TYPE_INT_TV:
3335df0566a6SJani Nikula 		case DEVICE_TYPE_TV:
3336df0566a6SJani Nikula 		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
3337df0566a6SJani Nikula 			break;
3338df0566a6SJani Nikula 		default:
3339df0566a6SJani Nikula 			continue;
3340df0566a6SJani Nikula 		}
3341df0566a6SJani Nikula 		/* Only when the addin_offset is non-zero, it is regarded
3342df0566a6SJani Nikula 		 * as present.
3343df0566a6SJani Nikula 		 */
3344df0566a6SJani Nikula 		if (child->addin_offset)
3345df0566a6SJani Nikula 			return true;
3346df0566a6SJani Nikula 	}
3347df0566a6SJani Nikula 
3348df0566a6SJani Nikula 	return false;
3349df0566a6SJani Nikula }
3350df0566a6SJani Nikula 
3351df0566a6SJani Nikula /**
3352df0566a6SJani Nikula  * intel_bios_is_lvds_present - is LVDS present in VBT
3353dbd440d8SJani Nikula  * @i915:	i915 device instance
3354df0566a6SJani Nikula  * @i2c_pin:	i2c pin for LVDS if present
3355df0566a6SJani Nikula  *
3356df0566a6SJani Nikula  * Return true if LVDS is present. If no child devices were parsed from VBT,
3357df0566a6SJani Nikula  * assume LVDS is present.
3358df0566a6SJani Nikula  */
3359dbd440d8SJani Nikula bool intel_bios_is_lvds_present(struct drm_i915_private *i915, u8 *i2c_pin)
3360df0566a6SJani Nikula {
33613162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3362df0566a6SJani Nikula 
3363a434689cSJani Nikula 	if (list_empty(&i915->display.vbt.display_devices))
3364df0566a6SJani Nikula 		return true;
3365df0566a6SJani Nikula 
3366a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3367d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3368df0566a6SJani Nikula 
3369df0566a6SJani Nikula 		/* If the device type is not LFP, continue.
3370df0566a6SJani Nikula 		 * We have to check both the new identifiers as well as the
3371df0566a6SJani Nikula 		 * old for compatibility with some BIOSes.
3372df0566a6SJani Nikula 		 */
3373df0566a6SJani Nikula 		if (child->device_type != DEVICE_TYPE_INT_LFP &&
3374df0566a6SJani Nikula 		    child->device_type != DEVICE_TYPE_LFP)
3375df0566a6SJani Nikula 			continue;
3376df0566a6SJani Nikula 
3377dbd440d8SJani Nikula 		if (intel_gmbus_is_valid_pin(i915, child->i2c_pin))
3378df0566a6SJani Nikula 			*i2c_pin = child->i2c_pin;
3379df0566a6SJani Nikula 
3380df0566a6SJani Nikula 		/* However, we cannot trust the BIOS writers to populate
3381df0566a6SJani Nikula 		 * the VBT correctly.  Since LVDS requires additional
3382df0566a6SJani Nikula 		 * information from AIM blocks, a non-zero addin offset is
3383df0566a6SJani Nikula 		 * a good indicator that the LVDS is actually present.
3384df0566a6SJani Nikula 		 */
3385df0566a6SJani Nikula 		if (child->addin_offset)
3386df0566a6SJani Nikula 			return true;
3387df0566a6SJani Nikula 
3388df0566a6SJani Nikula 		/* But even then some BIOS writers perform some black magic
3389df0566a6SJani Nikula 		 * and instantiate the device without reference to any
3390df0566a6SJani Nikula 		 * additional data.  Trust that if the VBT was written into
3391df0566a6SJani Nikula 		 * the OpRegion then they have validated the LVDS's existence.
3392df0566a6SJani Nikula 		 */
33937249dfcbSJani Nikula 		if (i915->display.opregion.vbt)
3394df0566a6SJani Nikula 			return true;
3395df0566a6SJani Nikula 	}
3396df0566a6SJani Nikula 
3397df0566a6SJani Nikula 	return false;
3398df0566a6SJani Nikula }
3399df0566a6SJani Nikula 
3400df0566a6SJani Nikula /**
3401df0566a6SJani Nikula  * intel_bios_is_port_present - is the specified digital port present
3402dbd440d8SJani Nikula  * @i915:	i915 device instance
3403df0566a6SJani Nikula  * @port:	port to check
3404df0566a6SJani Nikula  *
3405df0566a6SJani Nikula  * Return true if the device in %port is present.
3406df0566a6SJani Nikula  */
3407dbd440d8SJani Nikula bool intel_bios_is_port_present(struct drm_i915_private *i915, enum port port)
3408df0566a6SJani Nikula {
3409b17a15d6SVille Syrjälä 	const struct intel_bios_encoder_data *devdata;
3410b17a15d6SVille Syrjälä 
3411a868a1e5SVille Syrjälä 	if (WARN_ON(!has_ddi_port_info(i915)))
3412df0566a6SJani Nikula 		return true;
3413df0566a6SJani Nikula 
3414b17a15d6SVille Syrjälä 	if (!is_port_valid(i915, port))
3415b17a15d6SVille Syrjälä 		return false;
3416b17a15d6SVille Syrjälä 
3417b17a15d6SVille Syrjälä 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3418b17a15d6SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3419b17a15d6SVille Syrjälä 
3420b17a15d6SVille Syrjälä 		if (dvo_port_to_port(i915, child->dvo_port) == port)
3421b17a15d6SVille Syrjälä 			return true;
3422b17a15d6SVille Syrjälä 	}
3423b17a15d6SVille Syrjälä 
3424b17a15d6SVille Syrjälä 	return false;
3425df0566a6SJani Nikula }
3426df0566a6SJani Nikula 
3427044cbc7aSVille Syrjälä static bool intel_bios_encoder_supports_dp_dual_mode(const struct intel_bios_encoder_data *devdata)
342832c2bc89SVille Syrjälä {
3429044cbc7aSVille Syrjälä 	const struct child_device_config *child = &devdata->child;
3430044cbc7aSVille Syrjälä 
3431044cbc7aSVille Syrjälä 	if (!intel_bios_encoder_supports_dp(devdata) ||
3432044cbc7aSVille Syrjälä 	    !intel_bios_encoder_supports_hdmi(devdata))
343332c2bc89SVille Syrjälä 		return false;
343432c2bc89SVille Syrjälä 
343532c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_DPA)
343632c2bc89SVille Syrjälä 		return true;
343732c2bc89SVille Syrjälä 
343832c2bc89SVille Syrjälä 	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
343932c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_HDMIA &&
344032c2bc89SVille Syrjälä 	    child->aux_channel != 0)
344132c2bc89SVille Syrjälä 		return true;
344232c2bc89SVille Syrjälä 
344332c2bc89SVille Syrjälä 	return false;
344432c2bc89SVille Syrjälä }
344532c2bc89SVille Syrjälä 
344632c2bc89SVille Syrjälä bool intel_bios_is_port_dp_dual_mode(struct drm_i915_private *i915,
3447df0566a6SJani Nikula 				     enum port port)
3448df0566a6SJani Nikula {
3449a868a1e5SVille Syrjälä 	const struct intel_bios_encoder_data *devdata =
3450a868a1e5SVille Syrjälä 		intel_bios_encoder_data_lookup(i915, port);
345132c2bc89SVille Syrjälä 
3452044cbc7aSVille Syrjälä 	return devdata && intel_bios_encoder_supports_dp_dual_mode(devdata);
345332c2bc89SVille Syrjälä }
3454df0566a6SJani Nikula 
3455df0566a6SJani Nikula /**
3456df0566a6SJani Nikula  * intel_bios_is_dsi_present - is DSI present in VBT
3457dbd440d8SJani Nikula  * @i915:	i915 device instance
3458df0566a6SJani Nikula  * @port:	port for DSI if present
3459df0566a6SJani Nikula  *
3460df0566a6SJani Nikula  * Return true if DSI is present, and return the port in %port.
3461df0566a6SJani Nikula  */
3462dbd440d8SJani Nikula bool intel_bios_is_dsi_present(struct drm_i915_private *i915,
3463df0566a6SJani Nikula 			       enum port *port)
3464df0566a6SJani Nikula {
34653162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3466df0566a6SJani Nikula 
3467a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3468d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3469d24b3475SVille Syrjälä 		u8 dvo_port = child->dvo_port;
3470df0566a6SJani Nikula 
3471df0566a6SJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
3472df0566a6SJani Nikula 			continue;
3473df0566a6SJani Nikula 
3474118b5c13SVille Syrjälä 		if (dsi_dvo_port_to_port(i915, dvo_port) == PORT_NONE) {
3475dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
3476e92cbf38SWambui Karuga 				    "VBT has unsupported DSI port %c\n",
3477df0566a6SJani Nikula 				    port_name(dvo_port - DVO_PORT_MIPIA));
3478118b5c13SVille Syrjälä 			continue;
3479df0566a6SJani Nikula 		}
3480118b5c13SVille Syrjälä 
3481118b5c13SVille Syrjälä 		if (port)
3482118b5c13SVille Syrjälä 			*port = dsi_dvo_port_to_port(i915, dvo_port);
3483118b5c13SVille Syrjälä 		return true;
3484df0566a6SJani Nikula 	}
3485df0566a6SJani Nikula 
3486df0566a6SJani Nikula 	return false;
3487df0566a6SJani Nikula }
3488df0566a6SJani Nikula 
34891bf2f3bfSJani Nikula static void fill_dsc(struct intel_crtc_state *crtc_state,
34901bf2f3bfSJani Nikula 		     struct dsc_compression_parameters_entry *dsc,
34911bf2f3bfSJani Nikula 		     int dsc_max_bpc)
34921bf2f3bfSJani Nikula {
34931bf2f3bfSJani Nikula 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
34941bf2f3bfSJani Nikula 	int bpc = 8;
34951bf2f3bfSJani Nikula 
34961bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_major = dsc->version_major;
34971bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_minor = dsc->version_minor;
34981bf2f3bfSJani Nikula 
34991bf2f3bfSJani Nikula 	if (dsc->support_12bpc && dsc_max_bpc >= 12)
35001bf2f3bfSJani Nikula 		bpc = 12;
35011bf2f3bfSJani Nikula 	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
35021bf2f3bfSJani Nikula 		bpc = 10;
35031bf2f3bfSJani Nikula 	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
35041bf2f3bfSJani Nikula 		bpc = 8;
35051bf2f3bfSJani Nikula 	else
35061bf2f3bfSJani Nikula 		DRM_DEBUG_KMS("VBT: Unsupported BPC %d for DCS\n",
35071bf2f3bfSJani Nikula 			      dsc_max_bpc);
35081bf2f3bfSJani Nikula 
35091bf2f3bfSJani Nikula 	crtc_state->pipe_bpp = bpc * 3;
35101bf2f3bfSJani Nikula 
35111bf2f3bfSJani Nikula 	crtc_state->dsc.compressed_bpp = min(crtc_state->pipe_bpp,
35121bf2f3bfSJani Nikula 					     VBT_DSC_MAX_BPP(dsc->max_bpp));
35131bf2f3bfSJani Nikula 
35141bf2f3bfSJani Nikula 	/*
35151bf2f3bfSJani Nikula 	 * FIXME: This is ugly, and slice count should take DSC engine
35161bf2f3bfSJani Nikula 	 * throughput etc. into account.
35171bf2f3bfSJani Nikula 	 *
35181bf2f3bfSJani Nikula 	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
35191bf2f3bfSJani Nikula 	 */
35201bf2f3bfSJani Nikula 	if (dsc->slices_per_line & BIT(2)) {
35211bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 4;
35221bf2f3bfSJani Nikula 	} else if (dsc->slices_per_line & BIT(1)) {
35231bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 2;
35241bf2f3bfSJani Nikula 	} else {
35251bf2f3bfSJani Nikula 		/* FIXME */
35261bf2f3bfSJani Nikula 		if (!(dsc->slices_per_line & BIT(0)))
35271bf2f3bfSJani Nikula 			DRM_DEBUG_KMS("VBT: Unsupported DSC slice count for DSI\n");
35281bf2f3bfSJani Nikula 
35291bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 1;
35301bf2f3bfSJani Nikula 	}
35311bf2f3bfSJani Nikula 
35321bf2f3bfSJani Nikula 	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
35331bf2f3bfSJani Nikula 	    crtc_state->dsc.slice_count != 0)
35341bf2f3bfSJani Nikula 		DRM_DEBUG_KMS("VBT: DSC hdisplay %d not divisible by slice count %d\n",
35351bf2f3bfSJani Nikula 			      crtc_state->hw.adjusted_mode.crtc_hdisplay,
35361bf2f3bfSJani Nikula 			      crtc_state->dsc.slice_count);
35371bf2f3bfSJani Nikula 
35381bf2f3bfSJani Nikula 	/*
35391bf2f3bfSJani Nikula 	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
3540fd8a5b27SJani Nikula 	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63.
35411bf2f3bfSJani Nikula 	 */
3542fd8a5b27SJani Nikula 	vdsc_cfg->rc_model_size = drm_dsc_dp_rc_buffer_size(dsc->rc_buffer_block_size,
3543fd8a5b27SJani Nikula 							    dsc->rc_buffer_size);
35441bf2f3bfSJani Nikula 
35451bf2f3bfSJani Nikula 	/* FIXME: DSI spec says bpc + 1 for this one */
35461bf2f3bfSJani Nikula 	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
35471bf2f3bfSJani Nikula 
35481bf2f3bfSJani Nikula 	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
35491bf2f3bfSJani Nikula 
35501bf2f3bfSJani Nikula 	vdsc_cfg->slice_height = dsc->slice_height;
35511bf2f3bfSJani Nikula }
35521bf2f3bfSJani Nikula 
35531bf2f3bfSJani Nikula /* FIXME: initially DSI specific */
35541bf2f3bfSJani Nikula bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
35551bf2f3bfSJani Nikula 			       struct intel_crtc_state *crtc_state,
35561bf2f3bfSJani Nikula 			       int dsc_max_bpc)
35571bf2f3bfSJani Nikula {
35581bf2f3bfSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
35593162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
35601bf2f3bfSJani Nikula 
3561a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3562d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
35631bf2f3bfSJani Nikula 
35641bf2f3bfSJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
35651bf2f3bfSJani Nikula 			continue;
35661bf2f3bfSJani Nikula 
3567118b5c13SVille Syrjälä 		if (dsi_dvo_port_to_port(i915, child->dvo_port) == encoder->port) {
35681bf2f3bfSJani Nikula 			if (!devdata->dsc)
35691bf2f3bfSJani Nikula 				return false;
35701bf2f3bfSJani Nikula 
35711bf2f3bfSJani Nikula 			if (crtc_state)
35721bf2f3bfSJani Nikula 				fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
35731bf2f3bfSJani Nikula 
35741bf2f3bfSJani Nikula 			return true;
35751bf2f3bfSJani Nikula 		}
35761bf2f3bfSJani Nikula 	}
35771bf2f3bfSJani Nikula 
35781bf2f3bfSJani Nikula 	return false;
35791bf2f3bfSJani Nikula }
35801bf2f3bfSJani Nikula 
3581bb45217fSVille Syrjälä static enum aux_ch map_aux_ch(struct drm_i915_private *i915, u8 aux_channel)
3582df0566a6SJani Nikula {
3583df0566a6SJani Nikula 	enum aux_ch aux_ch;
3584df0566a6SJani Nikula 
358518c283dfSAditya Swarup 	/*
358618c283dfSAditya Swarup 	 * RKL/DG1 VBT uses PHY based mapping. Combo PHYs A,B,C,D
358718c283dfSAditya Swarup 	 * map to DDI A,B,TC1,TC2 respectively.
358818c283dfSAditya Swarup 	 *
358918c283dfSAditya Swarup 	 * ADL-S VBT uses PHY based mapping. Combo PHYs A,B,C,D,E
359018c283dfSAditya Swarup 	 * map to DDI A,TC1,TC2,TC3,TC4 respectively.
359118c283dfSAditya Swarup 	 */
3592bb45217fSVille Syrjälä 	switch (aux_channel) {
3593df0566a6SJani Nikula 	case DP_AUX_A:
3594df0566a6SJani Nikula 		aux_ch = AUX_CH_A;
3595df0566a6SJani Nikula 		break;
3596df0566a6SJani Nikula 	case DP_AUX_B:
3597dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
359818c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC1;
359918c283dfSAditya Swarup 		else
3600df0566a6SJani Nikula 			aux_ch = AUX_CH_B;
3601df0566a6SJani Nikula 		break;
3602df0566a6SJani Nikula 	case DP_AUX_C:
3603dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
360418c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC2;
3605dbd440d8SJani Nikula 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
360618c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC1;
360718c283dfSAditya Swarup 		else
360818c283dfSAditya Swarup 			aux_ch = AUX_CH_C;
3609df0566a6SJani Nikula 		break;
3610df0566a6SJani Nikula 	case DP_AUX_D:
3611612dc414SImre Deak 		if (DISPLAY_VER(i915) >= 13)
3612ed2615a8SMatt Roper 			aux_ch = AUX_CH_D_XELPD;
3613ed2615a8SMatt Roper 		else if (IS_ALDERLAKE_S(i915))
361418c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC3;
3615dbd440d8SJani Nikula 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
361618c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC2;
361718c283dfSAditya Swarup 		else
361818c283dfSAditya Swarup 			aux_ch = AUX_CH_D;
3619df0566a6SJani Nikula 		break;
3620df0566a6SJani Nikula 	case DP_AUX_E:
3621612dc414SImre Deak 		if (DISPLAY_VER(i915) >= 13)
3622ed2615a8SMatt Roper 			aux_ch = AUX_CH_E_XELPD;
3623ed2615a8SMatt Roper 		else if (IS_ALDERLAKE_S(i915))
362418c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC4;
362518c283dfSAditya Swarup 		else
3626df0566a6SJani Nikula 			aux_ch = AUX_CH_E;
3627df0566a6SJani Nikula 		break;
3628df0566a6SJani Nikula 	case DP_AUX_F:
3629612dc414SImre Deak 		if (DISPLAY_VER(i915) >= 13)
3630ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC1;
3631ed2615a8SMatt Roper 		else
3632df0566a6SJani Nikula 			aux_ch = AUX_CH_F;
3633df0566a6SJani Nikula 		break;
3634eb8de23cSKhaled Almahallawy 	case DP_AUX_G:
3635612dc414SImre Deak 		if (DISPLAY_VER(i915) >= 13)
3636ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC2;
3637ed2615a8SMatt Roper 		else
3638eb8de23cSKhaled Almahallawy 			aux_ch = AUX_CH_G;
3639eb8de23cSKhaled Almahallawy 		break;
36405bf22ee4SVille Syrjälä 	case DP_AUX_H:
3641612dc414SImre Deak 		if (DISPLAY_VER(i915) >= 13)
3642ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC3;
3643ed2615a8SMatt Roper 		else
36445bf22ee4SVille Syrjälä 			aux_ch = AUX_CH_H;
36455bf22ee4SVille Syrjälä 		break;
36465bf22ee4SVille Syrjälä 	case DP_AUX_I:
3647612dc414SImre Deak 		if (DISPLAY_VER(i915) >= 13)
3648ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC4;
3649ed2615a8SMatt Roper 		else
36505bf22ee4SVille Syrjälä 			aux_ch = AUX_CH_I;
36515bf22ee4SVille Syrjälä 		break;
3652df0566a6SJani Nikula 	default:
3653bb45217fSVille Syrjälä 		MISSING_CASE(aux_channel);
3654df0566a6SJani Nikula 		aux_ch = AUX_CH_A;
3655df0566a6SJani Nikula 		break;
3656df0566a6SJani Nikula 	}
3657df0566a6SJani Nikula 
3658df0566a6SJani Nikula 	return aux_ch;
3659df0566a6SJani Nikula }
3660d9ee2111SJani Nikula 
3661bb45217fSVille Syrjälä enum aux_ch intel_bios_dp_aux_ch(const struct intel_bios_encoder_data *devdata)
3662bb45217fSVille Syrjälä {
3663bb45217fSVille Syrjälä 	if (!devdata || !devdata->child.aux_channel)
3664bb45217fSVille Syrjälä 		return AUX_CH_NONE;
3665bb45217fSVille Syrjälä 
3666bb45217fSVille Syrjälä 	return map_aux_ch(devdata->i915, devdata->child.aux_channel);
3667bb45217fSVille Syrjälä }
3668d9ee2111SJani Nikula 
366902107ef1SVille Syrjälä int intel_bios_dp_boost_level(const struct intel_bios_encoder_data *devdata)
3670605a1872SJani Nikula {
3671a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3672c0a950d1SJani Nikula 		return 0;
3673605a1872SJani Nikula 
3674c0a950d1SJani Nikula 	return translate_iboost(devdata->child.dp_iboost_level);
3675605a1872SJani Nikula }
367601a60883SJani Nikula 
367702107ef1SVille Syrjälä int intel_bios_hdmi_boost_level(const struct intel_bios_encoder_data *devdata)
367801a60883SJani Nikula {
3679a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3680c0a950d1SJani Nikula 		return 0;
368101a60883SJani Nikula 
3682c0a950d1SJani Nikula 	return translate_iboost(devdata->child.hdmi_iboost_level);
368301a60883SJani Nikula }
3684f83acdabSJani Nikula 
368502107ef1SVille Syrjälä int intel_bios_hdmi_ddc_pin(const struct intel_bios_encoder_data *devdata)
3686f83acdabSJani Nikula {
3687dab8477bSJani Nikula 	if (!devdata || !devdata->child.ddc_pin)
3688dab8477bSJani Nikula 		return 0;
3689dab8477bSJani Nikula 
369002107ef1SVille Syrjälä 	return map_ddc_pin(devdata->i915, devdata->child.ddc_pin);
369117004bfbSJani Nikula }
3692c5faae5aSJani Nikula 
3693f08fbe6aSJani Nikula bool intel_bios_encoder_supports_typec_usb(const struct intel_bios_encoder_data *devdata)
3694c5faae5aSJani Nikula {
3695a434689cSJani Nikula 	return devdata->i915->display.vbt.version >= 195 && devdata->child.dp_usb_type_c;
3696c5faae5aSJani Nikula }
3697c5faae5aSJani Nikula 
3698f08fbe6aSJani Nikula bool intel_bios_encoder_supports_tbt(const struct intel_bios_encoder_data *devdata)
3699c5faae5aSJani Nikula {
3700a434689cSJani Nikula 	return devdata->i915->display.vbt.version >= 209 && devdata->child.tbt;
3701c5faae5aSJani Nikula }
370245c0673aSJani Nikula 
37035f42196dSVille Syrjälä bool intel_bios_encoder_lane_reversal(const struct intel_bios_encoder_data *devdata)
37045f42196dSVille Syrjälä {
37055f42196dSVille Syrjälä 	return devdata && devdata->child.lane_reversal;
37065f42196dSVille Syrjälä }
37075f42196dSVille Syrjälä 
37089151c85cSVille Syrjälä bool intel_bios_encoder_hpd_invert(const struct intel_bios_encoder_data *devdata)
37099151c85cSVille Syrjälä {
37109151c85cSVille Syrjälä 	return devdata && devdata->child.hpd_invert;
37119151c85cSVille Syrjälä }
37129151c85cSVille Syrjälä 
371345c0673aSJani Nikula const struct intel_bios_encoder_data *
371445c0673aSJani Nikula intel_bios_encoder_data_lookup(struct drm_i915_private *i915, enum port port)
371545c0673aSJani Nikula {
3716a434689cSJani Nikula 	return i915->display.vbt.ports[port];
371745c0673aSJani Nikula }
3718