xref: /linux/drivers/gpu/drm/i915/display/intel_bios.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 
28d962f0afSRadhakrishna Sripada #include <linux/firmware.h>
29d962f0afSRadhakrishna Sripada 
30da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h>
312a64b147SThomas Zimmermann #include <drm/display/drm_dsc_helper.h>
32cfc10489SJani Nikula #include <drm/drm_edid.h>
3331967638SImre Deak #include <drm/drm_fixed.h>
34df0566a6SJani Nikula 
35df0566a6SJani Nikula #include "i915_drv.h"
36ce2fce25SMatt Roper #include "i915_reg.h"
37cfc10489SJani Nikula #include "intel_display.h"
38cfc10489SJani Nikula #include "intel_display_types.h"
39cfc10489SJani Nikula #include "intel_gmbus.h"
4037310936SJani Nikula #include "intel_uncore.h"
41df0566a6SJani Nikula 
42df0566a6SJani Nikula #define _INTEL_BIOS_PRIVATE
43df0566a6SJani Nikula #include "intel_vbt_defs.h"
44df0566a6SJani Nikula 
45df0566a6SJani Nikula /**
46df0566a6SJani Nikula  * DOC: Video BIOS Table (VBT)
47df0566a6SJani Nikula  *
48df0566a6SJani Nikula  * The Video BIOS Table, or VBT, provides platform and board specific
49df0566a6SJani Nikula  * configuration information to the driver that is not discoverable or available
50df0566a6SJani Nikula  * through other means. The configuration is mostly related to display
51df0566a6SJani Nikula  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
52df0566a6SJani Nikula  * the PCI ROM.
53df0566a6SJani Nikula  *
54df0566a6SJani Nikula  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
55df0566a6SJani Nikula  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
56df0566a6SJani Nikula  * contain the actual configuration information. The VBT Header, and thus the
57df0566a6SJani Nikula  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
58df0566a6SJani Nikula  * BDB Header. The data blocks are concatenated after the BDB Header. The data
59df0566a6SJani Nikula  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
60df0566a6SJani Nikula  * data. (Block 53, the MIPI Sequence Block is an exception.)
61df0566a6SJani Nikula  *
62df0566a6SJani Nikula  * The driver parses the VBT during load. The relevant information is stored in
63df0566a6SJani Nikula  * driver private data for ease of use, and the actual VBT is not read after
64df0566a6SJani Nikula  * that.
65df0566a6SJani Nikula  */
66df0566a6SJani Nikula 
670d9ef19bSJani Nikula /* Wrapper for VBT child device config */
683162d057SJani Nikula struct intel_bios_encoder_data {
699aec6f76SJani Nikula 	struct intel_display *display;
707371fa34SJani Nikula 
710d9ef19bSJani Nikula 	struct child_device_config child;
726e0d46e9SJani Nikula 	struct dsc_compression_parameters_entry *dsc;
730d9ef19bSJani Nikula 	struct list_head node;
740d9ef19bSJani Nikula };
750d9ef19bSJani Nikula 
76bc3ca4d9SEaswar Hariharan #define	TARGET_ADDR1	0x70
77bc3ca4d9SEaswar Hariharan #define	TARGET_ADDR2	0x72
78df0566a6SJani Nikula 
79df0566a6SJani Nikula /* Get BDB block size given a pointer to Block ID. */
_get_blocksize(const u8 * block_base)80df0566a6SJani Nikula static u32 _get_blocksize(const u8 *block_base)
81df0566a6SJani Nikula {
82df0566a6SJani Nikula 	/* The MIPI Sequence Block v3+ has a separate size field. */
83df0566a6SJani Nikula 	if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
84df0566a6SJani Nikula 		return *((const u32 *)(block_base + 4));
85df0566a6SJani Nikula 	else
86df0566a6SJani Nikula 		return *((const u16 *)(block_base + 1));
87df0566a6SJani Nikula }
88df0566a6SJani Nikula 
89df0566a6SJani Nikula /* Get BDB block size give a pointer to data after Block ID and Block Size. */
get_blocksize(const void * block_data)90df0566a6SJani Nikula static u32 get_blocksize(const void *block_data)
91df0566a6SJani Nikula {
92df0566a6SJani Nikula 	return _get_blocksize(block_data - 3);
93df0566a6SJani Nikula }
94df0566a6SJani Nikula 
95df0566a6SJani Nikula static const void *
find_raw_section(const void * _bdb,enum bdb_block_id section_id)96e163cfb4SVille Syrjälä find_raw_section(const void *_bdb, enum bdb_block_id section_id)
97df0566a6SJani Nikula {
98df0566a6SJani Nikula 	const struct bdb_header *bdb = _bdb;
99df0566a6SJani Nikula 	const u8 *base = _bdb;
100df0566a6SJani Nikula 	int index = 0;
101df0566a6SJani Nikula 	u32 total, current_size;
102df0566a6SJani Nikula 	enum bdb_block_id current_id;
103df0566a6SJani Nikula 
104df0566a6SJani Nikula 	/* skip to first section */
105df0566a6SJani Nikula 	index += bdb->header_size;
106df0566a6SJani Nikula 	total = bdb->bdb_size;
107df0566a6SJani Nikula 
108df0566a6SJani Nikula 	/* walk the sections looking for section_id */
109df0566a6SJani Nikula 	while (index + 3 < total) {
110df0566a6SJani Nikula 		current_id = *(base + index);
111df0566a6SJani Nikula 		current_size = _get_blocksize(base + index);
112df0566a6SJani Nikula 		index += 3;
113df0566a6SJani Nikula 
114df0566a6SJani Nikula 		if (index + current_size > total)
115df0566a6SJani Nikula 			return NULL;
116df0566a6SJani Nikula 
117df0566a6SJani Nikula 		if (current_id == section_id)
118df0566a6SJani Nikula 			return base + index;
119df0566a6SJani Nikula 
120df0566a6SJani Nikula 		index += current_size;
121df0566a6SJani Nikula 	}
122df0566a6SJani Nikula 
123df0566a6SJani Nikula 	return NULL;
124df0566a6SJani Nikula }
125df0566a6SJani Nikula 
126e163cfb4SVille Syrjälä /*
127e163cfb4SVille Syrjälä  * Offset from the start of BDB to the start of the
128e163cfb4SVille Syrjälä  * block data (just past the block header).
129e163cfb4SVille Syrjälä  */
raw_block_offset(const void * bdb,enum bdb_block_id section_id)13039b1bc4bSVille Syrjälä static u32 raw_block_offset(const void *bdb, enum bdb_block_id section_id)
131e163cfb4SVille Syrjälä {
132e163cfb4SVille Syrjälä 	const void *block;
133e163cfb4SVille Syrjälä 
134e163cfb4SVille Syrjälä 	block = find_raw_section(bdb, section_id);
135e163cfb4SVille Syrjälä 	if (!block)
136e163cfb4SVille Syrjälä 		return 0;
137e163cfb4SVille Syrjälä 
138e163cfb4SVille Syrjälä 	return block - bdb;
139e163cfb4SVille Syrjälä }
140e163cfb4SVille Syrjälä 
141e163cfb4SVille Syrjälä struct bdb_block_entry {
142e163cfb4SVille Syrjälä 	struct list_head node;
143e163cfb4SVille Syrjälä 	enum bdb_block_id section_id;
144e163cfb4SVille Syrjälä 	u8 data[];
145e163cfb4SVille Syrjälä };
146e163cfb4SVille Syrjälä 
147e163cfb4SVille Syrjälä static const void *
bdb_find_section(struct intel_display * display,enum bdb_block_id section_id)1489aec6f76SJani Nikula bdb_find_section(struct intel_display *display,
149e163cfb4SVille Syrjälä 		 enum bdb_block_id section_id)
150e163cfb4SVille Syrjälä {
151e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry;
152e163cfb4SVille Syrjälä 
1539aec6f76SJani Nikula 	list_for_each_entry(entry, &display->vbt.bdb_blocks, node) {
154e163cfb4SVille Syrjälä 		if (entry->section_id == section_id)
155e163cfb4SVille Syrjälä 			return entry->data + 3;
156e163cfb4SVille Syrjälä 	}
157e163cfb4SVille Syrjälä 
158e163cfb4SVille Syrjälä 	return NULL;
159e163cfb4SVille Syrjälä }
160e163cfb4SVille Syrjälä 
161e163cfb4SVille Syrjälä static const struct {
162e163cfb4SVille Syrjälä 	enum bdb_block_id section_id;
163e163cfb4SVille Syrjälä 	size_t min_size;
164e163cfb4SVille Syrjälä } bdb_blocks[] = {
165e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERAL_FEATURES,
166e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_general_features), },
167e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERAL_DEFINITIONS,
168e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_general_definitions), },
169e163cfb4SVille Syrjälä 	{ .section_id = BDB_PSR,
170e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_psr), },
171e163cfb4SVille Syrjälä 	{ .section_id = BDB_DRIVER_FEATURES,
172e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_driver_features), },
173e163cfb4SVille Syrjälä 	{ .section_id = BDB_SDVO_LVDS_OPTIONS,
174e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_sdvo_lvds_options), },
1758e266908SVille Syrjälä 	{ .section_id = BDB_SDVO_LVDS_DTD,
1768e266908SVille Syrjälä 	  .min_size = sizeof(struct bdb_sdvo_lvds_dtd), },
177e163cfb4SVille Syrjälä 	{ .section_id = BDB_EDP,
178e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_edp), },
1796ac67ccfSVille Syrjälä 	{ .section_id = BDB_LFP_OPTIONS,
1806ac67ccfSVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_options), },
181901a0cadSVille Syrjälä 	/*
1826ac67ccfSVille Syrjälä 	 * BDB_LFP_DATA depends on BDB_LFP_DATA_PTRS,
183901a0cadSVille Syrjälä 	 * so keep the two ordered.
184901a0cadSVille Syrjälä 	 */
1856ac67ccfSVille Syrjälä 	{ .section_id = BDB_LFP_DATA_PTRS,
1866ac67ccfSVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_data_ptrs), },
1876ac67ccfSVille Syrjälä 	{ .section_id = BDB_LFP_DATA,
188901a0cadSVille Syrjälä 	  .min_size = 0, /* special case */ },
1896ac67ccfSVille Syrjälä 	{ .section_id = BDB_LFP_BACKLIGHT,
1906ac67ccfSVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_backlight), },
191e163cfb4SVille Syrjälä 	{ .section_id = BDB_LFP_POWER,
192e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_power), },
193e163cfb4SVille Syrjälä 	{ .section_id = BDB_MIPI_CONFIG,
194e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_mipi_config), },
195e163cfb4SVille Syrjälä 	{ .section_id = BDB_MIPI_SEQUENCE,
196e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_mipi_sequence) },
197e163cfb4SVille Syrjälä 	{ .section_id = BDB_COMPRESSION_PARAMETERS,
198e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_compression_parameters), },
199e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERIC_DTD,
200e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_generic_dtd), },
201e163cfb4SVille Syrjälä };
202e163cfb4SVille Syrjälä 
lfp_data_min_size(struct intel_display * display)2039aec6f76SJani Nikula static size_t lfp_data_min_size(struct intel_display *display)
204901a0cadSVille Syrjälä {
2056ac67ccfSVille Syrjälä 	const struct bdb_lfp_data_ptrs *ptrs;
206901a0cadSVille Syrjälä 	size_t size;
207901a0cadSVille Syrjälä 
2089aec6f76SJani Nikula 	ptrs = bdb_find_section(display, BDB_LFP_DATA_PTRS);
209901a0cadSVille Syrjälä 	if (!ptrs)
210901a0cadSVille Syrjälä 		return 0;
211901a0cadSVille Syrjälä 
2126ac67ccfSVille Syrjälä 	size = sizeof(struct bdb_lfp_data);
213901a0cadSVille Syrjälä 	if (ptrs->panel_name.table_size)
214901a0cadSVille Syrjälä 		size = max(size, ptrs->panel_name.offset +
2156ac67ccfSVille Syrjälä 			   sizeof(struct bdb_lfp_data_tail));
216901a0cadSVille Syrjälä 
217901a0cadSVille Syrjälä 	return size;
218901a0cadSVille Syrjälä }
219901a0cadSVille Syrjälä 
validate_lfp_data_ptrs(const void * bdb,const struct bdb_lfp_data_ptrs * ptrs)220514003e1SVille Syrjälä static bool validate_lfp_data_ptrs(const void *bdb,
2216ac67ccfSVille Syrjälä 				   const struct bdb_lfp_data_ptrs *ptrs)
222514003e1SVille Syrjälä {
2235ab58d69SVille Syrjälä 	int fp_timing_size, dvo_timing_size, panel_pnp_id_size, panel_name_size;
224514003e1SVille Syrjälä 	int data_block_size, lfp_data_size;
2254e78d602SVille Syrjälä 	const void *data_block;
226514003e1SVille Syrjälä 	int i;
227514003e1SVille Syrjälä 
2286ac67ccfSVille Syrjälä 	data_block = find_raw_section(bdb, BDB_LFP_DATA);
2294e78d602SVille Syrjälä 	if (!data_block)
2304e78d602SVille Syrjälä 		return false;
2314e78d602SVille Syrjälä 
2324e78d602SVille Syrjälä 	data_block_size = get_blocksize(data_block);
233514003e1SVille Syrjälä 	if (data_block_size == 0)
234514003e1SVille Syrjälä 		return false;
235514003e1SVille Syrjälä 
236514003e1SVille Syrjälä 	/* always 3 indicating the presence of fp_timing+dvo_timing+panel_pnp_id */
2376ac67ccfSVille Syrjälä 	if (ptrs->num_entries != 3)
238514003e1SVille Syrjälä 		return false;
239514003e1SVille Syrjälä 
240514003e1SVille Syrjälä 	fp_timing_size = ptrs->ptr[0].fp_timing.table_size;
241514003e1SVille Syrjälä 	dvo_timing_size = ptrs->ptr[0].dvo_timing.table_size;
242514003e1SVille Syrjälä 	panel_pnp_id_size = ptrs->ptr[0].panel_pnp_id.table_size;
2435ab58d69SVille Syrjälä 	panel_name_size = ptrs->panel_name.table_size;
244514003e1SVille Syrjälä 
245514003e1SVille Syrjälä 	/* fp_timing has variable size */
246514003e1SVille Syrjälä 	if (fp_timing_size < 32 ||
2477234f948SVille Syrjälä 	    dvo_timing_size != sizeof(struct bdb_edid_dtd) ||
2487234f948SVille Syrjälä 	    panel_pnp_id_size != sizeof(struct bdb_edid_pnp_id))
249514003e1SVille Syrjälä 		return false;
250514003e1SVille Syrjälä 
2515ab58d69SVille Syrjälä 	/* panel_name is not present in old VBTs */
2525ab58d69SVille Syrjälä 	if (panel_name_size != 0 &&
2537234f948SVille Syrjälä 	    panel_name_size != sizeof(struct bdb_edid_product_name))
2545ab58d69SVille Syrjälä 		return false;
2555ab58d69SVille Syrjälä 
256514003e1SVille Syrjälä 	lfp_data_size = ptrs->ptr[1].fp_timing.offset - ptrs->ptr[0].fp_timing.offset;
257514003e1SVille Syrjälä 	if (16 * lfp_data_size > data_block_size)
258514003e1SVille Syrjälä 		return false;
259514003e1SVille Syrjälä 
260514003e1SVille Syrjälä 	/* make sure the table entries have uniform size */
261514003e1SVille Syrjälä 	for (i = 1; i < 16; i++) {
262514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.table_size != fp_timing_size ||
263514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.table_size != dvo_timing_size ||
264514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.table_size != panel_pnp_id_size)
265514003e1SVille Syrjälä 			return false;
266514003e1SVille Syrjälä 
267514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset - ptrs->ptr[i-1].fp_timing.offset != lfp_data_size ||
268514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset - ptrs->ptr[i-1].dvo_timing.offset != lfp_data_size ||
269514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset - ptrs->ptr[i-1].panel_pnp_id.offset != lfp_data_size)
270514003e1SVille Syrjälä 			return false;
271514003e1SVille Syrjälä 	}
272514003e1SVille Syrjälä 
2734e78d602SVille Syrjälä 	/*
2744e78d602SVille Syrjälä 	 * Except for vlv/chv machines all real VBTs seem to have 6
2754e78d602SVille Syrjälä 	 * unaccounted bytes in the fp_timing table. And it doesn't
2764e78d602SVille Syrjälä 	 * appear to be a really intentional hole as the fp_timing
2774e78d602SVille Syrjälä 	 * 0xffff terminator is always within those 6 missing bytes.
2784e78d602SVille Syrjälä 	 */
2794e78d602SVille Syrjälä 	if (fp_timing_size + 6 + dvo_timing_size + panel_pnp_id_size == lfp_data_size)
2804e78d602SVille Syrjälä 		fp_timing_size += 6;
2814e78d602SVille Syrjälä 
2824e78d602SVille Syrjälä 	if (fp_timing_size + dvo_timing_size + panel_pnp_id_size != lfp_data_size)
2834e78d602SVille Syrjälä 		return false;
2844e78d602SVille Syrjälä 
2854e78d602SVille Syrjälä 	if (ptrs->ptr[0].fp_timing.offset + fp_timing_size != ptrs->ptr[0].dvo_timing.offset ||
2864e78d602SVille Syrjälä 	    ptrs->ptr[0].dvo_timing.offset + dvo_timing_size != ptrs->ptr[0].panel_pnp_id.offset ||
2874e78d602SVille Syrjälä 	    ptrs->ptr[0].panel_pnp_id.offset + panel_pnp_id_size != lfp_data_size)
2884e78d602SVille Syrjälä 		return false;
2894e78d602SVille Syrjälä 
290514003e1SVille Syrjälä 	/* make sure the tables fit inside the data block */
291514003e1SVille Syrjälä 	for (i = 0; i < 16; i++) {
292514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset + fp_timing_size > data_block_size ||
293514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset + dvo_timing_size > data_block_size ||
294514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset + panel_pnp_id_size > data_block_size)
295514003e1SVille Syrjälä 			return false;
296514003e1SVille Syrjälä 	}
297514003e1SVille Syrjälä 
2985ab58d69SVille Syrjälä 	if (ptrs->panel_name.offset + 16 * panel_name_size > data_block_size)
2995ab58d69SVille Syrjälä 		return false;
3005ab58d69SVille Syrjälä 
3014e78d602SVille Syrjälä 	/* make sure fp_timing terminators are present at expected locations */
3024e78d602SVille Syrjälä 	for (i = 0; i < 16; i++) {
3034e78d602SVille Syrjälä 		const u16 *t = data_block + ptrs->ptr[i].fp_timing.offset +
3044e78d602SVille Syrjälä 			fp_timing_size - 2;
3054e78d602SVille Syrjälä 
3064e78d602SVille Syrjälä 		if (*t != 0xffff)
3074e78d602SVille Syrjälä 			return false;
3084e78d602SVille Syrjälä 	}
3094e78d602SVille Syrjälä 
310514003e1SVille Syrjälä 	return true;
311514003e1SVille Syrjälä }
312514003e1SVille Syrjälä 
313918f3025SVille Syrjälä /* make the data table offsets relative to the data block */
fixup_lfp_data_ptrs(const void * bdb,void * ptrs_block)314918f3025SVille Syrjälä static bool fixup_lfp_data_ptrs(const void *bdb, void *ptrs_block)
315918f3025SVille Syrjälä {
3166ac67ccfSVille Syrjälä 	struct bdb_lfp_data_ptrs *ptrs = ptrs_block;
317918f3025SVille Syrjälä 	u32 offset;
318918f3025SVille Syrjälä 	int i;
319918f3025SVille Syrjälä 
3206ac67ccfSVille Syrjälä 	offset = raw_block_offset(bdb, BDB_LFP_DATA);
321918f3025SVille Syrjälä 
322918f3025SVille Syrjälä 	for (i = 0; i < 16; i++) {
323918f3025SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset < offset ||
324918f3025SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset < offset ||
325918f3025SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset < offset)
326918f3025SVille Syrjälä 			return false;
327918f3025SVille Syrjälä 
328918f3025SVille Syrjälä 		ptrs->ptr[i].fp_timing.offset -= offset;
329918f3025SVille Syrjälä 		ptrs->ptr[i].dvo_timing.offset -= offset;
330918f3025SVille Syrjälä 		ptrs->ptr[i].panel_pnp_id.offset -= offset;
331918f3025SVille Syrjälä 	}
332918f3025SVille Syrjälä 
3335ab58d69SVille Syrjälä 	if (ptrs->panel_name.table_size) {
3345ab58d69SVille Syrjälä 		if (ptrs->panel_name.offset < offset)
3355ab58d69SVille Syrjälä 			return false;
3365ab58d69SVille Syrjälä 
3375ab58d69SVille Syrjälä 		ptrs->panel_name.offset -= offset;
3385ab58d69SVille Syrjälä 	}
3395ab58d69SVille Syrjälä 
340514003e1SVille Syrjälä 	return validate_lfp_data_ptrs(bdb, ptrs);
341918f3025SVille Syrjälä }
342918f3025SVille Syrjälä 
make_lfp_data_ptr(struct lfp_data_ptr_table * table,int table_size,int total_size)3436ac67ccfSVille Syrjälä static int make_lfp_data_ptr(struct lfp_data_ptr_table *table,
344a87d0a84SVille Syrjälä 			     int table_size, int total_size)
345a87d0a84SVille Syrjälä {
346a87d0a84SVille Syrjälä 	if (total_size < table_size)
347a87d0a84SVille Syrjälä 		return total_size;
348a87d0a84SVille Syrjälä 
349a87d0a84SVille Syrjälä 	table->table_size = table_size;
350a87d0a84SVille Syrjälä 	table->offset = total_size - table_size;
351a87d0a84SVille Syrjälä 
352a87d0a84SVille Syrjälä 	return total_size - table_size;
353a87d0a84SVille Syrjälä }
354a87d0a84SVille Syrjälä 
next_lfp_data_ptr(struct lfp_data_ptr_table * next,const struct lfp_data_ptr_table * prev,int size)3556ac67ccfSVille Syrjälä static void next_lfp_data_ptr(struct lfp_data_ptr_table *next,
3566ac67ccfSVille Syrjälä 			      const struct lfp_data_ptr_table *prev,
357a87d0a84SVille Syrjälä 			      int size)
358a87d0a84SVille Syrjälä {
359a87d0a84SVille Syrjälä 	next->table_size = prev->table_size;
360a87d0a84SVille Syrjälä 	next->offset = prev->offset + size;
361a87d0a84SVille Syrjälä }
362a87d0a84SVille Syrjälä 
generate_lfp_data_ptrs(struct intel_display * display,const void * bdb)3639aec6f76SJani Nikula static void *generate_lfp_data_ptrs(struct intel_display *display,
364a87d0a84SVille Syrjälä 				    const void *bdb)
365a87d0a84SVille Syrjälä {
366d3a70518SVille Syrjälä 	int i, size, table_size, block_size, offset, fp_timing_size;
3676ac67ccfSVille Syrjälä 	struct bdb_lfp_data_ptrs *ptrs;
368d3a70518SVille Syrjälä 	const void *block;
369a87d0a84SVille Syrjälä 	void *ptrs_block;
370a87d0a84SVille Syrjälä 
371d3a70518SVille Syrjälä 	/*
372d3a70518SVille Syrjälä 	 * The hardcoded fp_timing_size is only valid for
373d3a70518SVille Syrjälä 	 * modernish VBTs. All older VBTs definitely should
374d3a70518SVille Syrjälä 	 * include block 41 and thus we don't need to
375d3a70518SVille Syrjälä 	 * generate one.
376d3a70518SVille Syrjälä 	 */
3779aec6f76SJani Nikula 	if (display->vbt.version < 155)
378d3a70518SVille Syrjälä 		return NULL;
379d3a70518SVille Syrjälä 
380d3a70518SVille Syrjälä 	fp_timing_size = 38;
381d3a70518SVille Syrjälä 
3826ac67ccfSVille Syrjälä 	block = find_raw_section(bdb, BDB_LFP_DATA);
383a87d0a84SVille Syrjälä 	if (!block)
384a87d0a84SVille Syrjälä 		return NULL;
385a87d0a84SVille Syrjälä 
3869aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "Generating LFP data table pointers\n");
387a87d0a84SVille Syrjälä 
388a87d0a84SVille Syrjälä 	block_size = get_blocksize(block);
389a87d0a84SVille Syrjälä 
3907234f948SVille Syrjälä 	size = fp_timing_size + sizeof(struct bdb_edid_dtd) +
3917234f948SVille Syrjälä 		sizeof(struct bdb_edid_pnp_id);
392a87d0a84SVille Syrjälä 	if (size * 16 > block_size)
393a87d0a84SVille Syrjälä 		return NULL;
394a87d0a84SVille Syrjälä 
395a87d0a84SVille Syrjälä 	ptrs_block = kzalloc(sizeof(*ptrs) + 3, GFP_KERNEL);
396a87d0a84SVille Syrjälä 	if (!ptrs_block)
397a87d0a84SVille Syrjälä 		return NULL;
398a87d0a84SVille Syrjälä 
3996ac67ccfSVille Syrjälä 	*(u8 *)(ptrs_block + 0) = BDB_LFP_DATA_PTRS;
400a87d0a84SVille Syrjälä 	*(u16 *)(ptrs_block + 1) = sizeof(*ptrs);
401a87d0a84SVille Syrjälä 	ptrs = ptrs_block + 3;
402a87d0a84SVille Syrjälä 
4037234f948SVille Syrjälä 	table_size = sizeof(struct bdb_edid_pnp_id);
404a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].panel_pnp_id, table_size, size);
405a87d0a84SVille Syrjälä 
4067234f948SVille Syrjälä 	table_size = sizeof(struct bdb_edid_dtd);
407a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].dvo_timing, table_size, size);
408a87d0a84SVille Syrjälä 
409d3a70518SVille Syrjälä 	table_size = fp_timing_size;
410a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].fp_timing, table_size, size);
411a87d0a84SVille Syrjälä 
412a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].fp_timing.table_size)
4136ac67ccfSVille Syrjälä 		ptrs->num_entries++;
414a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].dvo_timing.table_size)
4156ac67ccfSVille Syrjälä 		ptrs->num_entries++;
416a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].panel_pnp_id.table_size)
4176ac67ccfSVille Syrjälä 		ptrs->num_entries++;
418a87d0a84SVille Syrjälä 
4196ac67ccfSVille Syrjälä 	if (size != 0 || ptrs->num_entries != 3) {
4207674cd0bSXia Fukun 		kfree(ptrs_block);
421a87d0a84SVille Syrjälä 		return NULL;
422a87d0a84SVille Syrjälä 	}
423a87d0a84SVille Syrjälä 
4247234f948SVille Syrjälä 	size = fp_timing_size + sizeof(struct bdb_edid_dtd) +
4257234f948SVille Syrjälä 		sizeof(struct bdb_edid_pnp_id);
426a87d0a84SVille Syrjälä 	for (i = 1; i < 16; i++) {
427a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].fp_timing, &ptrs->ptr[i-1].fp_timing, size);
428a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].dvo_timing, &ptrs->ptr[i-1].dvo_timing, size);
429a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].panel_pnp_id, &ptrs->ptr[i-1].panel_pnp_id, size);
430a87d0a84SVille Syrjälä 	}
431a87d0a84SVille Syrjälä 
4327234f948SVille Syrjälä 	table_size = sizeof(struct bdb_edid_product_name);
433a87d0a84SVille Syrjälä 
434a87d0a84SVille Syrjälä 	if (16 * (size + table_size) <= block_size) {
435a87d0a84SVille Syrjälä 		ptrs->panel_name.table_size = table_size;
436a87d0a84SVille Syrjälä 		ptrs->panel_name.offset = size * 16;
437a87d0a84SVille Syrjälä 	}
438a87d0a84SVille Syrjälä 
439a87d0a84SVille Syrjälä 	offset = block - bdb;
440a87d0a84SVille Syrjälä 
441a87d0a84SVille Syrjälä 	for (i = 0; i < 16; i++) {
442a87d0a84SVille Syrjälä 		ptrs->ptr[i].fp_timing.offset += offset;
443a87d0a84SVille Syrjälä 		ptrs->ptr[i].dvo_timing.offset += offset;
444a87d0a84SVille Syrjälä 		ptrs->ptr[i].panel_pnp_id.offset += offset;
445a87d0a84SVille Syrjälä 	}
446a87d0a84SVille Syrjälä 
447a87d0a84SVille Syrjälä 	if (ptrs->panel_name.table_size)
448a87d0a84SVille Syrjälä 		ptrs->panel_name.offset += offset;
449a87d0a84SVille Syrjälä 
450a87d0a84SVille Syrjälä 	return ptrs_block;
451a87d0a84SVille Syrjälä }
452a87d0a84SVille Syrjälä 
453e163cfb4SVille Syrjälä static void
init_bdb_block(struct intel_display * display,const void * bdb,enum bdb_block_id section_id,size_t min_size)4549aec6f76SJani Nikula init_bdb_block(struct intel_display *display,
455e163cfb4SVille Syrjälä 	       const void *bdb, enum bdb_block_id section_id,
456e163cfb4SVille Syrjälä 	       size_t min_size)
457e163cfb4SVille Syrjälä {
458e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry;
459a87d0a84SVille Syrjälä 	void *temp_block = NULL;
460e163cfb4SVille Syrjälä 	const void *block;
461e163cfb4SVille Syrjälä 	size_t block_size;
462e163cfb4SVille Syrjälä 
463e163cfb4SVille Syrjälä 	block = find_raw_section(bdb, section_id);
464a87d0a84SVille Syrjälä 
465a87d0a84SVille Syrjälä 	/* Modern VBTs lack the LFP data table pointers block, make one up */
4666ac67ccfSVille Syrjälä 	if (!block && section_id == BDB_LFP_DATA_PTRS) {
4679aec6f76SJani Nikula 		temp_block = generate_lfp_data_ptrs(display, bdb);
468a87d0a84SVille Syrjälä 		if (temp_block)
469a87d0a84SVille Syrjälä 			block = temp_block + 3;
470a87d0a84SVille Syrjälä 	}
471e163cfb4SVille Syrjälä 	if (!block)
472e163cfb4SVille Syrjälä 		return;
473e163cfb4SVille Syrjälä 
4749aec6f76SJani Nikula 	drm_WARN(display->drm, min_size == 0,
475e163cfb4SVille Syrjälä 		 "Block %d min_size is zero\n", section_id);
476e163cfb4SVille Syrjälä 
477e163cfb4SVille Syrjälä 	block_size = get_blocksize(block);
478e163cfb4SVille Syrjälä 
479a06289f3SVille Syrjälä 	/*
480a06289f3SVille Syrjälä 	 * Version number and new block size are considered
481a06289f3SVille Syrjälä 	 * part of the header for MIPI sequenece block v3+.
482a06289f3SVille Syrjälä 	 */
483a06289f3SVille Syrjälä 	if (section_id == BDB_MIPI_SEQUENCE && *(const u8 *)block >= 3)
484a06289f3SVille Syrjälä 		block_size += 5;
485a06289f3SVille Syrjälä 
486e163cfb4SVille Syrjälä 	entry = kzalloc(struct_size(entry, data, max(min_size, block_size) + 3),
487e163cfb4SVille Syrjälä 			GFP_KERNEL);
488a87d0a84SVille Syrjälä 	if (!entry) {
489a87d0a84SVille Syrjälä 		kfree(temp_block);
490e163cfb4SVille Syrjälä 		return;
491a87d0a84SVille Syrjälä 	}
492e163cfb4SVille Syrjälä 
493e163cfb4SVille Syrjälä 	entry->section_id = section_id;
494e163cfb4SVille Syrjälä 	memcpy(entry->data, block - 3, block_size + 3);
495e163cfb4SVille Syrjälä 
496a87d0a84SVille Syrjälä 	kfree(temp_block);
497a87d0a84SVille Syrjälä 
4989aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
4999aec6f76SJani Nikula 		    "Found BDB block %d (size %zu, min size %zu)\n",
500e163cfb4SVille Syrjälä 		    section_id, block_size, min_size);
501e163cfb4SVille Syrjälä 
5026ac67ccfSVille Syrjälä 	if (section_id == BDB_LFP_DATA_PTRS &&
503918f3025SVille Syrjälä 	    !fixup_lfp_data_ptrs(bdb, entry->data + 3)) {
5049aec6f76SJani Nikula 		drm_err(display->drm,
5059aec6f76SJani Nikula 			"VBT has malformed LFP data table pointers\n");
506918f3025SVille Syrjälä 		kfree(entry);
507918f3025SVille Syrjälä 		return;
508918f3025SVille Syrjälä 	}
509918f3025SVille Syrjälä 
5109aec6f76SJani Nikula 	list_add_tail(&entry->node, &display->vbt.bdb_blocks);
511e163cfb4SVille Syrjälä }
512e163cfb4SVille Syrjälä 
init_bdb_blocks(struct intel_display * display,const void * bdb)5139aec6f76SJani Nikula static void init_bdb_blocks(struct intel_display *display,
514e163cfb4SVille Syrjälä 			    const void *bdb)
515e163cfb4SVille Syrjälä {
516e163cfb4SVille Syrjälä 	int i;
517e163cfb4SVille Syrjälä 
518e163cfb4SVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(bdb_blocks); i++) {
519e163cfb4SVille Syrjälä 		enum bdb_block_id section_id = bdb_blocks[i].section_id;
520e163cfb4SVille Syrjälä 		size_t min_size = bdb_blocks[i].min_size;
521e163cfb4SVille Syrjälä 
5226ac67ccfSVille Syrjälä 		if (section_id == BDB_LFP_DATA)
5239aec6f76SJani Nikula 			min_size = lfp_data_min_size(display);
524901a0cadSVille Syrjälä 
5259aec6f76SJani Nikula 		init_bdb_block(display, bdb, section_id, min_size);
526e163cfb4SVille Syrjälä 	}
527e163cfb4SVille Syrjälä }
528e163cfb4SVille Syrjälä 
529df0566a6SJani Nikula static void
fill_detail_timing_data(struct intel_display * display,struct drm_display_mode * panel_fixed_mode,const struct bdb_edid_dtd * dvo_timing)5309aec6f76SJani Nikula fill_detail_timing_data(struct intel_display *display,
531bb6f53d4SVille Syrjälä 			struct drm_display_mode *panel_fixed_mode,
5327234f948SVille Syrjälä 			const struct bdb_edid_dtd *dvo_timing)
533df0566a6SJani Nikula {
534df0566a6SJani Nikula 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
535df0566a6SJani Nikula 		dvo_timing->hactive_lo;
536df0566a6SJani Nikula 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
537df0566a6SJani Nikula 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
538df0566a6SJani Nikula 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
539df0566a6SJani Nikula 		((dvo_timing->hsync_pulse_width_hi << 8) |
540df0566a6SJani Nikula 			dvo_timing->hsync_pulse_width_lo);
541df0566a6SJani Nikula 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
542df0566a6SJani Nikula 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
543df0566a6SJani Nikula 
544df0566a6SJani Nikula 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
545df0566a6SJani Nikula 		dvo_timing->vactive_lo;
546df0566a6SJani Nikula 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
547df0566a6SJani Nikula 		((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
548df0566a6SJani Nikula 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
549df0566a6SJani Nikula 		((dvo_timing->vsync_pulse_width_hi << 4) |
550df0566a6SJani Nikula 			dvo_timing->vsync_pulse_width_lo);
551df0566a6SJani Nikula 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
552df0566a6SJani Nikula 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
553df0566a6SJani Nikula 	panel_fixed_mode->clock = dvo_timing->clock * 10;
554df0566a6SJani Nikula 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
555df0566a6SJani Nikula 
556df0566a6SJani Nikula 	if (dvo_timing->hsync_positive)
557df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
558df0566a6SJani Nikula 	else
559df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
560df0566a6SJani Nikula 
561df0566a6SJani Nikula 	if (dvo_timing->vsync_positive)
562df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
563df0566a6SJani Nikula 	else
564df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
565df0566a6SJani Nikula 
566df0566a6SJani Nikula 	panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
567df0566a6SJani Nikula 		dvo_timing->himage_lo;
568df0566a6SJani Nikula 	panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
569df0566a6SJani Nikula 		dvo_timing->vimage_lo;
570df0566a6SJani Nikula 
571bb6f53d4SVille Syrjälä 	/* Some VBTs have bogus h/vsync_end values */
572bb6f53d4SVille Syrjälä 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal) {
5739aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "reducing hsync_end %d->%d\n",
574bb6f53d4SVille Syrjälä 			    panel_fixed_mode->hsync_end, panel_fixed_mode->htotal);
575bb6f53d4SVille Syrjälä 		panel_fixed_mode->hsync_end = panel_fixed_mode->htotal;
576bb6f53d4SVille Syrjälä 	}
577bb6f53d4SVille Syrjälä 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal) {
5789aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "reducing vsync_end %d->%d\n",
579bb6f53d4SVille Syrjälä 			    panel_fixed_mode->vsync_end, panel_fixed_mode->vtotal);
580bb6f53d4SVille Syrjälä 		panel_fixed_mode->vsync_end = panel_fixed_mode->vtotal;
581bb6f53d4SVille Syrjälä 	}
582df0566a6SJani Nikula 
583df0566a6SJani Nikula 	drm_mode_set_name(panel_fixed_mode);
584df0566a6SJani Nikula }
585df0566a6SJani Nikula 
5867234f948SVille Syrjälä static const struct bdb_edid_dtd *
get_lfp_dvo_timing(const struct bdb_lfp_data * data,const struct bdb_lfp_data_ptrs * ptrs,int index)5876ac67ccfSVille Syrjälä get_lfp_dvo_timing(const struct bdb_lfp_data *data,
5886ac67ccfSVille Syrjälä 		   const struct bdb_lfp_data_ptrs *ptrs,
589df0566a6SJani Nikula 		   int index)
590df0566a6SJani Nikula {
59158b2e382SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].dvo_timing.offset;
592df0566a6SJani Nikula }
593df0566a6SJani Nikula 
5946ac67ccfSVille Syrjälä static const struct fp_timing *
get_lfp_fp_timing(const struct bdb_lfp_data * data,const struct bdb_lfp_data_ptrs * ptrs,int index)5956ac67ccfSVille Syrjälä get_lfp_fp_timing(const struct bdb_lfp_data *data,
5966ac67ccfSVille Syrjälä 		  const struct bdb_lfp_data_ptrs *ptrs,
597df0566a6SJani Nikula 		  int index)
598df0566a6SJani Nikula {
59958b2e382SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].fp_timing.offset;
600df0566a6SJani Nikula }
601df0566a6SJani Nikula 
6021701e62fSJani Nikula static const struct drm_edid_product_id *
get_lfp_pnp_id(const struct bdb_lfp_data * data,const struct bdb_lfp_data_ptrs * ptrs,int index)6036ac67ccfSVille Syrjälä get_lfp_pnp_id(const struct bdb_lfp_data *data,
6046ac67ccfSVille Syrjälä 	       const struct bdb_lfp_data_ptrs *ptrs,
605c518a775SVille Syrjälä 	       int index)
606c518a775SVille Syrjälä {
6076d247582SJani Nikula 	/* These two are supposed to have the same layout in memory. */
6087234f948SVille Syrjälä 	BUILD_BUG_ON(sizeof(struct bdb_edid_pnp_id) != sizeof(struct drm_edid_product_id));
6096d247582SJani Nikula 
610c518a775SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].panel_pnp_id.offset;
611c518a775SVille Syrjälä }
612c518a775SVille Syrjälä 
6136ac67ccfSVille Syrjälä static const struct bdb_lfp_data_tail *
get_lfp_data_tail(const struct bdb_lfp_data * data,const struct bdb_lfp_data_ptrs * ptrs)6146ac67ccfSVille Syrjälä get_lfp_data_tail(const struct bdb_lfp_data *data,
6156ac67ccfSVille Syrjälä 		  const struct bdb_lfp_data_ptrs *ptrs)
616901a0cadSVille Syrjälä {
617901a0cadSVille Syrjälä 	if (ptrs->panel_name.table_size)
618901a0cadSVille Syrjälä 		return (const void *)data + ptrs->panel_name.offset;
619901a0cadSVille Syrjälä 	else
620901a0cadSVille Syrjälä 		return NULL;
621901a0cadSVille Syrjälä }
622901a0cadSVille Syrjälä 
opregion_get_panel_type(struct intel_display * display,const struct intel_bios_encoder_data * devdata,const struct drm_edid * drm_edid,bool use_fallback)6239aec6f76SJani Nikula static int opregion_get_panel_type(struct intel_display *display,
6246434cf63SAnimesh Manna 				   const struct intel_bios_encoder_data *devdata,
625c36225a1SJani Nikula 				   const struct drm_edid *drm_edid, bool use_fallback)
626cc589f2dSVille Syrjälä {
627769b081cSJani Nikula 	return intel_opregion_get_panel_type(display);
628cc589f2dSVille Syrjälä }
629cc589f2dSVille Syrjälä 
vbt_get_panel_type(struct intel_display * display,const struct intel_bios_encoder_data * devdata,const struct drm_edid * drm_edid,bool use_fallback)6309aec6f76SJani Nikula static int vbt_get_panel_type(struct intel_display *display,
6316434cf63SAnimesh Manna 			      const struct intel_bios_encoder_data *devdata,
632c36225a1SJani Nikula 			      const struct drm_edid *drm_edid, bool use_fallback)
633719f4c51SVille Syrjälä {
6346ac67ccfSVille Syrjälä 	const struct bdb_lfp_options *lfp_options;
635719f4c51SVille Syrjälä 
6369aec6f76SJani Nikula 	lfp_options = bdb_find_section(display, BDB_LFP_OPTIONS);
6376ac67ccfSVille Syrjälä 	if (!lfp_options)
638719f4c51SVille Syrjälä 		return -1;
639719f4c51SVille Syrjälä 
6406ac67ccfSVille Syrjälä 	if (lfp_options->panel_type > 0xf &&
6416ac67ccfSVille Syrjälä 	    lfp_options->panel_type != 0xff) {
6429aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "Invalid VBT panel type 0x%x\n",
6436ac67ccfSVille Syrjälä 			    lfp_options->panel_type);
644719f4c51SVille Syrjälä 		return -1;
645719f4c51SVille Syrjälä 	}
646719f4c51SVille Syrjälä 
6476434cf63SAnimesh Manna 	if (devdata && devdata->child.handle == DEVICE_HANDLE_LFP2)
6486ac67ccfSVille Syrjälä 		return lfp_options->panel_type2;
6496434cf63SAnimesh Manna 
6509aec6f76SJani Nikula 	drm_WARN_ON(display->drm,
6519aec6f76SJani Nikula 		    devdata && devdata->child.handle != DEVICE_HANDLE_LFP1);
6526434cf63SAnimesh Manna 
6536ac67ccfSVille Syrjälä 	return lfp_options->panel_type;
654719f4c51SVille Syrjälä }
655719f4c51SVille Syrjälä 
pnpid_get_panel_type(struct intel_display * display,const struct intel_bios_encoder_data * devdata,const struct drm_edid * drm_edid,bool use_fallback)6569aec6f76SJani Nikula static int pnpid_get_panel_type(struct intel_display *display,
6576434cf63SAnimesh Manna 				const struct intel_bios_encoder_data *devdata,
658c36225a1SJani Nikula 				const struct drm_edid *drm_edid, bool use_fallback)
659c518a775SVille Syrjälä {
6606ac67ccfSVille Syrjälä 	const struct bdb_lfp_data *data;
6616ac67ccfSVille Syrjälä 	const struct bdb_lfp_data_ptrs *ptrs;
6626d247582SJani Nikula 	struct drm_edid_product_id product_id, product_id_nodate;
6636d247582SJani Nikula 	struct drm_printer p;
664c518a775SVille Syrjälä 	int i, best = -1;
665c518a775SVille Syrjälä 
6666d247582SJani Nikula 	if (!drm_edid)
667c518a775SVille Syrjälä 		return -1;
668c518a775SVille Syrjälä 
6696d247582SJani Nikula 	drm_edid_get_product_id(drm_edid, &product_id);
670c518a775SVille Syrjälä 
6716d247582SJani Nikula 	product_id_nodate = product_id;
6726d247582SJani Nikula 	product_id_nodate.week_of_manufacture = 0;
6736d247582SJani Nikula 	product_id_nodate.year_of_manufacture = 0;
674c518a775SVille Syrjälä 
6759aec6f76SJani Nikula 	p = drm_dbg_printer(display->drm, DRM_UT_KMS, "EDID");
6766d247582SJani Nikula 	drm_edid_print_product_id(&p, &product_id, true);
67706bfa86eSVille Syrjälä 
6789aec6f76SJani Nikula 	ptrs = bdb_find_section(display, BDB_LFP_DATA_PTRS);
679c518a775SVille Syrjälä 	if (!ptrs)
680c518a775SVille Syrjälä 		return -1;
681c518a775SVille Syrjälä 
6829aec6f76SJani Nikula 	data = bdb_find_section(display, BDB_LFP_DATA);
683c518a775SVille Syrjälä 	if (!data)
684c518a775SVille Syrjälä 		return -1;
685c518a775SVille Syrjälä 
686c518a775SVille Syrjälä 	for (i = 0; i < 16; i++) {
6871701e62fSJani Nikula 		const struct drm_edid_product_id *vbt_id =
6886ac67ccfSVille Syrjälä 			get_lfp_pnp_id(data, ptrs, i);
689c518a775SVille Syrjälä 
690c518a775SVille Syrjälä 		/* full match? */
6916d247582SJani Nikula 		if (!memcmp(vbt_id, &product_id, sizeof(*vbt_id)))
692c518a775SVille Syrjälä 			return i;
693c518a775SVille Syrjälä 
694c518a775SVille Syrjälä 		/*
695c518a775SVille Syrjälä 		 * Accept a match w/o date if no full match is found,
696c518a775SVille Syrjälä 		 * and the VBT entry does not specify a date.
697c518a775SVille Syrjälä 		 */
698c518a775SVille Syrjälä 		if (best < 0 &&
6996d247582SJani Nikula 		    !memcmp(vbt_id, &product_id_nodate, sizeof(*vbt_id)))
700c518a775SVille Syrjälä 			best = i;
701c518a775SVille Syrjälä 	}
702c518a775SVille Syrjälä 
703c518a775SVille Syrjälä 	return best;
704c518a775SVille Syrjälä }
705c518a775SVille Syrjälä 
fallback_get_panel_type(struct intel_display * display,const struct intel_bios_encoder_data * devdata,const struct drm_edid * drm_edid,bool use_fallback)7069aec6f76SJani Nikula static int fallback_get_panel_type(struct intel_display *display,
7076434cf63SAnimesh Manna 				   const struct intel_bios_encoder_data *devdata,
708c36225a1SJani Nikula 				   const struct drm_edid *drm_edid, bool use_fallback)
709cc589f2dSVille Syrjälä {
7103f9ffce5SVille Syrjälä 	return use_fallback ? 0 : -1;
711cc589f2dSVille Syrjälä }
712cc589f2dSVille Syrjälä 
713cc589f2dSVille Syrjälä enum panel_type {
714cc589f2dSVille Syrjälä 	PANEL_TYPE_OPREGION,
715cc589f2dSVille Syrjälä 	PANEL_TYPE_VBT,
716c518a775SVille Syrjälä 	PANEL_TYPE_PNPID,
717cc589f2dSVille Syrjälä 	PANEL_TYPE_FALLBACK,
718cc589f2dSVille Syrjälä };
719cc589f2dSVille Syrjälä 
get_panel_type(struct intel_display * display,const struct intel_bios_encoder_data * devdata,const struct drm_edid * drm_edid,bool use_fallback)7209aec6f76SJani Nikula static int get_panel_type(struct intel_display *display,
7216434cf63SAnimesh Manna 			  const struct intel_bios_encoder_data *devdata,
722c36225a1SJani Nikula 			  const struct drm_edid *drm_edid, bool use_fallback)
723719f4c51SVille Syrjälä {
724cc589f2dSVille Syrjälä 	struct {
725cc589f2dSVille Syrjälä 		const char *name;
7269aec6f76SJani Nikula 		int (*get_panel_type)(struct intel_display *display,
7276434cf63SAnimesh Manna 				      const struct intel_bios_encoder_data *devdata,
728c36225a1SJani Nikula 				      const struct drm_edid *drm_edid, bool use_fallback);
729cc589f2dSVille Syrjälä 		int panel_type;
730cc589f2dSVille Syrjälä 	} panel_types[] = {
731cc589f2dSVille Syrjälä 		[PANEL_TYPE_OPREGION] = {
732cc589f2dSVille Syrjälä 			.name = "OpRegion",
733cc589f2dSVille Syrjälä 			.get_panel_type = opregion_get_panel_type,
734cc589f2dSVille Syrjälä 		},
735cc589f2dSVille Syrjälä 		[PANEL_TYPE_VBT] = {
736cc589f2dSVille Syrjälä 			.name = "VBT",
737cc589f2dSVille Syrjälä 			.get_panel_type = vbt_get_panel_type,
738cc589f2dSVille Syrjälä 		},
739c518a775SVille Syrjälä 		[PANEL_TYPE_PNPID] = {
740c518a775SVille Syrjälä 			.name = "PNPID",
741c518a775SVille Syrjälä 			.get_panel_type = pnpid_get_panel_type,
742c518a775SVille Syrjälä 		},
743cc589f2dSVille Syrjälä 		[PANEL_TYPE_FALLBACK] = {
744cc589f2dSVille Syrjälä 			.name = "fallback",
745cc589f2dSVille Syrjälä 			.get_panel_type = fallback_get_panel_type,
746cc589f2dSVille Syrjälä 		},
747cc589f2dSVille Syrjälä 	};
748cc589f2dSVille Syrjälä 	int i;
749719f4c51SVille Syrjälä 
750cc589f2dSVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(panel_types); i++) {
7519aec6f76SJani Nikula 		panel_types[i].panel_type = panel_types[i].get_panel_type(display, devdata,
752c36225a1SJani Nikula 									  drm_edid, use_fallback);
753cc589f2dSVille Syrjälä 
7549aec6f76SJani Nikula 		drm_WARN_ON(display->drm, panel_types[i].panel_type > 0xf &&
755c518a775SVille Syrjälä 			    panel_types[i].panel_type != 0xff);
756cc589f2dSVille Syrjälä 
757cc589f2dSVille Syrjälä 		if (panel_types[i].panel_type >= 0)
7589aec6f76SJani Nikula 			drm_dbg_kms(display->drm, "Panel type (%s): %d\n",
759cc589f2dSVille Syrjälä 				    panel_types[i].name, panel_types[i].panel_type);
760719f4c51SVille Syrjälä 	}
761719f4c51SVille Syrjälä 
762cc589f2dSVille Syrjälä 	if (panel_types[PANEL_TYPE_OPREGION].panel_type >= 0)
763cc589f2dSVille Syrjälä 		i = PANEL_TYPE_OPREGION;
764c518a775SVille Syrjälä 	else if (panel_types[PANEL_TYPE_VBT].panel_type == 0xff &&
765c518a775SVille Syrjälä 		 panel_types[PANEL_TYPE_PNPID].panel_type >= 0)
766c518a775SVille Syrjälä 		i = PANEL_TYPE_PNPID;
767c518a775SVille Syrjälä 	else if (panel_types[PANEL_TYPE_VBT].panel_type != 0xff &&
768c518a775SVille Syrjälä 		 panel_types[PANEL_TYPE_VBT].panel_type >= 0)
769cc589f2dSVille Syrjälä 		i = PANEL_TYPE_VBT;
770cc589f2dSVille Syrjälä 	else
771cc589f2dSVille Syrjälä 		i = PANEL_TYPE_FALLBACK;
772719f4c51SVille Syrjälä 
7739aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "Selected panel type (%s): %d\n",
774cc589f2dSVille Syrjälä 		    panel_types[i].name, panel_types[i].panel_type);
775cc589f2dSVille Syrjälä 
776cc589f2dSVille Syrjälä 	return panel_types[i].panel_type;
777719f4c51SVille Syrjälä }
778719f4c51SVille Syrjälä 
panel_bits(unsigned int value,int panel_type,int num_bits)779a50cc495SVille Syrjälä static unsigned int panel_bits(unsigned int value, int panel_type, int num_bits)
780a50cc495SVille Syrjälä {
781a50cc495SVille Syrjälä 	return (value >> (panel_type * num_bits)) & (BIT(num_bits) - 1);
782a50cc495SVille Syrjälä }
783a50cc495SVille Syrjälä 
panel_bool(unsigned int value,int panel_type)784a50cc495SVille Syrjälä static bool panel_bool(unsigned int value, int panel_type)
785a50cc495SVille Syrjälä {
786a50cc495SVille Syrjälä 	return panel_bits(value, panel_type, 1);
787a50cc495SVille Syrjälä }
788a50cc495SVille Syrjälä 
7899e7ecedfSMatt Roper /* Parse general panel options */
790df0566a6SJani Nikula static void
parse_panel_options(struct intel_display * display,struct intel_panel * panel)7919aec6f76SJani Nikula parse_panel_options(struct intel_display *display,
7920256ea13SVille Syrjälä 		    struct intel_panel *panel)
793df0566a6SJani Nikula {
7946ac67ccfSVille Syrjälä 	const struct bdb_lfp_options *lfp_options;
7950256ea13SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
796df0566a6SJani Nikula 	int drrs_mode;
797df0566a6SJani Nikula 
7989aec6f76SJani Nikula 	lfp_options = bdb_find_section(display, BDB_LFP_OPTIONS);
7996ac67ccfSVille Syrjälä 	if (!lfp_options)
800df0566a6SJani Nikula 		return;
801df0566a6SJani Nikula 
8026ac67ccfSVille Syrjälä 	panel->vbt.lvds_dither = lfp_options->pixel_dither;
803df0566a6SJani Nikula 
8045c9016b2SVille Syrjälä 	/*
8055c9016b2SVille Syrjälä 	 * Empirical evidence indicates the block size can be
8065c9016b2SVille Syrjälä 	 * either 4,14,16,24+ bytes. For older VBTs no clear
8075c9016b2SVille Syrjälä 	 * relationship between the block size vs. BDB version.
8085c9016b2SVille Syrjälä 	 */
8096ac67ccfSVille Syrjälä 	if (get_blocksize(lfp_options) < 16)
8105c9016b2SVille Syrjälä 		return;
811df0566a6SJani Nikula 
8126ac67ccfSVille Syrjälä 	drrs_mode = panel_bits(lfp_options->dps_panel_type_bits,
813a50cc495SVille Syrjälä 			       panel_type, 2);
814df0566a6SJani Nikula 	/*
815df0566a6SJani Nikula 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
816df0566a6SJani Nikula 	 * The below piece of code is required to adjust vbt.drrs_type
817df0566a6SJani Nikula 	 * to match the enum drrs_support_type.
818df0566a6SJani Nikula 	 */
819df0566a6SJani Nikula 	switch (drrs_mode) {
820df0566a6SJani Nikula 	case 0:
8213cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_STATIC;
8229aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "DRRS supported mode is static\n");
823df0566a6SJani Nikula 		break;
824df0566a6SJani Nikula 	case 2:
8253cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_SEAMLESS;
8269aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
827e92cbf38SWambui Karuga 			    "DRRS supported mode is seamless\n");
828df0566a6SJani Nikula 		break;
829df0566a6SJani Nikula 	default:
8303cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_NONE;
8319aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
832e92cbf38SWambui Karuga 			    "DRRS not supported (VBT input)\n");
833df0566a6SJani Nikula 		break;
834df0566a6SJani Nikula 	}
8359e7ecedfSMatt Roper }
8369e7ecedfSMatt Roper 
8379e7ecedfSMatt Roper static void
parse_lfp_panel_dtd(struct intel_display * display,struct intel_panel * panel,const struct bdb_lfp_data * lfp_data,const struct bdb_lfp_data_ptrs * lfp_data_ptrs)8389aec6f76SJani Nikula parse_lfp_panel_dtd(struct intel_display *display,
8393cf05076SVille Syrjälä 		    struct intel_panel *panel,
8406ac67ccfSVille Syrjälä 		    const struct bdb_lfp_data *lfp_data,
8416ac67ccfSVille Syrjälä 		    const struct bdb_lfp_data_ptrs *lfp_data_ptrs)
8429e7ecedfSMatt Roper {
8437234f948SVille Syrjälä 	const struct bdb_edid_dtd *panel_dvo_timing;
8446ac67ccfSVille Syrjälä 	const struct fp_timing *fp_timing;
8459e7ecedfSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
8463cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
847df0566a6SJani Nikula 
8486ac67ccfSVille Syrjälä 	panel_dvo_timing = get_lfp_dvo_timing(lfp_data,
8496ac67ccfSVille Syrjälä 					      lfp_data_ptrs,
850df0566a6SJani Nikula 					      panel_type);
851df0566a6SJani Nikula 
852df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
853df0566a6SJani Nikula 	if (!panel_fixed_mode)
854df0566a6SJani Nikula 		return;
855df0566a6SJani Nikula 
8569aec6f76SJani Nikula 	fill_detail_timing_data(display, panel_fixed_mode, panel_dvo_timing);
857df0566a6SJani Nikula 
8586ac67ccfSVille Syrjälä 	panel->vbt.lfp_vbt_mode = panel_fixed_mode;
859df0566a6SJani Nikula 
8609aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
861f01bae2dSVille Syrjälä 		    "Found panel mode in BIOS VBT legacy lfp table: " DRM_MODE_FMT "\n",
862f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
863df0566a6SJani Nikula 
8646ac67ccfSVille Syrjälä 	fp_timing = get_lfp_fp_timing(lfp_data,
8656ac67ccfSVille Syrjälä 				      lfp_data_ptrs,
866df0566a6SJani Nikula 				      panel_type);
86758b2e382SVille Syrjälä 
868df0566a6SJani Nikula 	/* check the resolution, just to be sure */
869df0566a6SJani Nikula 	if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
870df0566a6SJani Nikula 	    fp_timing->y_res == panel_fixed_mode->vdisplay) {
8713cf05076SVille Syrjälä 		panel->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
8729aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
873e92cbf38SWambui Karuga 			    "VBT initial LVDS value %x\n",
8743cf05076SVille Syrjälä 			    panel->vbt.bios_lvds_val);
875df0566a6SJani Nikula 	}
876df0566a6SJani Nikula }
877df0566a6SJani Nikula 
878df0566a6SJani Nikula static void
parse_lfp_data(struct intel_display * display,struct intel_panel * panel)8799aec6f76SJani Nikula parse_lfp_data(struct intel_display *display,
8803cf05076SVille Syrjälä 	       struct intel_panel *panel)
88113367132SVille Syrjälä {
8826ac67ccfSVille Syrjälä 	const struct bdb_lfp_data *data;
8836ac67ccfSVille Syrjälä 	const struct bdb_lfp_data_tail *tail;
8846ac67ccfSVille Syrjälä 	const struct bdb_lfp_data_ptrs *ptrs;
8851701e62fSJani Nikula 	const struct drm_edid_product_id *pnp_id;
8866d247582SJani Nikula 	struct drm_printer p;
8873cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
88813367132SVille Syrjälä 
8899aec6f76SJani Nikula 	ptrs = bdb_find_section(display, BDB_LFP_DATA_PTRS);
89013367132SVille Syrjälä 	if (!ptrs)
89113367132SVille Syrjälä 		return;
89213367132SVille Syrjälä 
8939aec6f76SJani Nikula 	data = bdb_find_section(display, BDB_LFP_DATA);
89413367132SVille Syrjälä 	if (!data)
89513367132SVille Syrjälä 		return;
89613367132SVille Syrjälä 
8976ac67ccfSVille Syrjälä 	if (!panel->vbt.lfp_vbt_mode)
8989aec6f76SJani Nikula 		parse_lfp_panel_dtd(display, panel, data, ptrs);
899901a0cadSVille Syrjälä 
9006ac67ccfSVille Syrjälä 	pnp_id = get_lfp_pnp_id(data, ptrs, panel_type);
9016d247582SJani Nikula 
9029aec6f76SJani Nikula 	p = drm_dbg_printer(display->drm, DRM_UT_KMS, "Panel");
9031701e62fSJani Nikula 	drm_edid_print_product_id(&p, pnp_id, false);
90406bfa86eSVille Syrjälä 
905901a0cadSVille Syrjälä 	tail = get_lfp_data_tail(data, ptrs);
906901a0cadSVille Syrjälä 	if (!tail)
907901a0cadSVille Syrjälä 		return;
908901a0cadSVille Syrjälä 
9099aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "Panel name: %.*s\n",
91006bfa86eSVille Syrjälä 		    (int)sizeof(tail->panel_name[0].name),
91106bfa86eSVille Syrjälä 		    tail->panel_name[panel_type].name);
91206bfa86eSVille Syrjälä 
9139aec6f76SJani Nikula 	if (display->vbt.version >= 188) {
9143cf05076SVille Syrjälä 		panel->vbt.seamless_drrs_min_refresh_rate =
915790b45f1SVille Syrjälä 			tail->seamless_drrs_min_refresh_rate[panel_type];
9169aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
917790b45f1SVille Syrjälä 			    "Seamless DRRS min refresh rate: %d Hz\n",
9183cf05076SVille Syrjälä 			    panel->vbt.seamless_drrs_min_refresh_rate);
919790b45f1SVille Syrjälä 	}
92013367132SVille Syrjälä }
92113367132SVille Syrjälä 
92213367132SVille Syrjälä static void
parse_generic_dtd(struct intel_display * display,struct intel_panel * panel)9239aec6f76SJani Nikula parse_generic_dtd(struct intel_display *display,
9243cf05076SVille Syrjälä 		  struct intel_panel *panel)
92533ef6d4fSMatt Roper {
92633ef6d4fSMatt Roper 	const struct bdb_generic_dtd *generic_dtd;
92733ef6d4fSMatt Roper 	const struct generic_dtd_entry *dtd;
92833ef6d4fSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
92933ef6d4fSMatt Roper 	int num_dtd;
93033ef6d4fSMatt Roper 
93113367132SVille Syrjälä 	/*
93213367132SVille Syrjälä 	 * Older VBTs provided DTD information for internal displays through
93313367132SVille Syrjälä 	 * the "LFP panel tables" block (42).  As of VBT revision 229 the
93413367132SVille Syrjälä 	 * DTD information should be provided via a newer "generic DTD"
93513367132SVille Syrjälä 	 * block (58).  Just to be safe, we'll try the new generic DTD block
93613367132SVille Syrjälä 	 * first on VBT >= 229, but still fall back to trying the old LFP
93713367132SVille Syrjälä 	 * block if that fails.
93813367132SVille Syrjälä 	 */
9399aec6f76SJani Nikula 	if (display->vbt.version < 229)
94013367132SVille Syrjälä 		return;
94113367132SVille Syrjälä 
9429aec6f76SJani Nikula 	generic_dtd = bdb_find_section(display, BDB_GENERIC_DTD);
94333ef6d4fSMatt Roper 	if (!generic_dtd)
94433ef6d4fSMatt Roper 		return;
94533ef6d4fSMatt Roper 
94633ef6d4fSMatt Roper 	if (generic_dtd->gdtd_size < sizeof(struct generic_dtd_entry)) {
9479aec6f76SJani Nikula 		drm_err(display->drm, "GDTD size %u is too small.\n",
94833ef6d4fSMatt Roper 			generic_dtd->gdtd_size);
94933ef6d4fSMatt Roper 		return;
95033ef6d4fSMatt Roper 	} else if (generic_dtd->gdtd_size !=
95133ef6d4fSMatt Roper 		   sizeof(struct generic_dtd_entry)) {
9529aec6f76SJani Nikula 		drm_err(display->drm, "Unexpected GDTD size %u\n",
953e92cbf38SWambui Karuga 			generic_dtd->gdtd_size);
95433ef6d4fSMatt Roper 		/* DTD has unknown fields, but keep going */
95533ef6d4fSMatt Roper 	}
95633ef6d4fSMatt Roper 
95733ef6d4fSMatt Roper 	num_dtd = (get_blocksize(generic_dtd) -
95833ef6d4fSMatt Roper 		   sizeof(struct bdb_generic_dtd)) / generic_dtd->gdtd_size;
9593cf05076SVille Syrjälä 	if (panel->vbt.panel_type >= num_dtd) {
9609aec6f76SJani Nikula 		drm_err(display->drm,
961e92cbf38SWambui Karuga 			"Panel type %d not found in table of %d DTD's\n",
9623cf05076SVille Syrjälä 			panel->vbt.panel_type, num_dtd);
96333ef6d4fSMatt Roper 		return;
96433ef6d4fSMatt Roper 	}
96533ef6d4fSMatt Roper 
9663cf05076SVille Syrjälä 	dtd = &generic_dtd->dtd[panel->vbt.panel_type];
96733ef6d4fSMatt Roper 
96833ef6d4fSMatt Roper 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
96933ef6d4fSMatt Roper 	if (!panel_fixed_mode)
97033ef6d4fSMatt Roper 		return;
97133ef6d4fSMatt Roper 
97233ef6d4fSMatt Roper 	panel_fixed_mode->hdisplay = dtd->hactive;
97333ef6d4fSMatt Roper 	panel_fixed_mode->hsync_start =
97433ef6d4fSMatt Roper 		panel_fixed_mode->hdisplay + dtd->hfront_porch;
97533ef6d4fSMatt Roper 	panel_fixed_mode->hsync_end =
97633ef6d4fSMatt Roper 		panel_fixed_mode->hsync_start + dtd->hsync;
977ad278f35SVandita Kulkarni 	panel_fixed_mode->htotal =
978ad278f35SVandita Kulkarni 		panel_fixed_mode->hdisplay + dtd->hblank;
97933ef6d4fSMatt Roper 
98033ef6d4fSMatt Roper 	panel_fixed_mode->vdisplay = dtd->vactive;
98133ef6d4fSMatt Roper 	panel_fixed_mode->vsync_start =
98233ef6d4fSMatt Roper 		panel_fixed_mode->vdisplay + dtd->vfront_porch;
98333ef6d4fSMatt Roper 	panel_fixed_mode->vsync_end =
98433ef6d4fSMatt Roper 		panel_fixed_mode->vsync_start + dtd->vsync;
985ad278f35SVandita Kulkarni 	panel_fixed_mode->vtotal =
986ad278f35SVandita Kulkarni 		panel_fixed_mode->vdisplay + dtd->vblank;
98733ef6d4fSMatt Roper 
98833ef6d4fSMatt Roper 	panel_fixed_mode->clock = dtd->pixel_clock;
98933ef6d4fSMatt Roper 	panel_fixed_mode->width_mm = dtd->width_mm;
99033ef6d4fSMatt Roper 	panel_fixed_mode->height_mm = dtd->height_mm;
99133ef6d4fSMatt Roper 
99233ef6d4fSMatt Roper 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
99333ef6d4fSMatt Roper 	drm_mode_set_name(panel_fixed_mode);
99433ef6d4fSMatt Roper 
99533ef6d4fSMatt Roper 	if (dtd->hsync_positive_polarity)
99633ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
99733ef6d4fSMatt Roper 	else
99833ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
99933ef6d4fSMatt Roper 
100033ef6d4fSMatt Roper 	if (dtd->vsync_positive_polarity)
100133ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
100233ef6d4fSMatt Roper 	else
100333ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
100433ef6d4fSMatt Roper 
10059aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
1006f01bae2dSVille Syrjälä 		    "Found panel mode in BIOS VBT generic dtd table: " DRM_MODE_FMT "\n",
1007f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
100833ef6d4fSMatt Roper 
10096ac67ccfSVille Syrjälä 	panel->vbt.lfp_vbt_mode = panel_fixed_mode;
101033ef6d4fSMatt Roper }
101133ef6d4fSMatt Roper 
101233ef6d4fSMatt Roper static void
parse_lfp_backlight(struct intel_display * display,struct intel_panel * panel)10139aec6f76SJani Nikula parse_lfp_backlight(struct intel_display *display,
10143cf05076SVille Syrjälä 		    struct intel_panel *panel)
1015df0566a6SJani Nikula {
10166ac67ccfSVille Syrjälä 	const struct bdb_lfp_backlight *backlight_data;
1017df0566a6SJani Nikula 	const struct lfp_backlight_data_entry *entry;
10183cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1019d381baadSJosé Roberto de Souza 	u16 level;
1020df0566a6SJani Nikula 
10219aec6f76SJani Nikula 	backlight_data = bdb_find_section(display, BDB_LFP_BACKLIGHT);
1022df0566a6SJani Nikula 	if (!backlight_data)
1023df0566a6SJani Nikula 		return;
1024df0566a6SJani Nikula 
1025df0566a6SJani Nikula 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
10269aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
1027e92cbf38SWambui Karuga 			    "Unsupported backlight data entry size %u\n",
1028df0566a6SJani Nikula 			    backlight_data->entry_size);
1029df0566a6SJani Nikula 		return;
1030df0566a6SJani Nikula 	}
1031df0566a6SJani Nikula 
1032df0566a6SJani Nikula 	entry = &backlight_data->data[panel_type];
1033df0566a6SJani Nikula 
10343cf05076SVille Syrjälä 	panel->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
10353cf05076SVille Syrjälä 	if (!panel->vbt.backlight.present) {
10369aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
1037e92cbf38SWambui Karuga 			    "PWM backlight not present in VBT (type %u)\n",
1038df0566a6SJani Nikula 			    entry->type);
1039df0566a6SJani Nikula 		return;
1040df0566a6SJani Nikula 	}
1041df0566a6SJani Nikula 
10423cf05076SVille Syrjälä 	panel->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
1043a0dcb06dSJani Nikula 	panel->vbt.backlight.controller = 0;
10449aec6f76SJani Nikula 	if (display->vbt.version >= 191) {
1045df0566a6SJani Nikula 		const struct lfp_backlight_control_method *method;
1046df0566a6SJani Nikula 
1047df0566a6SJani Nikula 		method = &backlight_data->backlight_control[panel_type];
10483cf05076SVille Syrjälä 		panel->vbt.backlight.type = method->type;
10493cf05076SVille Syrjälä 		panel->vbt.backlight.controller = method->controller;
1050df0566a6SJani Nikula 	}
1051df0566a6SJani Nikula 
10523cf05076SVille Syrjälä 	panel->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
10533cf05076SVille Syrjälä 	panel->vbt.backlight.active_low_pwm = entry->active_low_pwm;
1054d381baadSJosé Roberto de Souza 
10559aec6f76SJani Nikula 	if (display->vbt.version >= 234) {
1056d381baadSJosé Roberto de Souza 		u16 min_level;
1057d381baadSJosé Roberto de Souza 		bool scale;
1058d381baadSJosé Roberto de Souza 
1059d381baadSJosé Roberto de Souza 		level = backlight_data->brightness_level[panel_type].level;
1060d381baadSJosé Roberto de Souza 		min_level = backlight_data->brightness_min_level[panel_type].level;
1061d381baadSJosé Roberto de Souza 
10629aec6f76SJani Nikula 		if (display->vbt.version >= 236)
1063d381baadSJosé Roberto de Souza 			scale = backlight_data->brightness_precision_bits[panel_type] == 16;
1064d381baadSJosé Roberto de Souza 		else
1065d381baadSJosé Roberto de Souza 			scale = level > 255;
1066d381baadSJosé Roberto de Souza 
1067d381baadSJosé Roberto de Souza 		if (scale)
1068d381baadSJosé Roberto de Souza 			min_level = min_level / 255;
1069d381baadSJosé Roberto de Souza 
1070d381baadSJosé Roberto de Souza 		if (min_level > 255) {
10719aec6f76SJani Nikula 			drm_warn(display->drm, "Brightness min level > 255\n");
1072d381baadSJosé Roberto de Souza 			level = 255;
1073d381baadSJosé Roberto de Souza 		}
10743cf05076SVille Syrjälä 		panel->vbt.backlight.min_brightness = min_level;
107584d3d71fSLee Shawn C 
10763cf05076SVille Syrjälä 		panel->vbt.backlight.brightness_precision_bits =
107784d3d71fSLee Shawn C 			backlight_data->brightness_precision_bits[panel_type];
1078d381baadSJosé Roberto de Souza 	} else {
1079d381baadSJosé Roberto de Souza 		level = backlight_data->level[panel_type];
10803cf05076SVille Syrjälä 		panel->vbt.backlight.min_brightness = entry->min_brightness;
1081d381baadSJosé Roberto de Souza 	}
1082d381baadSJosé Roberto de Souza 
10839aec6f76SJani Nikula 	if (display->vbt.version >= 239)
1084fe82b93fSVille Syrjälä 		panel->vbt.backlight.hdr_dpcd_refresh_timeout =
1085fe82b93fSVille Syrjälä 			DIV_ROUND_UP(backlight_data->hdr_dpcd_refresh_timeout[panel_type], 100);
1086fe82b93fSVille Syrjälä 	else
1087fe82b93fSVille Syrjälä 		panel->vbt.backlight.hdr_dpcd_refresh_timeout = 30;
1088fe82b93fSVille Syrjälä 
10899aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
1090e92cbf38SWambui Karuga 		    "VBT backlight PWM modulation frequency %u Hz, "
1091df0566a6SJani Nikula 		    "active %s, min brightness %u, level %u, controller %u\n",
10923cf05076SVille Syrjälä 		    panel->vbt.backlight.pwm_freq_hz,
10933cf05076SVille Syrjälä 		    panel->vbt.backlight.active_low_pwm ? "low" : "high",
10943cf05076SVille Syrjälä 		    panel->vbt.backlight.min_brightness,
1095d381baadSJosé Roberto de Souza 		    level,
10963cf05076SVille Syrjälä 		    panel->vbt.backlight.controller);
1097df0566a6SJani Nikula }
1098df0566a6SJani Nikula 
1099df0566a6SJani Nikula static void
parse_sdvo_lvds_data(struct intel_display * display,struct intel_panel * panel)11009aec6f76SJani Nikula parse_sdvo_lvds_data(struct intel_display *display,
11013cf05076SVille Syrjälä 		     struct intel_panel *panel)
1102df0566a6SJani Nikula {
11038e266908SVille Syrjälä 	const struct bdb_sdvo_lvds_dtd *dtd;
1104df0566a6SJani Nikula 	struct drm_display_mode *panel_fixed_mode;
1105df0566a6SJani Nikula 	int index;
1106df0566a6SJani Nikula 
11079aec6f76SJani Nikula 	index = display->params.vbt_sdvo_panel_type;
1108df0566a6SJani Nikula 	if (index == -2) {
11099aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
11108e266908SVille Syrjälä 			    "Ignore SDVO LVDS mode from BIOS VBT tables.\n");
1111df0566a6SJani Nikula 		return;
1112df0566a6SJani Nikula 	}
1113df0566a6SJani Nikula 
1114df0566a6SJani Nikula 	if (index == -1) {
1115df0566a6SJani Nikula 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
1116df0566a6SJani Nikula 
11179aec6f76SJani Nikula 		sdvo_lvds_options = bdb_find_section(display, BDB_SDVO_LVDS_OPTIONS);
1118df0566a6SJani Nikula 		if (!sdvo_lvds_options)
1119df0566a6SJani Nikula 			return;
1120df0566a6SJani Nikula 
1121df0566a6SJani Nikula 		index = sdvo_lvds_options->panel_type;
1122df0566a6SJani Nikula 	}
1123df0566a6SJani Nikula 
11249aec6f76SJani Nikula 	dtd = bdb_find_section(display, BDB_SDVO_LVDS_DTD);
11258e266908SVille Syrjälä 	if (!dtd)
1126df0566a6SJani Nikula 		return;
1127df0566a6SJani Nikula 
1128b2c2f2dfSLuca Coelho 	/*
1129b2c2f2dfSLuca Coelho 	 * This should not happen, as long as the panel_type
1130b2c2f2dfSLuca Coelho 	 * enumeration doesn't grow over 4 items.  But if it does, it
1131b2c2f2dfSLuca Coelho 	 * could lead to hard-to-detect bugs, so better double-check
1132b2c2f2dfSLuca Coelho 	 * it here to be sure.
1133b2c2f2dfSLuca Coelho 	 */
1134b2c2f2dfSLuca Coelho 	if (index >= ARRAY_SIZE(dtd->dtd)) {
11359aec6f76SJani Nikula 		drm_err(display->drm,
11369aec6f76SJani Nikula 			"index %d is larger than dtd->dtd[4] array\n",
1137b2c2f2dfSLuca Coelho 			index);
1138b2c2f2dfSLuca Coelho 		return;
1139b2c2f2dfSLuca Coelho 	}
1140b2c2f2dfSLuca Coelho 
1141df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
1142df0566a6SJani Nikula 	if (!panel_fixed_mode)
1143df0566a6SJani Nikula 		return;
1144df0566a6SJani Nikula 
11459aec6f76SJani Nikula 	fill_detail_timing_data(display, panel_fixed_mode, &dtd->dtd[index]);
1146df0566a6SJani Nikula 
11473cf05076SVille Syrjälä 	panel->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
1148df0566a6SJani Nikula 
11499aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
11508e266908SVille Syrjälä 		    "Found SDVO LVDS mode in BIOS VBT tables: " DRM_MODE_FMT "\n",
1151f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
1152df0566a6SJani Nikula }
1153df0566a6SJani Nikula 
intel_bios_ssc_frequency(struct intel_display * display,bool alternate)11549aec6f76SJani Nikula static int intel_bios_ssc_frequency(struct intel_display *display,
1155df0566a6SJani Nikula 				    bool alternate)
1156df0566a6SJani Nikula {
11579aec6f76SJani Nikula 	switch (DISPLAY_VER(display)) {
1158df0566a6SJani Nikula 	case 2:
1159df0566a6SJani Nikula 		return alternate ? 66667 : 48000;
1160df0566a6SJani Nikula 	case 3:
1161df0566a6SJani Nikula 	case 4:
1162df0566a6SJani Nikula 		return alternate ? 100000 : 96000;
1163df0566a6SJani Nikula 	default:
1164df0566a6SJani Nikula 		return alternate ? 100000 : 120000;
1165df0566a6SJani Nikula 	}
1166df0566a6SJani Nikula }
1167df0566a6SJani Nikula 
1168df0566a6SJani Nikula static void
parse_general_features(struct intel_display * display)11699aec6f76SJani Nikula parse_general_features(struct intel_display *display)
1170df0566a6SJani Nikula {
11719aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
1172df0566a6SJani Nikula 	const struct bdb_general_features *general;
1173df0566a6SJani Nikula 
11749aec6f76SJani Nikula 	general = bdb_find_section(display, BDB_GENERAL_FEATURES);
1175df0566a6SJani Nikula 	if (!general)
1176df0566a6SJani Nikula 		return;
1177df0566a6SJani Nikula 
11789aec6f76SJani Nikula 	display->vbt.int_tv_support = general->int_tv_support;
1179df0566a6SJani Nikula 	/* int_crt_support can't be trusted on earlier platforms */
11809aec6f76SJani Nikula 	if (display->vbt.version >= 155 &&
11819aec6f76SJani Nikula 	    (HAS_DDI(display) || IS_VALLEYVIEW(i915)))
11829aec6f76SJani Nikula 		display->vbt.int_crt_support = general->int_crt_support;
11839aec6f76SJani Nikula 	display->vbt.lvds_use_ssc = general->enable_ssc;
11849aec6f76SJani Nikula 	display->vbt.lvds_ssc_freq =
11859aec6f76SJani Nikula 		intel_bios_ssc_frequency(display, general->ssc_freq);
11869aec6f76SJani Nikula 	display->vbt.display_clock_mode = general->display_clock_mode;
11879aec6f76SJani Nikula 	display->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
11889aec6f76SJani Nikula 	if (display->vbt.version >= 181) {
11899aec6f76SJani Nikula 		display->vbt.orientation = general->rotate_180 ?
1190df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
1191df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_NORMAL;
1192df0566a6SJani Nikula 	} else {
11939aec6f76SJani Nikula 		display->vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1194df0566a6SJani Nikula 	}
1195b70ad01aSJosé Roberto de Souza 
11969aec6f76SJani Nikula 	if (display->vbt.version >= 249 && general->afc_startup_config) {
11979aec6f76SJani Nikula 		display->vbt.override_afc_startup = true;
11989aec6f76SJani Nikula 		display->vbt.override_afc_startup_val = general->afc_startup_config == 1 ? 0 : 7;
1199b70ad01aSJosé Roberto de Souza 	}
1200b70ad01aSJosé Roberto de Souza 
12019aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
1202e92cbf38SWambui 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",
12039aec6f76SJani Nikula 		    display->vbt.int_tv_support,
12049aec6f76SJani Nikula 		    display->vbt.int_crt_support,
12059aec6f76SJani Nikula 		    display->vbt.lvds_use_ssc,
12069aec6f76SJani Nikula 		    display->vbt.lvds_ssc_freq,
12079aec6f76SJani Nikula 		    display->vbt.display_clock_mode,
12089aec6f76SJani Nikula 		    display->vbt.fdi_rx_polarity_inverted);
1209df0566a6SJani Nikula }
1210df0566a6SJani Nikula 
1211df0566a6SJani Nikula static const struct child_device_config *
child_device_ptr(const struct bdb_general_definitions * defs,int i)1212df0566a6SJani Nikula child_device_ptr(const struct bdb_general_definitions *defs, int i)
1213df0566a6SJani Nikula {
1214df0566a6SJani Nikula 	return (const void *) &defs->devices[i * defs->child_dev_size];
1215df0566a6SJani Nikula }
1216df0566a6SJani Nikula 
1217df0566a6SJani Nikula static void
parse_sdvo_device_mapping(struct intel_display * display)12189aec6f76SJani Nikula parse_sdvo_device_mapping(struct intel_display *display)
1219df0566a6SJani Nikula {
12203162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
12210d9ef19bSJani Nikula 	int count = 0;
1222df0566a6SJani Nikula 
1223df0566a6SJani Nikula 	/*
1224df0566a6SJani Nikula 	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
1225df0566a6SJani Nikula 	 * accurate and doesn't have to be, as long as it's not too strict.
1226df0566a6SJani Nikula 	 */
12279aec6f76SJani Nikula 	if (!IS_DISPLAY_VER(display, 3, 7)) {
12289aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "Skipping SDVO device mapping\n");
1229df0566a6SJani Nikula 		return;
1230df0566a6SJani Nikula 	}
1231df0566a6SJani Nikula 
12329aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
1233d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
1234d24b3475SVille Syrjälä 		struct sdvo_device_mapping *mapping;
1235df0566a6SJani Nikula 
1236bc3ca4d9SEaswar Hariharan 		if (child->target_addr != TARGET_ADDR1 &&
1237bc3ca4d9SEaswar Hariharan 		    child->target_addr != TARGET_ADDR2) {
1238df0566a6SJani Nikula 			/*
1239bc3ca4d9SEaswar Hariharan 			 * If the target address is neither 0x70 nor 0x72,
1240df0566a6SJani Nikula 			 * it is not a SDVO device. Skip it.
1241df0566a6SJani Nikula 			 */
1242df0566a6SJani Nikula 			continue;
1243df0566a6SJani Nikula 		}
1244df0566a6SJani Nikula 		if (child->dvo_port != DEVICE_PORT_DVOB &&
1245df0566a6SJani Nikula 		    child->dvo_port != DEVICE_PORT_DVOC) {
1246df0566a6SJani Nikula 			/* skip the incorrect SDVO port */
12479aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
1248e92cbf38SWambui Karuga 				    "Incorrect SDVO port. Skip it\n");
1249df0566a6SJani Nikula 			continue;
1250df0566a6SJani Nikula 		}
12519aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
1252bc3ca4d9SEaswar Hariharan 			    "the SDVO device with target addr %2x is found on"
1253df0566a6SJani Nikula 			    " %s port\n",
1254bc3ca4d9SEaswar Hariharan 			    child->target_addr,
1255df0566a6SJani Nikula 			    (child->dvo_port == DEVICE_PORT_DVOB) ?
1256df0566a6SJani Nikula 			    "SDVOB" : "SDVOC");
12579aec6f76SJani Nikula 		mapping = &display->vbt.sdvo_mappings[child->dvo_port - 1];
1258df0566a6SJani Nikula 		if (!mapping->initialized) {
1259df0566a6SJani Nikula 			mapping->dvo_port = child->dvo_port;
1260bc3ca4d9SEaswar Hariharan 			mapping->target_addr = child->target_addr;
1261df0566a6SJani Nikula 			mapping->dvo_wiring = child->dvo_wiring;
1262df0566a6SJani Nikula 			mapping->ddc_pin = child->ddc_pin;
1263df0566a6SJani Nikula 			mapping->i2c_pin = child->i2c_pin;
1264df0566a6SJani Nikula 			mapping->initialized = 1;
12659aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
1266e92cbf38SWambui Karuga 				    "SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
1267bc3ca4d9SEaswar Hariharan 				    mapping->dvo_port, mapping->target_addr,
1268e92cbf38SWambui Karuga 				    mapping->dvo_wiring, mapping->ddc_pin,
1269df0566a6SJani Nikula 				    mapping->i2c_pin);
1270df0566a6SJani Nikula 		} else {
12719aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
1272e92cbf38SWambui Karuga 				    "Maybe one SDVO port is shared by "
1273df0566a6SJani Nikula 				    "two SDVO device.\n");
1274df0566a6SJani Nikula 		}
1275bc3ca4d9SEaswar Hariharan 		if (child->target2_addr) {
1276df0566a6SJani Nikula 			/* Maybe this is a SDVO device with multiple inputs */
1277df0566a6SJani Nikula 			/* And the mapping info is not added */
12789aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
1279bc3ca4d9SEaswar Hariharan 				    "there exists the target2_addr. Maybe this"
1280df0566a6SJani Nikula 				    " is a SDVO device with multiple inputs.\n");
1281df0566a6SJani Nikula 		}
1282df0566a6SJani Nikula 		count++;
1283df0566a6SJani Nikula 	}
1284df0566a6SJani Nikula 
1285df0566a6SJani Nikula 	if (!count) {
1286df0566a6SJani Nikula 		/* No SDVO device info is found */
12879aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
1288e92cbf38SWambui Karuga 			    "No SDVO device info is found in VBT\n");
1289df0566a6SJani Nikula 	}
1290df0566a6SJani Nikula }
1291df0566a6SJani Nikula 
1292df0566a6SJani Nikula static void
parse_driver_features(struct intel_display * display)12939aec6f76SJani Nikula parse_driver_features(struct intel_display *display)
1294df0566a6SJani Nikula {
1295df0566a6SJani Nikula 	const struct bdb_driver_features *driver;
1296df0566a6SJani Nikula 
12979aec6f76SJani Nikula 	driver = bdb_find_section(display, BDB_DRIVER_FEATURES);
1298df0566a6SJani Nikula 	if (!driver)
1299df0566a6SJani Nikula 		return;
1300df0566a6SJani Nikula 
13019aec6f76SJani Nikula 	if (DISPLAY_VER(display) >= 5) {
1302df0566a6SJani Nikula 		/*
1303df0566a6SJani Nikula 		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
1304df0566a6SJani Nikula 		 * to mean "eDP". The VBT spec doesn't agree with that
1305df0566a6SJani Nikula 		 * interpretation, but real world VBTs seem to.
1306df0566a6SJani Nikula 		 */
1307df0566a6SJani Nikula 		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
13089aec6f76SJani Nikula 			display->vbt.int_lvds_support = 0;
1309df0566a6SJani Nikula 	} else {
1310df0566a6SJani Nikula 		/*
1311df0566a6SJani Nikula 		 * FIXME it's not clear which BDB version has the LVDS config
1312df0566a6SJani Nikula 		 * bits defined. Revision history in the VBT spec says:
1313df0566a6SJani Nikula 		 * "0.92 | Add two definitions for VBT value of LVDS Active
1314df0566a6SJani Nikula 		 *  Config (00b and 11b values defined) | 06/13/2005"
1315df0566a6SJani Nikula 		 * but does not the specify the BDB version.
1316df0566a6SJani Nikula 		 *
1317df0566a6SJani Nikula 		 * So far version 134 (on i945gm) is the oldest VBT observed
1318df0566a6SJani Nikula 		 * in the wild with the bits correctly populated. Version
1319df0566a6SJani Nikula 		 * 108 (on i85x) does not have the bits correctly populated.
1320df0566a6SJani Nikula 		 */
13219aec6f76SJani Nikula 		if (display->vbt.version >= 134 &&
1322df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
1323df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
13249aec6f76SJani Nikula 			display->vbt.int_lvds_support = 0;
1325df0566a6SJani Nikula 	}
1326c3fbcf60SVille Syrjälä }
1327c3fbcf60SVille Syrjälä 
1328c3fbcf60SVille Syrjälä static void
parse_panel_driver_features(struct intel_display * display,struct intel_panel * panel)13299aec6f76SJani Nikula parse_panel_driver_features(struct intel_display *display,
13303cf05076SVille Syrjälä 			    struct intel_panel *panel)
1331c3fbcf60SVille Syrjälä {
1332c3fbcf60SVille Syrjälä 	const struct bdb_driver_features *driver;
1333c3fbcf60SVille Syrjälä 
13349aec6f76SJani Nikula 	driver = bdb_find_section(display, BDB_DRIVER_FEATURES);
1335c3fbcf60SVille Syrjälä 	if (!driver)
1336c3fbcf60SVille Syrjälä 		return;
1337df0566a6SJani Nikula 
13389aec6f76SJani Nikula 	if (display->vbt.version < 228) {
13399aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "DRRS State Enabled:%d\n",
1340e92cbf38SWambui Karuga 			    driver->drrs_enabled);
1341df0566a6SJani Nikula 		/*
1342df0566a6SJani Nikula 		 * If DRRS is not supported, drrs_type has to be set to 0.
1343df0566a6SJani Nikula 		 * This is because, VBT is configured in such a way that
1344df0566a6SJani Nikula 		 * static DRRS is 0 and DRRS not supported is represented by
1345df0566a6SJani Nikula 		 * driver->drrs_enabled=false
1346df0566a6SJani Nikula 		 */
13475a18db2eSVille Syrjälä 		if (!driver->drrs_enabled && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
13485a18db2eSVille Syrjälä 			/*
13495a18db2eSVille Syrjälä 			 * FIXME Should DMRRS perhaps be treated as seamless
13505a18db2eSVille Syrjälä 			 * but without the automatic downclocking?
13515a18db2eSVille Syrjälä 			 */
13525a18db2eSVille Syrjälä 			if (driver->dmrrs_enabled)
13535a18db2eSVille Syrjälä 				panel->vbt.drrs_type = DRRS_TYPE_STATIC;
13545a18db2eSVille Syrjälä 			else
13553cf05076SVille Syrjälä 				panel->vbt.drrs_type = DRRS_TYPE_NONE;
13565a18db2eSVille Syrjälä 		}
1357551fb93dSJosé Roberto de Souza 
13583cf05076SVille Syrjälä 		panel->vbt.psr.enable = driver->psr_enabled;
1359df0566a6SJani Nikula 	}
1360551fb93dSJosé Roberto de Souza }
1361551fb93dSJosé Roberto de Souza 
1362551fb93dSJosé Roberto de Souza static void
parse_power_conservation_features(struct intel_display * display,struct intel_panel * panel)13639aec6f76SJani Nikula parse_power_conservation_features(struct intel_display *display,
13643cf05076SVille Syrjälä 				  struct intel_panel *panel)
1365551fb93dSJosé Roberto de Souza {
1366551fb93dSJosé Roberto de Souza 	const struct bdb_lfp_power *power;
13673cf05076SVille Syrjälä 	u8 panel_type = panel->vbt.panel_type;
1368551fb93dSJosé Roberto de Souza 
1369fba99b1aSVille Syrjälä 	panel->vbt.vrr = true; /* matches Windows behaviour */
1370551fb93dSJosé Roberto de Souza 
13719aec6f76SJani Nikula 	if (display->vbt.version < 228)
1372551fb93dSJosé Roberto de Souza 		return;
1373551fb93dSJosé Roberto de Souza 
13749aec6f76SJani Nikula 	power = bdb_find_section(display, BDB_LFP_POWER);
1375551fb93dSJosé Roberto de Souza 	if (!power)
1376551fb93dSJosé Roberto de Souza 		return;
1377551fb93dSJosé Roberto de Souza 
1378a50cc495SVille Syrjälä 	panel->vbt.psr.enable = panel_bool(power->psr, panel_type);
1379551fb93dSJosé Roberto de Souza 
1380551fb93dSJosé Roberto de Souza 	/*
1381551fb93dSJosé Roberto de Souza 	 * If DRRS is not supported, drrs_type has to be set to 0.
1382551fb93dSJosé Roberto de Souza 	 * This is because, VBT is configured in such a way that
1383551fb93dSJosé Roberto de Souza 	 * static DRRS is 0 and DRRS not supported is represented by
1384551fb93dSJosé Roberto de Souza 	 * power->drrs & BIT(panel_type)=false
1385551fb93dSJosé Roberto de Souza 	 */
1386a50cc495SVille Syrjälä 	if (!panel_bool(power->drrs, panel_type) && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
13875a18db2eSVille Syrjälä 		/*
13885a18db2eSVille Syrjälä 		 * FIXME Should DMRRS perhaps be treated as seamless
13895a18db2eSVille Syrjälä 		 * but without the automatic downclocking?
13905a18db2eSVille Syrjälä 		 */
1391a50cc495SVille Syrjälä 		if (panel_bool(power->dmrrs, panel_type))
13925a18db2eSVille Syrjälä 			panel->vbt.drrs_type = DRRS_TYPE_STATIC;
13935a18db2eSVille Syrjälä 		else
13943cf05076SVille Syrjälä 			panel->vbt.drrs_type = DRRS_TYPE_NONE;
13955a18db2eSVille Syrjälä 	}
1396f615cb6aSJosé Roberto de Souza 
13979aec6f76SJani Nikula 	if (display->vbt.version >= 232)
1398a50cc495SVille Syrjälä 		panel->vbt.edp.hobl = panel_bool(power->hobl, panel_type);
1399fba99b1aSVille Syrjälä 
14009aec6f76SJani Nikula 	if (display->vbt.version >= 233)
1401a50cc495SVille Syrjälä 		panel->vbt.vrr = panel_bool(power->vrr_feature_enabled,
1402a50cc495SVille Syrjälä 					    panel_type);
1403551fb93dSJosé Roberto de Souza }
1404df0566a6SJani Nikula 
1405df0566a6SJani Nikula static void
parse_edp(struct intel_display * display,struct intel_panel * panel)14069aec6f76SJani Nikula parse_edp(struct intel_display *display,
14073cf05076SVille Syrjälä 	  struct intel_panel *panel)
1408df0566a6SJani Nikula {
1409df0566a6SJani Nikula 	const struct bdb_edp *edp;
1410df0566a6SJani Nikula 	const struct edp_power_seq *edp_pps;
1411df0566a6SJani Nikula 	const struct edp_fast_link_params *edp_link_params;
14123cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1413df0566a6SJani Nikula 
14149aec6f76SJani Nikula 	edp = bdb_find_section(display, BDB_EDP);
1415df0566a6SJani Nikula 	if (!edp)
1416df0566a6SJani Nikula 		return;
1417df0566a6SJani Nikula 
1418a50cc495SVille Syrjälä 	switch (panel_bits(edp->color_depth, panel_type, 2)) {
1419df0566a6SJani Nikula 	case EDP_18BPP:
14203cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 18;
1421df0566a6SJani Nikula 		break;
1422df0566a6SJani Nikula 	case EDP_24BPP:
14233cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 24;
1424df0566a6SJani Nikula 		break;
1425df0566a6SJani Nikula 	case EDP_30BPP:
14263cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 30;
1427df0566a6SJani Nikula 		break;
1428df0566a6SJani Nikula 	}
1429df0566a6SJani Nikula 
1430df0566a6SJani Nikula 	/* Get the eDP sequencing and link info */
1431df0566a6SJani Nikula 	edp_pps = &edp->power_seqs[panel_type];
1432df0566a6SJani Nikula 	edp_link_params = &edp->fast_link_params[panel_type];
1433df0566a6SJani Nikula 
14343cf05076SVille Syrjälä 	panel->vbt.edp.pps = *edp_pps;
1435df0566a6SJani Nikula 
14369aec6f76SJani Nikula 	if (display->vbt.version >= 224) {
1437f06d1d66SVille Syrjälä 		panel->vbt.edp.rate =
1438f06d1d66SVille Syrjälä 			edp->edp_fast_link_training_rate[panel_type] * 20;
1439f06d1d66SVille Syrjälä 	} else {
1440df0566a6SJani Nikula 		switch (edp_link_params->rate) {
1441df0566a6SJani Nikula 		case EDP_RATE_1_62:
1442f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 162000;
1443df0566a6SJani Nikula 			break;
1444df0566a6SJani Nikula 		case EDP_RATE_2_7:
1445f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 270000;
1446f06d1d66SVille Syrjälä 			break;
1447f06d1d66SVille Syrjälä 		case EDP_RATE_5_4:
1448f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 540000;
1449df0566a6SJani Nikula 			break;
1450df0566a6SJani Nikula 		default:
14519aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
1452e92cbf38SWambui Karuga 				    "VBT has unknown eDP link rate value %u\n",
1453df0566a6SJani Nikula 				    edp_link_params->rate);
1454df0566a6SJani Nikula 			break;
1455df0566a6SJani Nikula 		}
1456f06d1d66SVille Syrjälä 	}
1457df0566a6SJani Nikula 
1458df0566a6SJani Nikula 	switch (edp_link_params->lanes) {
1459df0566a6SJani Nikula 	case EDP_LANE_1:
14603cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 1;
1461df0566a6SJani Nikula 		break;
1462df0566a6SJani Nikula 	case EDP_LANE_2:
14633cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 2;
1464df0566a6SJani Nikula 		break;
1465df0566a6SJani Nikula 	case EDP_LANE_4:
14663cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 4;
1467df0566a6SJani Nikula 		break;
1468df0566a6SJani Nikula 	default:
14699aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
1470e92cbf38SWambui Karuga 			    "VBT has unknown eDP lane count value %u\n",
1471df0566a6SJani Nikula 			    edp_link_params->lanes);
1472df0566a6SJani Nikula 		break;
1473df0566a6SJani Nikula 	}
1474df0566a6SJani Nikula 
1475df0566a6SJani Nikula 	switch (edp_link_params->preemphasis) {
1476df0566a6SJani Nikula 	case EDP_PREEMPHASIS_NONE:
14773cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
1478df0566a6SJani Nikula 		break;
1479df0566a6SJani Nikula 	case EDP_PREEMPHASIS_3_5dB:
14803cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
1481df0566a6SJani Nikula 		break;
1482df0566a6SJani Nikula 	case EDP_PREEMPHASIS_6dB:
14833cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
1484df0566a6SJani Nikula 		break;
1485df0566a6SJani Nikula 	case EDP_PREEMPHASIS_9_5dB:
14863cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
1487df0566a6SJani Nikula 		break;
1488df0566a6SJani Nikula 	default:
14899aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
1490e92cbf38SWambui Karuga 			    "VBT has unknown eDP pre-emphasis value %u\n",
1491df0566a6SJani Nikula 			    edp_link_params->preemphasis);
1492df0566a6SJani Nikula 		break;
1493df0566a6SJani Nikula 	}
1494df0566a6SJani Nikula 
1495df0566a6SJani Nikula 	switch (edp_link_params->vswing) {
1496df0566a6SJani Nikula 	case EDP_VSWING_0_4V:
14973cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
1498df0566a6SJani Nikula 		break;
1499df0566a6SJani Nikula 	case EDP_VSWING_0_6V:
15003cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
1501df0566a6SJani Nikula 		break;
1502df0566a6SJani Nikula 	case EDP_VSWING_0_8V:
15033cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
1504df0566a6SJani Nikula 		break;
1505df0566a6SJani Nikula 	case EDP_VSWING_1_2V:
15063cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
1507df0566a6SJani Nikula 		break;
1508df0566a6SJani Nikula 	default:
15099aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
1510e92cbf38SWambui Karuga 			    "VBT has unknown eDP voltage swing value %u\n",
1511df0566a6SJani Nikula 			    edp_link_params->vswing);
1512df0566a6SJani Nikula 		break;
1513df0566a6SJani Nikula 	}
1514df0566a6SJani Nikula 
15159aec6f76SJani Nikula 	if (display->vbt.version >= 173) {
1516df0566a6SJani Nikula 		u8 vswing;
1517df0566a6SJani Nikula 
1518df0566a6SJani Nikula 		/* Don't read from VBT if module parameter has valid value*/
15199aec6f76SJani Nikula 		if (display->params.edp_vswing) {
15203cf05076SVille Syrjälä 			panel->vbt.edp.low_vswing =
15219aec6f76SJani Nikula 				display->params.edp_vswing == 1;
1522df0566a6SJani Nikula 		} else {
1523df0566a6SJani Nikula 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
15243cf05076SVille Syrjälä 			panel->vbt.edp.low_vswing = vswing == 0;
1525df0566a6SJani Nikula 		}
1526df0566a6SJani Nikula 	}
1527b395c29aSVille Syrjälä 
15283cf05076SVille Syrjälä 	panel->vbt.edp.drrs_msa_timing_delay =
1529a50cc495SVille Syrjälä 		panel_bits(edp->sdrrs_msa_timing_delay, panel_type, 2);
153024b8b74eSVille Syrjälä 
15319aec6f76SJani Nikula 	if (display->vbt.version >= 244)
153224b8b74eSVille Syrjälä 		panel->vbt.edp.max_link_rate =
153324b8b74eSVille Syrjälä 			edp->edp_max_port_link_rate[panel_type] * 20;
1534b47e62b1SVille Syrjälä 
15359aec6f76SJani Nikula 	if (display->vbt.version >= 251)
1536b47e62b1SVille Syrjälä 		panel->vbt.edp.dsc_disable =
1537b47e62b1SVille Syrjälä 			panel_bool(edp->edp_dsc_disable, panel_type);
1538df0566a6SJani Nikula }
1539df0566a6SJani Nikula 
1540df0566a6SJani Nikula static void
parse_psr(struct intel_display * display,struct intel_panel * panel)15419aec6f76SJani Nikula parse_psr(struct intel_display *display,
15423cf05076SVille Syrjälä 	  struct intel_panel *panel)
1543df0566a6SJani Nikula {
15449aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
1545df0566a6SJani Nikula 	const struct bdb_psr *psr;
1546df0566a6SJani Nikula 	const struct psr_table *psr_table;
15473cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1548df0566a6SJani Nikula 
15499aec6f76SJani Nikula 	psr = bdb_find_section(display, BDB_PSR);
1550df0566a6SJani Nikula 	if (!psr) {
15519aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "No PSR BDB found.\n");
1552df0566a6SJani Nikula 		return;
1553df0566a6SJani Nikula 	}
1554df0566a6SJani Nikula 
1555df0566a6SJani Nikula 	psr_table = &psr->psr_table[panel_type];
1556df0566a6SJani Nikula 
15573cf05076SVille Syrjälä 	panel->vbt.psr.full_link = psr_table->full_link;
15583cf05076SVille Syrjälä 	panel->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
1559df0566a6SJani Nikula 
1560df0566a6SJani Nikula 	/* Allowed VBT values goes from 0 to 15 */
15613cf05076SVille Syrjälä 	panel->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
1562df0566a6SJani Nikula 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
1563df0566a6SJani Nikula 
1564df0566a6SJani Nikula 	/*
1565df0566a6SJani Nikula 	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
1566df0566a6SJani Nikula 	 * Old decimal value is wake up time in multiples of 100 us.
1567df0566a6SJani Nikula 	 */
15689aec6f76SJani Nikula 	if (display->vbt.version >= 205 &&
15699aec6f76SJani Nikula 	    (DISPLAY_VER(display) >= 9 && !IS_BROXTON(i915))) {
1570df0566a6SJani Nikula 		switch (psr_table->tp1_wakeup_time) {
1571df0566a6SJani Nikula 		case 0:
15723cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 500;
1573df0566a6SJani Nikula 			break;
1574df0566a6SJani Nikula 		case 1:
15753cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 100;
1576df0566a6SJani Nikula 			break;
1577df0566a6SJani Nikula 		case 3:
15783cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 0;
1579df0566a6SJani Nikula 			break;
1580df0566a6SJani Nikula 		default:
15819aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
1582e92cbf38SWambui Karuga 				    "VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1583df0566a6SJani Nikula 				    psr_table->tp1_wakeup_time);
1584df561f66SGustavo A. R. Silva 			fallthrough;
1585df0566a6SJani Nikula 		case 2:
15863cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 2500;
1587df0566a6SJani Nikula 			break;
1588df0566a6SJani Nikula 		}
1589df0566a6SJani Nikula 
1590df0566a6SJani Nikula 		switch (psr_table->tp2_tp3_wakeup_time) {
1591df0566a6SJani Nikula 		case 0:
15923cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 500;
1593df0566a6SJani Nikula 			break;
1594df0566a6SJani Nikula 		case 1:
15953cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 100;
1596df0566a6SJani Nikula 			break;
1597df0566a6SJani Nikula 		case 3:
15983cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 0;
1599df0566a6SJani Nikula 			break;
1600df0566a6SJani Nikula 		default:
16019aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
1602e92cbf38SWambui Karuga 				    "VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1603df0566a6SJani Nikula 				    psr_table->tp2_tp3_wakeup_time);
1604df561f66SGustavo A. R. Silva 			fallthrough;
1605df0566a6SJani Nikula 		case 2:
16063cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
1607df0566a6SJani Nikula 		break;
1608df0566a6SJani Nikula 		}
1609df0566a6SJani Nikula 	} else {
16103cf05076SVille Syrjälä 		panel->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
16113cf05076SVille Syrjälä 		panel->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
1612df0566a6SJani Nikula 	}
1613df0566a6SJani Nikula 
16149aec6f76SJani Nikula 	if (display->vbt.version >= 226) {
1615b5ea9c93SDhinakaran Pandiyan 		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
1616df0566a6SJani Nikula 
1617a50cc495SVille Syrjälä 		wakeup_time = panel_bits(wakeup_time, panel_type, 2);
1618df0566a6SJani Nikula 		switch (wakeup_time) {
1619df0566a6SJani Nikula 		case 0:
1620df0566a6SJani Nikula 			wakeup_time = 500;
1621df0566a6SJani Nikula 			break;
1622df0566a6SJani Nikula 		case 1:
1623df0566a6SJani Nikula 			wakeup_time = 100;
1624df0566a6SJani Nikula 			break;
1625df0566a6SJani Nikula 		case 3:
1626df0566a6SJani Nikula 			wakeup_time = 50;
1627df0566a6SJani Nikula 			break;
1628df0566a6SJani Nikula 		default:
1629df0566a6SJani Nikula 		case 2:
1630df0566a6SJani Nikula 			wakeup_time = 2500;
1631df0566a6SJani Nikula 			break;
1632df0566a6SJani Nikula 		}
16333cf05076SVille Syrjälä 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
1634df0566a6SJani Nikula 	} else {
1635df0566a6SJani Nikula 		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
16363cf05076SVille Syrjälä 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = panel->vbt.psr.tp2_tp3_wakeup_time_us;
1637df0566a6SJani Nikula 	}
1638df0566a6SJani Nikula }
1639df0566a6SJani Nikula 
parse_dsi_backlight_ports(struct intel_display * display,struct intel_panel * panel,enum port port)16409aec6f76SJani Nikula static void parse_dsi_backlight_ports(struct intel_display *display,
16413cf05076SVille Syrjälä 				      struct intel_panel *panel,
16423cf05076SVille Syrjälä 				      enum port port)
1643df0566a6SJani Nikula {
16449aec6f76SJani Nikula 	enum port port_bc = DISPLAY_VER(display) >= 11 ? PORT_B : PORT_C;
1645ab55165dSJani Nikula 
16469aec6f76SJani Nikula 	if (!panel->vbt.dsi.config->dual_link || display->vbt.version < 197) {
16473cf05076SVille Syrjälä 		panel->vbt.dsi.bl_ports = BIT(port);
16483cf05076SVille Syrjälä 		if (panel->vbt.dsi.config->cabc_supported)
16493cf05076SVille Syrjälä 			panel->vbt.dsi.cabc_ports = BIT(port);
1650df0566a6SJani Nikula 
1651df0566a6SJani Nikula 		return;
1652df0566a6SJani Nikula 	}
1653df0566a6SJani Nikula 
16543cf05076SVille Syrjälä 	switch (panel->vbt.dsi.config->dl_dcs_backlight_ports) {
1655df0566a6SJani Nikula 	case DL_DCS_PORT_A:
16563cf05076SVille Syrjälä 		panel->vbt.dsi.bl_ports = BIT(PORT_A);
1657df0566a6SJani Nikula 		break;
1658df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1659ab55165dSJani Nikula 		panel->vbt.dsi.bl_ports = BIT(port_bc);
1660df0566a6SJani Nikula 		break;
1661df0566a6SJani Nikula 	default:
1662df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
1663ab55165dSJani Nikula 		panel->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(port_bc);
1664df0566a6SJani Nikula 		break;
1665df0566a6SJani Nikula 	}
1666df0566a6SJani Nikula 
16673cf05076SVille Syrjälä 	if (!panel->vbt.dsi.config->cabc_supported)
1668df0566a6SJani Nikula 		return;
1669df0566a6SJani Nikula 
16703cf05076SVille Syrjälä 	switch (panel->vbt.dsi.config->dl_dcs_cabc_ports) {
1671df0566a6SJani Nikula 	case DL_DCS_PORT_A:
16723cf05076SVille Syrjälä 		panel->vbt.dsi.cabc_ports = BIT(PORT_A);
1673df0566a6SJani Nikula 		break;
1674df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1675ab55165dSJani Nikula 		panel->vbt.dsi.cabc_ports = BIT(port_bc);
1676df0566a6SJani Nikula 		break;
1677df0566a6SJani Nikula 	default:
1678df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
16793cf05076SVille Syrjälä 		panel->vbt.dsi.cabc_ports =
1680ab55165dSJani Nikula 					BIT(PORT_A) | BIT(port_bc);
1681df0566a6SJani Nikula 		break;
1682df0566a6SJani Nikula 	}
1683df0566a6SJani Nikula }
1684df0566a6SJani Nikula 
1685df0566a6SJani Nikula static void
parse_mipi_config(struct intel_display * display,struct intel_panel * panel)16869aec6f76SJani Nikula parse_mipi_config(struct intel_display *display,
16873cf05076SVille Syrjälä 		  struct intel_panel *panel)
1688df0566a6SJani Nikula {
1689df0566a6SJani Nikula 	const struct bdb_mipi_config *start;
1690df0566a6SJani Nikula 	const struct mipi_config *config;
1691df0566a6SJani Nikula 	const struct mipi_pps_data *pps;
16923cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1693df0566a6SJani Nikula 	enum port port;
1694df0566a6SJani Nikula 
1695df0566a6SJani Nikula 	/* parse MIPI blocks only if LFP type is MIPI */
16969aec6f76SJani Nikula 	if (!intel_bios_is_dsi_present(display, &port))
1697df0566a6SJani Nikula 		return;
1698df0566a6SJani Nikula 
1699df0566a6SJani Nikula 	/* Initialize this to undefined indicating no generic MIPI support */
17003cf05076SVille Syrjälä 	panel->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
1701df0566a6SJani Nikula 
17029aec6f76SJani Nikula 	start = bdb_find_section(display, BDB_MIPI_CONFIG);
1703df0566a6SJani Nikula 	if (!start) {
17049aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "No MIPI config BDB found");
1705df0566a6SJani Nikula 		return;
1706df0566a6SJani Nikula 	}
1707df0566a6SJani Nikula 
17089aec6f76SJani Nikula 	drm_dbg(display->drm, "Found MIPI Config block, panel index = %d\n",
1709df0566a6SJani Nikula 		panel_type);
1710df0566a6SJani Nikula 
1711df0566a6SJani Nikula 	/*
1712df0566a6SJani Nikula 	 * get hold of the correct configuration block and pps data as per
1713df0566a6SJani Nikula 	 * the panel_type as index
1714df0566a6SJani Nikula 	 */
1715df0566a6SJani Nikula 	config = &start->config[panel_type];
1716df0566a6SJani Nikula 	pps = &start->pps[panel_type];
1717df0566a6SJani Nikula 
1718df0566a6SJani Nikula 	/* store as of now full data. Trim when we realise all is not needed */
17193cf05076SVille Syrjälä 	panel->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
17203cf05076SVille Syrjälä 	if (!panel->vbt.dsi.config)
1721df0566a6SJani Nikula 		return;
1722df0566a6SJani Nikula 
17233cf05076SVille Syrjälä 	panel->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
17243cf05076SVille Syrjälä 	if (!panel->vbt.dsi.pps) {
17253cf05076SVille Syrjälä 		kfree(panel->vbt.dsi.config);
1726df0566a6SJani Nikula 		return;
1727df0566a6SJani Nikula 	}
1728df0566a6SJani Nikula 
17299aec6f76SJani Nikula 	parse_dsi_backlight_ports(display, panel, port);
1730df0566a6SJani Nikula 
1731df0566a6SJani Nikula 	/* FIXME is the 90 vs. 270 correct? */
1732df0566a6SJani Nikula 	switch (config->rotation) {
1733df0566a6SJani Nikula 	case ENABLE_ROTATION_0:
1734df0566a6SJani Nikula 		/*
1735df0566a6SJani Nikula 		 * Most (all?) VBTs claim 0 degrees despite having
1736df0566a6SJani Nikula 		 * an upside down panel, thus we do not trust this.
1737df0566a6SJani Nikula 		 */
17383cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1739df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1740df0566a6SJani Nikula 		break;
1741df0566a6SJani Nikula 	case ENABLE_ROTATION_90:
17423cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1743df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1744df0566a6SJani Nikula 		break;
1745df0566a6SJani Nikula 	case ENABLE_ROTATION_180:
17463cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1747df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1748df0566a6SJani Nikula 		break;
1749df0566a6SJani Nikula 	case ENABLE_ROTATION_270:
17503cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1751df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1752df0566a6SJani Nikula 		break;
1753df0566a6SJani Nikula 	}
1754df0566a6SJani Nikula 
1755df0566a6SJani Nikula 	/* We have mandatory mipi config blocks. Initialize as generic panel */
17563cf05076SVille Syrjälä 	panel->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
1757df0566a6SJani Nikula }
1758df0566a6SJani Nikula 
1759df0566a6SJani Nikula /* Find the sequence block and size for the given panel. */
1760df0566a6SJani Nikula static const u8 *
find_panel_sequence_block(struct intel_display * display,const struct bdb_mipi_sequence * sequence,u16 panel_id,u32 * seq_size)17619aec6f76SJani Nikula find_panel_sequence_block(struct intel_display *display,
1762ff9bc20cSVille Syrjälä 			  const struct bdb_mipi_sequence *sequence,
1763df0566a6SJani Nikula 			  u16 panel_id, u32 *seq_size)
1764df0566a6SJani Nikula {
1765df0566a6SJani Nikula 	u32 total = get_blocksize(sequence);
1766df0566a6SJani Nikula 	const u8 *data = &sequence->data[0];
1767df0566a6SJani Nikula 	u8 current_id;
1768df0566a6SJani Nikula 	u32 current_size;
1769df0566a6SJani Nikula 	int header_size = sequence->version >= 3 ? 5 : 3;
1770df0566a6SJani Nikula 	int index = 0;
1771df0566a6SJani Nikula 	int i;
1772df0566a6SJani Nikula 
1773df0566a6SJani Nikula 	/* skip new block size */
1774df0566a6SJani Nikula 	if (sequence->version >= 3)
1775df0566a6SJani Nikula 		data += 4;
1776df0566a6SJani Nikula 
1777df0566a6SJani Nikula 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
1778df0566a6SJani Nikula 		if (index + header_size > total) {
17799aec6f76SJani Nikula 			drm_err(display->drm,
17809aec6f76SJani Nikula 				"Invalid sequence block (header)\n");
1781df0566a6SJani Nikula 			return NULL;
1782df0566a6SJani Nikula 		}
1783df0566a6SJani Nikula 
1784df0566a6SJani Nikula 		current_id = *(data + index);
1785df0566a6SJani Nikula 		if (sequence->version >= 3)
1786df0566a6SJani Nikula 			current_size = *((const u32 *)(data + index + 1));
1787df0566a6SJani Nikula 		else
1788df0566a6SJani Nikula 			current_size = *((const u16 *)(data + index + 1));
1789df0566a6SJani Nikula 
1790df0566a6SJani Nikula 		index += header_size;
1791df0566a6SJani Nikula 
1792df0566a6SJani Nikula 		if (index + current_size > total) {
17939aec6f76SJani Nikula 			drm_err(display->drm, "Invalid sequence block\n");
1794df0566a6SJani Nikula 			return NULL;
1795df0566a6SJani Nikula 		}
1796df0566a6SJani Nikula 
1797df0566a6SJani Nikula 		if (current_id == panel_id) {
1798df0566a6SJani Nikula 			*seq_size = current_size;
1799df0566a6SJani Nikula 			return data + index;
1800df0566a6SJani Nikula 		}
1801df0566a6SJani Nikula 
1802df0566a6SJani Nikula 		index += current_size;
1803df0566a6SJani Nikula 	}
1804df0566a6SJani Nikula 
18059aec6f76SJani Nikula 	drm_err(display->drm,
18069aec6f76SJani Nikula 		"Sequence block detected but no valid configuration\n");
1807df0566a6SJani Nikula 
1808df0566a6SJani Nikula 	return NULL;
1809df0566a6SJani Nikula }
1810df0566a6SJani Nikula 
goto_next_sequence(struct intel_display * display,const u8 * data,int index,int total)18119aec6f76SJani Nikula static int goto_next_sequence(struct intel_display *display,
1812ff9bc20cSVille Syrjälä 			      const u8 *data, int index, int total)
1813df0566a6SJani Nikula {
1814df0566a6SJani Nikula 	u16 len;
1815df0566a6SJani Nikula 
1816df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1817df0566a6SJani Nikula 	for (index = index + 1; index < total; index += len) {
1818df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1819df0566a6SJani Nikula 		index++;
1820df0566a6SJani Nikula 
1821df0566a6SJani Nikula 		switch (operation_byte) {
1822df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_END:
1823df0566a6SJani Nikula 			return index;
1824df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1825df0566a6SJani Nikula 			if (index + 4 > total)
1826df0566a6SJani Nikula 				return 0;
1827df0566a6SJani Nikula 
1828df0566a6SJani Nikula 			len = *((const u16 *)(data + index + 2)) + 4;
1829df0566a6SJani Nikula 			break;
1830df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1831df0566a6SJani Nikula 			len = 4;
1832df0566a6SJani Nikula 			break;
1833df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1834df0566a6SJani Nikula 			len = 2;
1835df0566a6SJani Nikula 			break;
1836df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1837df0566a6SJani Nikula 			if (index + 7 > total)
1838df0566a6SJani Nikula 				return 0;
1839df0566a6SJani Nikula 			len = *(data + index + 6) + 7;
1840df0566a6SJani Nikula 			break;
1841df0566a6SJani Nikula 		default:
18429aec6f76SJani Nikula 			drm_err(display->drm, "Unknown operation byte\n");
1843df0566a6SJani Nikula 			return 0;
1844df0566a6SJani Nikula 		}
1845df0566a6SJani Nikula 	}
1846df0566a6SJani Nikula 
1847df0566a6SJani Nikula 	return 0;
1848df0566a6SJani Nikula }
1849df0566a6SJani Nikula 
goto_next_sequence_v3(struct intel_display * display,const u8 * data,int index,int total)18509aec6f76SJani Nikula static int goto_next_sequence_v3(struct intel_display *display,
1851ff9bc20cSVille Syrjälä 				 const u8 *data, int index, int total)
1852df0566a6SJani Nikula {
1853df0566a6SJani Nikula 	int seq_end;
1854df0566a6SJani Nikula 	u16 len;
1855df0566a6SJani Nikula 	u32 size_of_sequence;
1856df0566a6SJani Nikula 
1857df0566a6SJani Nikula 	/*
1858df0566a6SJani Nikula 	 * Could skip sequence based on Size of Sequence alone, but also do some
1859df0566a6SJani Nikula 	 * checking on the structure.
1860df0566a6SJani Nikula 	 */
1861df0566a6SJani Nikula 	if (total < 5) {
18629aec6f76SJani Nikula 		drm_err(display->drm, "Too small sequence size\n");
1863df0566a6SJani Nikula 		return 0;
1864df0566a6SJani Nikula 	}
1865df0566a6SJani Nikula 
1866df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1867df0566a6SJani Nikula 	index++;
1868df0566a6SJani Nikula 
1869df0566a6SJani Nikula 	/*
1870df0566a6SJani Nikula 	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
1871df0566a6SJani Nikula 	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
1872df0566a6SJani Nikula 	 * byte.
1873df0566a6SJani Nikula 	 */
1874df0566a6SJani Nikula 	size_of_sequence = *((const u32 *)(data + index));
1875df0566a6SJani Nikula 	index += 4;
1876df0566a6SJani Nikula 
1877df0566a6SJani Nikula 	seq_end = index + size_of_sequence;
1878df0566a6SJani Nikula 	if (seq_end > total) {
18799aec6f76SJani Nikula 		drm_err(display->drm, "Invalid sequence size\n");
1880df0566a6SJani Nikula 		return 0;
1881df0566a6SJani Nikula 	}
1882df0566a6SJani Nikula 
1883df0566a6SJani Nikula 	for (; index < total; index += len) {
1884df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1885df0566a6SJani Nikula 		index++;
1886df0566a6SJani Nikula 
1887df0566a6SJani Nikula 		if (operation_byte == MIPI_SEQ_ELEM_END) {
1888df0566a6SJani Nikula 			if (index != seq_end) {
18899aec6f76SJani Nikula 				drm_err(display->drm,
18909aec6f76SJani Nikula 					"Invalid element structure\n");
1891df0566a6SJani Nikula 				return 0;
1892df0566a6SJani Nikula 			}
1893df0566a6SJani Nikula 			return index;
1894df0566a6SJani Nikula 		}
1895df0566a6SJani Nikula 
1896df0566a6SJani Nikula 		len = *(data + index);
1897df0566a6SJani Nikula 		index++;
1898df0566a6SJani Nikula 
1899df0566a6SJani Nikula 		/*
1900df0566a6SJani Nikula 		 * FIXME: Would be nice to check elements like for v1/v2 in
1901df0566a6SJani Nikula 		 * goto_next_sequence() above.
1902df0566a6SJani Nikula 		 */
1903df0566a6SJani Nikula 		switch (operation_byte) {
1904df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1905df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1906df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1907df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1908df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SPI:
1909df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_PMIC:
1910df0566a6SJani Nikula 			break;
1911df0566a6SJani Nikula 		default:
19129aec6f76SJani Nikula 			drm_err(display->drm, "Unknown operation byte %u\n",
1913df0566a6SJani Nikula 				operation_byte);
1914df0566a6SJani Nikula 			break;
1915df0566a6SJani Nikula 		}
1916df0566a6SJani Nikula 	}
1917df0566a6SJani Nikula 
1918df0566a6SJani Nikula 	return 0;
1919df0566a6SJani Nikula }
1920df0566a6SJani Nikula 
1921df0566a6SJani Nikula /*
1922df0566a6SJani Nikula  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
1923df0566a6SJani Nikula  * skip all delay + gpio operands and stop at the first DSI packet op.
1924df0566a6SJani Nikula  */
get_init_otp_deassert_fragment_len(struct intel_display * display,struct intel_panel * panel)19259aec6f76SJani Nikula static int get_init_otp_deassert_fragment_len(struct intel_display *display,
19263cf05076SVille Syrjälä 					      struct intel_panel *panel)
1927df0566a6SJani Nikula {
19283cf05076SVille Syrjälä 	const u8 *data = panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1929df0566a6SJani Nikula 	int index, len;
1930df0566a6SJani Nikula 
19319aec6f76SJani Nikula 	if (drm_WARN_ON(display->drm,
19323cf05076SVille Syrjälä 			!data || panel->vbt.dsi.seq_version != 1))
1933df0566a6SJani Nikula 		return 0;
1934df0566a6SJani Nikula 
1935df0566a6SJani Nikula 	/* index = 1 to skip sequence byte */
1936df0566a6SJani Nikula 	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1937df0566a6SJani Nikula 		switch (data[index]) {
1938df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1939df0566a6SJani Nikula 			return index == 1 ? 0 : index;
1940df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1941df0566a6SJani Nikula 			len = 5; /* 1 byte for operand + uint32 */
1942df0566a6SJani Nikula 			break;
1943df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1944df0566a6SJani Nikula 			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1945df0566a6SJani Nikula 			break;
1946df0566a6SJani Nikula 		default:
1947df0566a6SJani Nikula 			return 0;
1948df0566a6SJani Nikula 		}
1949df0566a6SJani Nikula 	}
1950df0566a6SJani Nikula 
1951df0566a6SJani Nikula 	return 0;
1952df0566a6SJani Nikula }
1953df0566a6SJani Nikula 
1954df0566a6SJani Nikula /*
1955df0566a6SJani Nikula  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1956df0566a6SJani Nikula  * The deassert must be done before calling intel_dsi_device_ready, so for
1957df0566a6SJani Nikula  * these devices we split the init OTP sequence into a deassert sequence and
1958df0566a6SJani Nikula  * the actual init OTP part.
1959df0566a6SJani Nikula  */
vlv_fixup_mipi_sequences(struct intel_display * display,struct intel_panel * panel)19609aec6f76SJani Nikula static void vlv_fixup_mipi_sequences(struct intel_display *display,
19613cf05076SVille Syrjälä 				     struct intel_panel *panel)
1962df0566a6SJani Nikula {
1963df0566a6SJani Nikula 	u8 *init_otp;
1964df0566a6SJani Nikula 	int len;
1965df0566a6SJani Nikula 
1966df0566a6SJani Nikula 	/* Limit this to v1 vid-mode sequences */
19673cf05076SVille Syrjälä 	if (panel->vbt.dsi.config->is_cmd_mode ||
19683cf05076SVille Syrjälä 	    panel->vbt.dsi.seq_version != 1)
1969df0566a6SJani Nikula 		return;
1970df0566a6SJani Nikula 
1971df0566a6SJani Nikula 	/* Only do this if there are otp and assert seqs and no deassert seq */
19723cf05076SVille Syrjälä 	if (!panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
19733cf05076SVille Syrjälä 	    !panel->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
19743cf05076SVille Syrjälä 	    panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1975df0566a6SJani Nikula 		return;
1976df0566a6SJani Nikula 
1977df0566a6SJani Nikula 	/* The deassert-sequence ends at the first DSI packet */
19789aec6f76SJani Nikula 	len = get_init_otp_deassert_fragment_len(display, panel);
1979df0566a6SJani Nikula 	if (!len)
1980df0566a6SJani Nikula 		return;
1981df0566a6SJani Nikula 
19829aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
1983e92cbf38SWambui Karuga 		    "Using init OTP fragment to deassert reset\n");
1984df0566a6SJani Nikula 
1985df0566a6SJani Nikula 	/* Copy the fragment, update seq byte and terminate it */
19863cf05076SVille Syrjälä 	init_otp = (u8 *)panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
19873cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
19883cf05076SVille Syrjälä 	if (!panel->vbt.dsi.deassert_seq)
1989df0566a6SJani Nikula 		return;
19903cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
19913cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1992df0566a6SJani Nikula 	/* Use the copy for deassert */
19933cf05076SVille Syrjälä 	panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
19943cf05076SVille Syrjälä 		panel->vbt.dsi.deassert_seq;
1995df0566a6SJani Nikula 	/* Replace the last byte of the fragment with init OTP seq byte */
1996df0566a6SJani Nikula 	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1997df0566a6SJani Nikula 	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
19983cf05076SVille Syrjälä 	panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
1999df0566a6SJani Nikula }
2000df0566a6SJani Nikula 
200194ae4612SVille Syrjälä /*
200294ae4612SVille Syrjälä  * Some machines (eg. Lenovo 82TQ) appear to have broken
200394ae4612SVille Syrjälä  * VBT sequences:
200494ae4612SVille Syrjälä  * - INIT_OTP is not present at all
200594ae4612SVille Syrjälä  * - what should be in INIT_OTP is in DISPLAY_ON
200694ae4612SVille Syrjälä  * - what should be in DISPLAY_ON is in BACKLIGHT_ON
200794ae4612SVille Syrjälä  *   (along with the actual backlight stuff)
200894ae4612SVille Syrjälä  *
200994ae4612SVille Syrjälä  * To make those work we simply swap DISPLAY_ON and INIT_OTP.
201094ae4612SVille Syrjälä  *
201194ae4612SVille Syrjälä  * TODO: Do we need to limit this to specific machines,
201294ae4612SVille Syrjälä  *       or examine the contents of the sequences to
201394ae4612SVille Syrjälä  *       avoid false positives?
201494ae4612SVille Syrjälä  */
icl_fixup_mipi_sequences(struct intel_display * display,struct intel_panel * panel)20159aec6f76SJani Nikula static void icl_fixup_mipi_sequences(struct intel_display *display,
201694ae4612SVille Syrjälä 				     struct intel_panel *panel)
201794ae4612SVille Syrjälä {
201894ae4612SVille Syrjälä 	if (!panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] &&
201994ae4612SVille Syrjälä 	    panel->vbt.dsi.sequence[MIPI_SEQ_DISPLAY_ON]) {
20209aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
20219aec6f76SJani Nikula 			    "Broken VBT: Swapping INIT_OTP and DISPLAY_ON sequences\n");
202294ae4612SVille Syrjälä 
202394ae4612SVille Syrjälä 		swap(panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP],
202494ae4612SVille Syrjälä 		     panel->vbt.dsi.sequence[MIPI_SEQ_DISPLAY_ON]);
202594ae4612SVille Syrjälä 	}
202694ae4612SVille Syrjälä }
202794ae4612SVille Syrjälä 
fixup_mipi_sequences(struct intel_display * display,struct intel_panel * panel)20289aec6f76SJani Nikula static void fixup_mipi_sequences(struct intel_display *display,
202994ae4612SVille Syrjälä 				 struct intel_panel *panel)
203094ae4612SVille Syrjälä {
20319aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
20329aec6f76SJani Nikula 
20339aec6f76SJani Nikula 	if (DISPLAY_VER(display) >= 11)
20349aec6f76SJani Nikula 		icl_fixup_mipi_sequences(display, panel);
203594ae4612SVille Syrjälä 	else if (IS_VALLEYVIEW(i915))
20369aec6f76SJani Nikula 		vlv_fixup_mipi_sequences(display, panel);
203794ae4612SVille Syrjälä }
203894ae4612SVille Syrjälä 
2039df0566a6SJani Nikula static void
parse_mipi_sequence(struct intel_display * display,struct intel_panel * panel)20409aec6f76SJani Nikula parse_mipi_sequence(struct intel_display *display,
20413cf05076SVille Syrjälä 		    struct intel_panel *panel)
2042df0566a6SJani Nikula {
20433cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
2044df0566a6SJani Nikula 	const struct bdb_mipi_sequence *sequence;
2045df0566a6SJani Nikula 	const u8 *seq_data;
2046df0566a6SJani Nikula 	u32 seq_size;
2047df0566a6SJani Nikula 	u8 *data;
2048df0566a6SJani Nikula 	int index = 0;
2049df0566a6SJani Nikula 
2050df0566a6SJani Nikula 	/* Only our generic panel driver uses the sequence block. */
20513cf05076SVille Syrjälä 	if (panel->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
2052df0566a6SJani Nikula 		return;
2053df0566a6SJani Nikula 
20549aec6f76SJani Nikula 	sequence = bdb_find_section(display, BDB_MIPI_SEQUENCE);
2055df0566a6SJani Nikula 	if (!sequence) {
20569aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2057e92cbf38SWambui Karuga 			    "No MIPI Sequence found, parsing complete\n");
2058df0566a6SJani Nikula 		return;
2059df0566a6SJani Nikula 	}
2060df0566a6SJani Nikula 
2061df0566a6SJani Nikula 	/* Fail gracefully for forward incompatible sequence block. */
2062df0566a6SJani Nikula 	if (sequence->version >= 4) {
20639aec6f76SJani Nikula 		drm_err(display->drm,
2064e92cbf38SWambui Karuga 			"Unable to parse MIPI Sequence Block v%u\n",
2065df0566a6SJani Nikula 			sequence->version);
2066df0566a6SJani Nikula 		return;
2067df0566a6SJani Nikula 	}
2068df0566a6SJani Nikula 
20699aec6f76SJani Nikula 	drm_dbg(display->drm, "Found MIPI sequence block v%u\n",
2070e92cbf38SWambui Karuga 		sequence->version);
2071df0566a6SJani Nikula 
20729aec6f76SJani Nikula 	seq_data = find_panel_sequence_block(display, sequence, panel_type, &seq_size);
2073df0566a6SJani Nikula 	if (!seq_data)
2074df0566a6SJani Nikula 		return;
2075df0566a6SJani Nikula 
2076df0566a6SJani Nikula 	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
2077df0566a6SJani Nikula 	if (!data)
2078df0566a6SJani Nikula 		return;
2079df0566a6SJani Nikula 
2080df0566a6SJani Nikula 	/* Parse the sequences, store pointers to each sequence. */
2081df0566a6SJani Nikula 	for (;;) {
2082df0566a6SJani Nikula 		u8 seq_id = *(data + index);
2083df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_END)
2084df0566a6SJani Nikula 			break;
2085df0566a6SJani Nikula 
2086df0566a6SJani Nikula 		if (seq_id >= MIPI_SEQ_MAX) {
20879aec6f76SJani Nikula 			drm_err(display->drm, "Unknown sequence %u\n",
2088e92cbf38SWambui Karuga 				seq_id);
2089df0566a6SJani Nikula 			goto err;
2090df0566a6SJani Nikula 		}
2091df0566a6SJani Nikula 
2092df0566a6SJani Nikula 		/* Log about presence of sequences we won't run. */
2093df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
20949aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
2095e92cbf38SWambui Karuga 				    "Unsupported sequence %u\n", seq_id);
2096df0566a6SJani Nikula 
20973cf05076SVille Syrjälä 		panel->vbt.dsi.sequence[seq_id] = data + index;
2098df0566a6SJani Nikula 
2099df0566a6SJani Nikula 		if (sequence->version >= 3)
21009aec6f76SJani Nikula 			index = goto_next_sequence_v3(display, data, index, seq_size);
2101df0566a6SJani Nikula 		else
21029aec6f76SJani Nikula 			index = goto_next_sequence(display, data, index, seq_size);
2103df0566a6SJani Nikula 		if (!index) {
21049aec6f76SJani Nikula 			drm_err(display->drm, "Invalid sequence %u\n",
2105e92cbf38SWambui Karuga 				seq_id);
2106df0566a6SJani Nikula 			goto err;
2107df0566a6SJani Nikula 		}
2108df0566a6SJani Nikula 	}
2109df0566a6SJani Nikula 
21103cf05076SVille Syrjälä 	panel->vbt.dsi.data = data;
21113cf05076SVille Syrjälä 	panel->vbt.dsi.size = seq_size;
21123cf05076SVille Syrjälä 	panel->vbt.dsi.seq_version = sequence->version;
2113df0566a6SJani Nikula 
21149aec6f76SJani Nikula 	fixup_mipi_sequences(display, panel);
2115df0566a6SJani Nikula 
21169aec6f76SJani Nikula 	drm_dbg(display->drm, "MIPI related VBT parsing complete\n");
2117df0566a6SJani Nikula 	return;
2118df0566a6SJani Nikula 
2119df0566a6SJani Nikula err:
2120df0566a6SJani Nikula 	kfree(data);
21213cf05076SVille Syrjälä 	memset(panel->vbt.dsi.sequence, 0, sizeof(panel->vbt.dsi.sequence));
2122df0566a6SJani Nikula }
2123df0566a6SJani Nikula 
21246e0d46e9SJani Nikula static void
parse_compression_parameters(struct intel_display * display)21259aec6f76SJani Nikula parse_compression_parameters(struct intel_display *display)
21266e0d46e9SJani Nikula {
21276e0d46e9SJani Nikula 	const struct bdb_compression_parameters *params;
21283162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
21296e0d46e9SJani Nikula 	u16 block_size;
21306e0d46e9SJani Nikula 	int index;
21316e0d46e9SJani Nikula 
21329aec6f76SJani Nikula 	if (display->vbt.version < 198)
21336e0d46e9SJani Nikula 		return;
21346e0d46e9SJani Nikula 
21359aec6f76SJani Nikula 	params = bdb_find_section(display, BDB_COMPRESSION_PARAMETERS);
21366e0d46e9SJani Nikula 	if (params) {
21376e0d46e9SJani Nikula 		/* Sanity checks */
21386e0d46e9SJani Nikula 		if (params->entry_size != sizeof(params->data[0])) {
21399aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
2140e92cbf38SWambui Karuga 				    "VBT: unsupported compression param entry size\n");
21416e0d46e9SJani Nikula 			return;
21426e0d46e9SJani Nikula 		}
21436e0d46e9SJani Nikula 
21446e0d46e9SJani Nikula 		block_size = get_blocksize(params);
21456e0d46e9SJani Nikula 		if (block_size < sizeof(*params)) {
21469aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
2147e92cbf38SWambui Karuga 				    "VBT: expected 16 compression param entries\n");
21486e0d46e9SJani Nikula 			return;
21496e0d46e9SJani Nikula 		}
21506e0d46e9SJani Nikula 	}
21516e0d46e9SJani Nikula 
21529aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
2153d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
21546e0d46e9SJani Nikula 
21556e0d46e9SJani Nikula 		if (!child->compression_enable)
21566e0d46e9SJani Nikula 			continue;
21576e0d46e9SJani Nikula 
21586e0d46e9SJani Nikula 		if (!params) {
21599aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
2160e92cbf38SWambui Karuga 				    "VBT: compression params not available\n");
21616e0d46e9SJani Nikula 			continue;
21626e0d46e9SJani Nikula 		}
21636e0d46e9SJani Nikula 
21646e0d46e9SJani Nikula 		if (child->compression_method_cps) {
21659aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
2166e92cbf38SWambui Karuga 				    "VBT: CPS compression not supported\n");
21676e0d46e9SJani Nikula 			continue;
21686e0d46e9SJani Nikula 		}
21696e0d46e9SJani Nikula 
21706e0d46e9SJani Nikula 		index = child->compression_structure_index;
21716e0d46e9SJani Nikula 
21726e0d46e9SJani Nikula 		devdata->dsc = kmemdup(&params->data[index],
21736e0d46e9SJani Nikula 				       sizeof(*devdata->dsc), GFP_KERNEL);
21746e0d46e9SJani Nikula 	}
21756e0d46e9SJani Nikula }
21766e0d46e9SJani Nikula 
translate_iboost(struct intel_display * display,u8 val)21779aec6f76SJani Nikula static u8 translate_iboost(struct intel_display *display, u8 val)
2178df0566a6SJani Nikula {
2179df0566a6SJani Nikula 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
2180df0566a6SJani Nikula 
2181df0566a6SJani Nikula 	if (val >= ARRAY_SIZE(mapping)) {
21829aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2183ff9bc20cSVille Syrjälä 			    "Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
2184df0566a6SJani Nikula 		return 0;
2185df0566a6SJani Nikula 	}
2186df0566a6SJani Nikula 	return mapping[val];
2187df0566a6SJani Nikula }
2188df0566a6SJani Nikula 
21899e1dbc1aSJani Nikula static const u8 cnp_ddc_pin_map[] = {
21909e1dbc1aSJani Nikula 	[0] = 0, /* N/A */
21913d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = DDC_BUS_DDI_B,
21923d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = DDC_BUS_DDI_C,
21933d7af6cfSVille Syrjälä 	[GMBUS_PIN_4_CNP] = DDC_BUS_DDI_D, /* sic */
21943d7af6cfSVille Syrjälä 	[GMBUS_PIN_3_BXT] = DDC_BUS_DDI_F, /* sic */
21959e1dbc1aSJani Nikula };
21969e1dbc1aSJani Nikula 
21979e1dbc1aSJani Nikula static const u8 icp_ddc_pin_map[] = {
21983d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
21993d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
22003d7af6cfSVille Syrjälä 	[GMBUS_PIN_3_BXT] = TGL_DDC_BUS_DDI_C,
22013d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = ICL_DDC_BUS_PORT_1,
22023d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = ICL_DDC_BUS_PORT_2,
22033d7af6cfSVille Syrjälä 	[GMBUS_PIN_11_TC3_ICP] = ICL_DDC_BUS_PORT_3,
22043d7af6cfSVille Syrjälä 	[GMBUS_PIN_12_TC4_ICP] = ICL_DDC_BUS_PORT_4,
22053d7af6cfSVille Syrjälä 	[GMBUS_PIN_13_TC5_TGP] = TGL_DDC_BUS_PORT_5,
22063d7af6cfSVille Syrjälä 	[GMBUS_PIN_14_TC6_TGP] = TGL_DDC_BUS_PORT_6,
22079e1dbc1aSJani Nikula };
22089e1dbc1aSJani Nikula 
22099e1dbc1aSJani Nikula static const u8 rkl_pch_tgp_ddc_pin_map[] = {
22103d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
22113d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
22123d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = RKL_DDC_BUS_DDI_D,
22133d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = RKL_DDC_BUS_DDI_E,
22149e1dbc1aSJani Nikula };
22159e1dbc1aSJani Nikula 
22169e1dbc1aSJani Nikula static const u8 adls_ddc_pin_map[] = {
22173d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
22183d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = ADLS_DDC_BUS_PORT_TC1,
22193d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = ADLS_DDC_BUS_PORT_TC2,
22203d7af6cfSVille Syrjälä 	[GMBUS_PIN_11_TC3_ICP] = ADLS_DDC_BUS_PORT_TC3,
22213d7af6cfSVille Syrjälä 	[GMBUS_PIN_12_TC4_ICP] = ADLS_DDC_BUS_PORT_TC4,
22229e1dbc1aSJani Nikula };
22239e1dbc1aSJani Nikula 
22249e1dbc1aSJani Nikula static const u8 gen9bc_tgp_ddc_pin_map[] = {
22253d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = DDC_BUS_DDI_B,
22263d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = DDC_BUS_DDI_C,
22273d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = DDC_BUS_DDI_D,
22289e1dbc1aSJani Nikula };
22299e1dbc1aSJani Nikula 
2230af10ec31STejas Upadhyay static const u8 adlp_ddc_pin_map[] = {
22313d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
22323d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
22333d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = ADLP_DDC_BUS_PORT_TC1,
22343d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = ADLP_DDC_BUS_PORT_TC2,
22353d7af6cfSVille Syrjälä 	[GMBUS_PIN_11_TC3_ICP] = ADLP_DDC_BUS_PORT_TC3,
22363d7af6cfSVille Syrjälä 	[GMBUS_PIN_12_TC4_ICP] = ADLP_DDC_BUS_PORT_TC4,
2237af10ec31STejas Upadhyay };
2238af10ec31STejas Upadhyay 
map_ddc_pin(struct intel_display * display,u8 vbt_pin)22399aec6f76SJani Nikula static u8 map_ddc_pin(struct intel_display *display, u8 vbt_pin)
22409e1dbc1aSJani Nikula {
22419aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
22429e1dbc1aSJani Nikula 	const u8 *ddc_pin_map;
22433d7af6cfSVille Syrjälä 	int i, n_entries;
22449e1dbc1aSJani Nikula 
224593cbc1acSHaridhar Kalvala 	if (INTEL_PCH_TYPE(i915) >= PCH_MTL || IS_ALDERLAKE_P(i915)) {
2246af10ec31STejas Upadhyay 		ddc_pin_map = adlp_ddc_pin_map;
2247af10ec31STejas Upadhyay 		n_entries = ARRAY_SIZE(adlp_ddc_pin_map);
2248af10ec31STejas Upadhyay 	} else if (IS_ALDERLAKE_S(i915)) {
22499e1dbc1aSJani Nikula 		ddc_pin_map = adls_ddc_pin_map;
22509e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(adls_ddc_pin_map);
2251c528aaa3SAnkit Nautiyal 	} else if (INTEL_PCH_TYPE(i915) >= PCH_DG1) {
2252c528aaa3SAnkit Nautiyal 		return vbt_pin;
22539e1dbc1aSJani Nikula 	} else if (IS_ROCKETLAKE(i915) && INTEL_PCH_TYPE(i915) == PCH_TGP) {
22549e1dbc1aSJani Nikula 		ddc_pin_map = rkl_pch_tgp_ddc_pin_map;
22559e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(rkl_pch_tgp_ddc_pin_map);
22569aec6f76SJani Nikula 	} else if (HAS_PCH_TGP(i915) && DISPLAY_VER(display) == 9) {
22579e1dbc1aSJani Nikula 		ddc_pin_map = gen9bc_tgp_ddc_pin_map;
22589e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(gen9bc_tgp_ddc_pin_map);
22599e1dbc1aSJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_ICP) {
22609e1dbc1aSJani Nikula 		ddc_pin_map = icp_ddc_pin_map;
22619e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
22629e1dbc1aSJani Nikula 	} else if (HAS_PCH_CNP(i915)) {
22639e1dbc1aSJani Nikula 		ddc_pin_map = cnp_ddc_pin_map;
22649e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
22659e1dbc1aSJani Nikula 	} else {
22669e1dbc1aSJani Nikula 		/* Assuming direct map */
22679e1dbc1aSJani Nikula 		return vbt_pin;
22689e1dbc1aSJani Nikula 	}
22699e1dbc1aSJani Nikula 
22703d7af6cfSVille Syrjälä 	for (i = 0; i < n_entries; i++) {
22713d7af6cfSVille Syrjälä 		if (ddc_pin_map[i] == vbt_pin)
22723d7af6cfSVille Syrjälä 			return i;
22733d7af6cfSVille Syrjälä 	}
22749e1dbc1aSJani Nikula 
22759aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
22769e1dbc1aSJani Nikula 		    "Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
22779e1dbc1aSJani Nikula 		    vbt_pin);
22789e1dbc1aSJani Nikula 	return 0;
22799e1dbc1aSJani Nikula }
22809e1dbc1aSJani Nikula 
dvo_port_type(u8 dvo_port)228132c2bc89SVille Syrjälä static u8 dvo_port_type(u8 dvo_port)
228232c2bc89SVille Syrjälä {
228332c2bc89SVille Syrjälä 	switch (dvo_port) {
228432c2bc89SVille Syrjälä 	case DVO_PORT_HDMIA:
228532c2bc89SVille Syrjälä 	case DVO_PORT_HDMIB:
228632c2bc89SVille Syrjälä 	case DVO_PORT_HDMIC:
228732c2bc89SVille Syrjälä 	case DVO_PORT_HDMID:
228832c2bc89SVille Syrjälä 	case DVO_PORT_HDMIE:
228932c2bc89SVille Syrjälä 	case DVO_PORT_HDMIF:
229032c2bc89SVille Syrjälä 	case DVO_PORT_HDMIG:
229132c2bc89SVille Syrjälä 	case DVO_PORT_HDMIH:
229232c2bc89SVille Syrjälä 	case DVO_PORT_HDMII:
229332c2bc89SVille Syrjälä 		return DVO_PORT_HDMIA;
229432c2bc89SVille Syrjälä 	case DVO_PORT_DPA:
229532c2bc89SVille Syrjälä 	case DVO_PORT_DPB:
229632c2bc89SVille Syrjälä 	case DVO_PORT_DPC:
229732c2bc89SVille Syrjälä 	case DVO_PORT_DPD:
229832c2bc89SVille Syrjälä 	case DVO_PORT_DPE:
229932c2bc89SVille Syrjälä 	case DVO_PORT_DPF:
230032c2bc89SVille Syrjälä 	case DVO_PORT_DPG:
230132c2bc89SVille Syrjälä 	case DVO_PORT_DPH:
230232c2bc89SVille Syrjälä 	case DVO_PORT_DPI:
230332c2bc89SVille Syrjälä 		return DVO_PORT_DPA;
230432c2bc89SVille Syrjälä 	case DVO_PORT_MIPIA:
230532c2bc89SVille Syrjälä 	case DVO_PORT_MIPIB:
230632c2bc89SVille Syrjälä 	case DVO_PORT_MIPIC:
230732c2bc89SVille Syrjälä 	case DVO_PORT_MIPID:
230832c2bc89SVille Syrjälä 		return DVO_PORT_MIPIA;
230932c2bc89SVille Syrjälä 	default:
231032c2bc89SVille Syrjälä 		return dvo_port;
231132c2bc89SVille Syrjälä 	}
231232c2bc89SVille Syrjälä }
231332c2bc89SVille Syrjälä 
__dvo_port_to_port(int n_ports,int n_dvo,const int port_mapping[][3],u8 dvo_port)23144628142aSLucas De Marchi static enum port __dvo_port_to_port(int n_ports, int n_dvo,
23154628142aSLucas De Marchi 				    const int port_mapping[][3], u8 dvo_port)
2316df0566a6SJani Nikula {
2317df0566a6SJani Nikula 	enum port port;
2318df0566a6SJani Nikula 	int i;
2319df0566a6SJani Nikula 
23204628142aSLucas De Marchi 	for (port = PORT_A; port < n_ports; port++) {
23214628142aSLucas De Marchi 		for (i = 0; i < n_dvo; i++) {
23224628142aSLucas De Marchi 			if (port_mapping[port][i] == -1)
2323df0566a6SJani Nikula 				break;
2324df0566a6SJani Nikula 
23254628142aSLucas De Marchi 			if (dvo_port == port_mapping[port][i])
2326df0566a6SJani Nikula 				return port;
2327df0566a6SJani Nikula 		}
2328df0566a6SJani Nikula 	}
2329df0566a6SJani Nikula 
2330df0566a6SJani Nikula 	return PORT_NONE;
2331df0566a6SJani Nikula }
2332df0566a6SJani Nikula 
dvo_port_to_port(struct intel_display * display,u8 dvo_port)23339aec6f76SJani Nikula static enum port dvo_port_to_port(struct intel_display *display,
23344628142aSLucas De Marchi 				  u8 dvo_port)
23354628142aSLucas De Marchi {
23369aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
23374628142aSLucas De Marchi 	/*
23384628142aSLucas De Marchi 	 * Each DDI port can have more than one value on the "DVO Port" field,
23394628142aSLucas De Marchi 	 * so look for all the possible values for each port.
23404628142aSLucas De Marchi 	 */
23414628142aSLucas De Marchi 	static const int port_mapping[][3] = {
23424628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
23434628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
23444628142aSLucas De Marchi 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
23454628142aSLucas De Marchi 		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
23468c1a8f12SMatt Roper 		[PORT_E] = { DVO_PORT_HDMIE, DVO_PORT_DPE, DVO_PORT_CRT },
23474628142aSLucas De Marchi 		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
23484628142aSLucas De Marchi 		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2349176430ccSVille Syrjälä 		[PORT_H] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2350176430ccSVille Syrjälä 		[PORT_I] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
23514628142aSLucas De Marchi 	};
23524628142aSLucas De Marchi 	/*
23531d8ca002SVille Syrjälä 	 * RKL VBT uses PHY based mapping. Combo PHYs A,B,C,D
23541d8ca002SVille Syrjälä 	 * map to DDI A,B,TC1,TC2 respectively.
23554628142aSLucas De Marchi 	 */
23564628142aSLucas De Marchi 	static const int rkl_port_mapping[][3] = {
23574628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
23584628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
23594628142aSLucas De Marchi 		[PORT_C] = { -1 },
23601d8ca002SVille Syrjälä 		[PORT_TC1] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
23611d8ca002SVille Syrjälä 		[PORT_TC2] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
23624628142aSLucas De Marchi 	};
236318c283dfSAditya Swarup 	/*
236418c283dfSAditya Swarup 	 * Alderlake S ports used in the driver are PORT_A, PORT_D, PORT_E,
236518c283dfSAditya Swarup 	 * PORT_F and PORT_G, we need to map that to correct VBT sections.
236618c283dfSAditya Swarup 	 */
236718c283dfSAditya Swarup 	static const int adls_port_mapping[][3] = {
236818c283dfSAditya Swarup 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
236918c283dfSAditya Swarup 		[PORT_B] = { -1 },
237018c283dfSAditya Swarup 		[PORT_C] = { -1 },
237118c283dfSAditya Swarup 		[PORT_TC1] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
237218c283dfSAditya Swarup 		[PORT_TC2] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
237318c283dfSAditya Swarup 		[PORT_TC3] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
237418c283dfSAditya Swarup 		[PORT_TC4] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
237518c283dfSAditya Swarup 	};
2376eeb63c54SJosé Roberto de Souza 	static const int xelpd_port_mapping[][3] = {
2377eeb63c54SJosé Roberto de Souza 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2378eeb63c54SJosé Roberto de Souza 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2379eeb63c54SJosé Roberto de Souza 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2380eeb63c54SJosé Roberto de Souza 		[PORT_D_XELPD] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2381eeb63c54SJosé Roberto de Souza 		[PORT_E_XELPD] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
2382eeb63c54SJosé Roberto de Souza 		[PORT_TC1] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
2383eeb63c54SJosé Roberto de Souza 		[PORT_TC2] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2384eeb63c54SJosé Roberto de Souza 		[PORT_TC3] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2385eeb63c54SJosé Roberto de Souza 		[PORT_TC4] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
2386eeb63c54SJosé Roberto de Souza 	};
23874628142aSLucas De Marchi 
23889aec6f76SJani Nikula 	if (DISPLAY_VER(display) >= 13)
2389eeb63c54SJosé Roberto de Souza 		return __dvo_port_to_port(ARRAY_SIZE(xelpd_port_mapping),
2390eeb63c54SJosé Roberto de Souza 					  ARRAY_SIZE(xelpd_port_mapping[0]),
2391eeb63c54SJosé Roberto de Souza 					  xelpd_port_mapping,
2392eeb63c54SJosé Roberto de Souza 					  dvo_port);
2393eeb63c54SJosé Roberto de Souza 	else if (IS_ALDERLAKE_S(i915))
239418c283dfSAditya Swarup 		return __dvo_port_to_port(ARRAY_SIZE(adls_port_mapping),
239518c283dfSAditya Swarup 					  ARRAY_SIZE(adls_port_mapping[0]),
239618c283dfSAditya Swarup 					  adls_port_mapping,
239718c283dfSAditya Swarup 					  dvo_port);
2398dbd440d8SJani Nikula 	else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
23994628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(rkl_port_mapping),
24004628142aSLucas De Marchi 					  ARRAY_SIZE(rkl_port_mapping[0]),
24014628142aSLucas De Marchi 					  rkl_port_mapping,
24024628142aSLucas De Marchi 					  dvo_port);
24034628142aSLucas De Marchi 	else
24044628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(port_mapping),
24054628142aSLucas De Marchi 					  ARRAY_SIZE(port_mapping[0]),
24064628142aSLucas De Marchi 					  port_mapping,
24074628142aSLucas De Marchi 					  dvo_port);
24084628142aSLucas De Marchi }
24094628142aSLucas De Marchi 
2410118b5c13SVille Syrjälä static enum port
dsi_dvo_port_to_port(struct intel_display * display,u8 dvo_port)24119aec6f76SJani Nikula dsi_dvo_port_to_port(struct intel_display *display, u8 dvo_port)
2412118b5c13SVille Syrjälä {
2413118b5c13SVille Syrjälä 	switch (dvo_port) {
2414118b5c13SVille Syrjälä 	case DVO_PORT_MIPIA:
2415118b5c13SVille Syrjälä 		return PORT_A;
2416118b5c13SVille Syrjälä 	case DVO_PORT_MIPIC:
24179aec6f76SJani Nikula 		if (DISPLAY_VER(display) >= 11)
2418118b5c13SVille Syrjälä 			return PORT_B;
2419118b5c13SVille Syrjälä 		else
2420118b5c13SVille Syrjälä 			return PORT_C;
2421118b5c13SVille Syrjälä 	default:
2422118b5c13SVille Syrjälä 		return PORT_NONE;
2423118b5c13SVille Syrjälä 	}
2424118b5c13SVille Syrjälä }
2425118b5c13SVille Syrjälä 
intel_bios_encoder_port(const struct intel_bios_encoder_data * devdata)2426021a62a5SVille Syrjälä enum port intel_bios_encoder_port(const struct intel_bios_encoder_data *devdata)
2427d84b1945SVille Syrjälä {
24289aec6f76SJani Nikula 	struct intel_display *display = devdata->display;
2429d84b1945SVille Syrjälä 	const struct child_device_config *child = &devdata->child;
2430d84b1945SVille Syrjälä 	enum port port;
2431d84b1945SVille Syrjälä 
24329aec6f76SJani Nikula 	port = dvo_port_to_port(display, child->dvo_port);
24339aec6f76SJani Nikula 	if (port == PORT_NONE && DISPLAY_VER(display) >= 11)
24349aec6f76SJani Nikula 		port = dsi_dvo_port_to_port(display, child->dvo_port);
2435d84b1945SVille Syrjälä 
2436d84b1945SVille Syrjälä 	return port;
2437d84b1945SVille Syrjälä }
2438d84b1945SVille Syrjälä 
parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)2439b60e320bSLee Shawn C static int parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)
2440b60e320bSLee Shawn C {
2441b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
2442b60e320bSLee Shawn C 	default:
2443b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_DEF:
2444b60e320bSLee Shawn C 		return 0;
2445b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR20:
2446b60e320bSLee Shawn C 		return 2000000;
2447b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR13P5:
2448b60e320bSLee Shawn C 		return 1350000;
2449b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR10:
2450b60e320bSLee Shawn C 		return 1000000;
2451b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR3:
2452b60e320bSLee Shawn C 		return 810000;
2453b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR2:
2454b60e320bSLee Shawn C 		return 540000;
2455b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR:
2456b60e320bSLee Shawn C 		return 270000;
2457b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_LBR:
2458b60e320bSLee Shawn C 		return 162000;
2459b60e320bSLee Shawn C 	}
2460b60e320bSLee Shawn C }
2461b60e320bSLee Shawn C 
parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)2462b60e320bSLee Shawn C static int parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)
2463b60e320bSLee Shawn C {
2464b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
2465b60e320bSLee Shawn C 	default:
2466b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR3:
2467b60e320bSLee Shawn C 		return 810000;
2468b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR2:
2469b60e320bSLee Shawn C 		return 540000;
2470b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR:
2471b60e320bSLee Shawn C 		return 270000;
2472b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_LBR:
2473b60e320bSLee Shawn C 		return 162000;
2474b60e320bSLee Shawn C 	}
2475b60e320bSLee Shawn C }
2476b60e320bSLee Shawn C 
intel_bios_dp_max_link_rate(const struct intel_bios_encoder_data * devdata)247702107ef1SVille Syrjälä int intel_bios_dp_max_link_rate(const struct intel_bios_encoder_data *devdata)
247872337aacSJani Nikula {
24799aec6f76SJani Nikula 	if (!devdata || devdata->display->vbt.version < 216)
248072337aacSJani Nikula 		return 0;
248172337aacSJani Nikula 
24829aec6f76SJani Nikula 	if (devdata->display->vbt.version >= 230)
248372337aacSJani Nikula 		return parse_bdb_230_dp_max_link_rate(devdata->child.dp_max_link_rate);
248472337aacSJani Nikula 	else
248572337aacSJani Nikula 		return parse_bdb_216_dp_max_link_rate(devdata->child.dp_max_link_rate);
248672337aacSJani Nikula }
248772337aacSJani Nikula 
intel_bios_dp_max_lane_count(const struct intel_bios_encoder_data * devdata)248802107ef1SVille Syrjälä int intel_bios_dp_max_lane_count(const struct intel_bios_encoder_data *devdata)
24894182a311SVille Syrjälä {
24909aec6f76SJani Nikula 	if (!devdata || devdata->display->vbt.version < 244)
24914182a311SVille Syrjälä 		return 0;
24924182a311SVille Syrjälä 
24934182a311SVille Syrjälä 	return devdata->child.dp_max_lane_count + 1;
24944182a311SVille Syrjälä }
24954182a311SVille Syrjälä 
sanitize_device_type(struct intel_bios_encoder_data * devdata,enum port port)2496d0ab409dSJani Nikula static void sanitize_device_type(struct intel_bios_encoder_data *devdata,
2497d0ab409dSJani Nikula 				 enum port port)
2498d0ab409dSJani Nikula {
24999aec6f76SJani Nikula 	struct intel_display *display = devdata->display;
2500d0ab409dSJani Nikula 	bool is_hdmi;
2501d0ab409dSJani Nikula 
25029aec6f76SJani Nikula 	if (port != PORT_A || DISPLAY_VER(display) >= 12)
2503d0ab409dSJani Nikula 		return;
2504d0ab409dSJani Nikula 
250586996822SJani Nikula 	if (!intel_bios_encoder_supports_dvi(devdata))
2506d0ab409dSJani Nikula 		return;
2507d0ab409dSJani Nikula 
250886996822SJani Nikula 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2509d0ab409dSJani Nikula 
25109aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "VBT claims port A supports DVI%s, ignoring\n",
2511d0ab409dSJani Nikula 		    is_hdmi ? "/HDMI" : "");
2512d0ab409dSJani Nikula 
2513d0ab409dSJani Nikula 	devdata->child.device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
2514d0ab409dSJani Nikula 	devdata->child.device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
2515d0ab409dSJani Nikula }
2516d0ab409dSJani Nikula 
sanitize_hdmi_level_shift(struct intel_bios_encoder_data * devdata,enum port port)25179e372744SVille Syrjälä static void sanitize_hdmi_level_shift(struct intel_bios_encoder_data *devdata,
25189e372744SVille Syrjälä 				      enum port port)
25199e372744SVille Syrjälä {
25209aec6f76SJani Nikula 	struct intel_display *display = devdata->display;
25219aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
25229e372744SVille Syrjälä 
25239e372744SVille Syrjälä 	if (!intel_bios_encoder_supports_dvi(devdata))
25249e372744SVille Syrjälä 		return;
25259e372744SVille Syrjälä 
25269e372744SVille Syrjälä 	/*
25279e372744SVille Syrjälä 	 * Some BDW machines (eg. HP Pavilion 15-ab) shipped
25289e372744SVille Syrjälä 	 * with a HSW VBT where the level shifter value goes
25299e372744SVille Syrjälä 	 * up to 11, whereas the BDW max is 9.
25309e372744SVille Syrjälä 	 */
25319e372744SVille Syrjälä 	if (IS_BROADWELL(i915) && devdata->child.hdmi_level_shifter_value > 9) {
25329aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
25339aec6f76SJani Nikula 			    "Bogus port %c VBT HDMI level shift %d, adjusting to %d\n",
25349e372744SVille Syrjälä 			    port_name(port), devdata->child.hdmi_level_shifter_value, 9);
25359e372744SVille Syrjälä 
25369e372744SVille Syrjälä 		devdata->child.hdmi_level_shifter_value = 9;
25379e372744SVille Syrjälä 	}
25389e372744SVille Syrjälä }
25399e372744SVille Syrjälä 
2540d0ab409dSJani Nikula static bool
intel_bios_encoder_supports_crt(const struct intel_bios_encoder_data * devdata)2541d0ab409dSJani Nikula intel_bios_encoder_supports_crt(const struct intel_bios_encoder_data *devdata)
2542d0ab409dSJani Nikula {
2543d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
2544d0ab409dSJani Nikula }
2545d0ab409dSJani Nikula 
254645c0673aSJani Nikula bool
intel_bios_encoder_supports_dvi(const struct intel_bios_encoder_data * devdata)2547d0ab409dSJani Nikula intel_bios_encoder_supports_dvi(const struct intel_bios_encoder_data *devdata)
2548d0ab409dSJani Nikula {
2549d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
2550d0ab409dSJani Nikula }
2551d0ab409dSJani Nikula 
255245c0673aSJani Nikula bool
intel_bios_encoder_supports_hdmi(const struct intel_bios_encoder_data * devdata)2553d0ab409dSJani Nikula intel_bios_encoder_supports_hdmi(const struct intel_bios_encoder_data *devdata)
2554d0ab409dSJani Nikula {
2555d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dvi(devdata) &&
2556d0ab409dSJani Nikula 		(devdata->child.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
2557d0ab409dSJani Nikula }
2558d0ab409dSJani Nikula 
255945c0673aSJani Nikula bool
intel_bios_encoder_supports_dp(const struct intel_bios_encoder_data * devdata)2560d0ab409dSJani Nikula intel_bios_encoder_supports_dp(const struct intel_bios_encoder_data *devdata)
2561d0ab409dSJani Nikula {
2562d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2563d0ab409dSJani Nikula }
2564d0ab409dSJani Nikula 
25659d4b7af5SVille Syrjälä bool
intel_bios_encoder_supports_edp(const struct intel_bios_encoder_data * devdata)2566d0ab409dSJani Nikula intel_bios_encoder_supports_edp(const struct intel_bios_encoder_data *devdata)
2567d0ab409dSJani Nikula {
2568d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dp(devdata) &&
2569d0ab409dSJani Nikula 		devdata->child.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR;
2570d0ab409dSJani Nikula }
2571d0ab409dSJani Nikula 
2572021a62a5SVille Syrjälä bool
intel_bios_encoder_supports_dsi(const struct intel_bios_encoder_data * devdata)2573ba00eb6aSVille Syrjälä intel_bios_encoder_supports_dsi(const struct intel_bios_encoder_data *devdata)
2574ba00eb6aSVille Syrjälä {
2575ba00eb6aSVille Syrjälä 	return devdata->child.device_type & DEVICE_TYPE_MIPI_OUTPUT;
2576ba00eb6aSVille Syrjälä }
2577ba00eb6aSVille Syrjälä 
2578db5d650fSVille Syrjälä bool
intel_bios_encoder_is_lspcon(const struct intel_bios_encoder_data * devdata)2579db5d650fSVille Syrjälä intel_bios_encoder_is_lspcon(const struct intel_bios_encoder_data *devdata)
2580db5d650fSVille Syrjälä {
25819aec6f76SJani Nikula 	return devdata && HAS_LSPCON(devdata->display) && devdata->child.lspcon;
2582db5d650fSVille Syrjälä }
2583db5d650fSVille Syrjälä 
258402107ef1SVille Syrjälä /* This is an index in the HDMI/DVI DDI buffer translation table, or -1 */
intel_bios_hdmi_level_shift(const struct intel_bios_encoder_data * devdata)258502107ef1SVille Syrjälä int intel_bios_hdmi_level_shift(const struct intel_bios_encoder_data *devdata)
2586a9a56e76SJani Nikula {
25879aec6f76SJani Nikula 	if (!devdata || devdata->display->vbt.version < 158 ||
25889aec6f76SJani Nikula 	    DISPLAY_VER(devdata->display) >= 14)
2589a9a56e76SJani Nikula 		return -1;
2590a9a56e76SJani Nikula 
2591a9a56e76SJani Nikula 	return devdata->child.hdmi_level_shifter_value;
2592a9a56e76SJani Nikula }
2593a9a56e76SJani Nikula 
intel_bios_hdmi_max_tmds_clock(const struct intel_bios_encoder_data * devdata)259402107ef1SVille Syrjälä int intel_bios_hdmi_max_tmds_clock(const struct intel_bios_encoder_data *devdata)
25956ba69981SJani Nikula {
25969aec6f76SJani Nikula 	if (!devdata || devdata->display->vbt.version < 204)
25976ba69981SJani Nikula 		return 0;
25986ba69981SJani Nikula 
25996ba69981SJani Nikula 	switch (devdata->child.hdmi_max_data_rate) {
26006ba69981SJani Nikula 	default:
26016ba69981SJani Nikula 		MISSING_CASE(devdata->child.hdmi_max_data_rate);
26026ba69981SJani Nikula 		fallthrough;
26036ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_PLATFORM:
26046ba69981SJani Nikula 		return 0;
26055708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_594:
26065708fe0dSLee Shawn C 		return 594000;
26075708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_340:
26085708fe0dSLee Shawn C 		return 340000;
26095708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_300:
26105708fe0dSLee Shawn C 		return 300000;
26116ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_297:
26126ba69981SJani Nikula 		return 297000;
26136ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_165:
26146ba69981SJani Nikula 		return 165000;
26156ba69981SJani Nikula 	}
26166ba69981SJani Nikula }
26176ba69981SJani Nikula 
is_port_valid(struct intel_display * display,enum port port)26189aec6f76SJani Nikula static bool is_port_valid(struct intel_display *display, enum port port)
26195a9d38b2SLucas De Marchi {
26209aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
26215a9d38b2SLucas De Marchi 	/*
2622cad83b40SLucas De Marchi 	 * On some ICL SKUs port F is not present, but broken VBTs mark
26235a9d38b2SLucas De Marchi 	 * the port as present. Only try to initialize port F for the
26245a9d38b2SLucas De Marchi 	 * SKUs that may actually have it.
26255a9d38b2SLucas De Marchi 	 */
2626cad83b40SLucas De Marchi 	if (port == PORT_F && IS_ICELAKE(i915))
2627cad83b40SLucas De Marchi 		return IS_ICL_WITH_PORT_F(i915);
26285a9d38b2SLucas De Marchi 
26295a9d38b2SLucas De Marchi 	return true;
26305a9d38b2SLucas De Marchi }
26315a9d38b2SLucas De Marchi 
print_ddi_port(const struct intel_bios_encoder_data * devdata)2632021a62a5SVille Syrjälä static void print_ddi_port(const struct intel_bios_encoder_data *devdata)
2633df0566a6SJani Nikula {
26349aec6f76SJani Nikula 	struct intel_display *display = devdata->display;
2635d1dad6f4SJani Nikula 	const struct child_device_config *child = &devdata->child;
2636ba00eb6aSVille Syrjälä 	bool is_dvi, is_hdmi, is_dp, is_edp, is_dsi, is_crt, supports_typec_usb, supports_tbt;
263772337aacSJani Nikula 	int dp_boost_level, dp_max_link_rate, hdmi_boost_level, hdmi_level_shift, max_tmds_clock;
2638021a62a5SVille Syrjälä 	enum port port;
2639021a62a5SVille Syrjälä 
2640021a62a5SVille Syrjälä 	port = intel_bios_encoder_port(devdata);
2641021a62a5SVille Syrjälä 	if (port == PORT_NONE)
2642021a62a5SVille Syrjälä 		return;
2643df0566a6SJani Nikula 
2644d0ab409dSJani Nikula 	is_dvi = intel_bios_encoder_supports_dvi(devdata);
2645d0ab409dSJani Nikula 	is_dp = intel_bios_encoder_supports_dp(devdata);
2646d0ab409dSJani Nikula 	is_crt = intel_bios_encoder_supports_crt(devdata);
2647d0ab409dSJani Nikula 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2648d0ab409dSJani Nikula 	is_edp = intel_bios_encoder_supports_edp(devdata);
2649ba00eb6aSVille Syrjälä 	is_dsi = intel_bios_encoder_supports_dsi(devdata);
2650df0566a6SJani Nikula 
2651f08fbe6aSJani Nikula 	supports_typec_usb = intel_bios_encoder_supports_typec_usb(devdata);
2652f08fbe6aSJani Nikula 	supports_tbt = intel_bios_encoder_supports_tbt(devdata);
2653df0566a6SJani Nikula 
26549aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
26552bea1d7cSVille Syrjälä 		    "Port %c VBT info: CRT:%d DVI:%d HDMI:%d DP:%d eDP:%d DSI:%d DP++:%d LSPCON:%d USB-Type-C:%d TBT:%d DSC:%d\n",
2656ba00eb6aSVille Syrjälä 		    port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp, is_dsi,
26572bea1d7cSVille Syrjälä 		    intel_bios_encoder_supports_dp_dual_mode(devdata),
2658db5d650fSVille Syrjälä 		    intel_bios_encoder_is_lspcon(devdata),
2659f08fbe6aSJani Nikula 		    supports_typec_usb, supports_tbt,
26606e0d46e9SJani Nikula 		    devdata->dsc != NULL);
2661df0566a6SJani Nikula 
266202107ef1SVille Syrjälä 	hdmi_level_shift = intel_bios_hdmi_level_shift(devdata);
2663a9a56e76SJani Nikula 	if (hdmi_level_shift >= 0) {
26649aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
26656ee8d381SJani Nikula 			    "Port %c VBT HDMI level shift: %d\n",
2666a9a56e76SJani Nikula 			    port_name(port), hdmi_level_shift);
2667df0566a6SJani Nikula 	}
2668df0566a6SJani Nikula 
266902107ef1SVille Syrjälä 	max_tmds_clock = intel_bios_hdmi_max_tmds_clock(devdata);
2670df0566a6SJani Nikula 	if (max_tmds_clock)
26719aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
26726ee8d381SJani Nikula 			    "Port %c VBT HDMI max TMDS clock: %d kHz\n",
2673df0566a6SJani Nikula 			    port_name(port), max_tmds_clock);
2674df0566a6SJani Nikula 
2675c0a950d1SJani Nikula 	/* I_boost config for SKL and above */
267602107ef1SVille Syrjälä 	dp_boost_level = intel_bios_dp_boost_level(devdata);
2677c0a950d1SJani Nikula 	if (dp_boost_level)
26789aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
26796ee8d381SJani Nikula 			    "Port %c VBT (e)DP boost level: %d\n",
2680c0a950d1SJani Nikula 			    port_name(port), dp_boost_level);
2681c0a950d1SJani Nikula 
268202107ef1SVille Syrjälä 	hdmi_boost_level = intel_bios_hdmi_boost_level(devdata);
2683c0a950d1SJani Nikula 	if (hdmi_boost_level)
26849aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
26856ee8d381SJani Nikula 			    "Port %c VBT HDMI boost level: %d\n",
2686c0a950d1SJani Nikula 			    port_name(port), hdmi_boost_level);
2687df0566a6SJani Nikula 
268802107ef1SVille Syrjälä 	dp_max_link_rate = intel_bios_dp_max_link_rate(devdata);
268972337aacSJani Nikula 	if (dp_max_link_rate)
26909aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
26916ee8d381SJani Nikula 			    "Port %c VBT DP max link rate: %d\n",
269272337aacSJani Nikula 			    port_name(port), dp_max_link_rate);
2693429a0955SVille Syrjälä 
2694429a0955SVille Syrjälä 	/*
2695429a0955SVille Syrjälä 	 * FIXME need to implement support for VBT
2696429a0955SVille Syrjälä 	 * vswing/preemph tables should this ever trigger.
2697429a0955SVille Syrjälä 	 */
26989aec6f76SJani Nikula 	drm_WARN(display->drm, child->use_vbt_vswing,
2699429a0955SVille Syrjälä 		 "Port %c asks to use VBT vswing/preemph tables\n",
2700429a0955SVille Syrjälä 		 port_name(port));
27018d2ba05bSJani Nikula }
27028d2ba05bSJani Nikula 
parse_ddi_port(struct intel_bios_encoder_data * devdata)27038d2ba05bSJani Nikula static void parse_ddi_port(struct intel_bios_encoder_data *devdata)
27048d2ba05bSJani Nikula {
27059aec6f76SJani Nikula 	struct intel_display *display = devdata->display;
27068d2ba05bSJani Nikula 	enum port port;
27078d2ba05bSJani Nikula 
2708d84b1945SVille Syrjälä 	port = intel_bios_encoder_port(devdata);
27098d2ba05bSJani Nikula 	if (port == PORT_NONE)
27108d2ba05bSJani Nikula 		return;
27118d2ba05bSJani Nikula 
27129aec6f76SJani Nikula 	if (!is_port_valid(display, port)) {
27139aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
27148d2ba05bSJani Nikula 			    "VBT reports port %c as supported, but that can't be true: skipping\n",
27158d2ba05bSJani Nikula 			    port_name(port));
27168d2ba05bSJani Nikula 		return;
27178d2ba05bSJani Nikula 	}
27188d2ba05bSJani Nikula 
27198d2ba05bSJani Nikula 	sanitize_device_type(devdata, port);
27209e372744SVille Syrjälä 	sanitize_hdmi_level_shift(devdata, port);
2721df0566a6SJani Nikula }
2722df0566a6SJani Nikula 
has_ddi_port_info(struct intel_display * display)27239aec6f76SJani Nikula static bool has_ddi_port_info(struct intel_display *display)
2724b90b6e41SVille Syrjälä {
27259aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
27269aec6f76SJani Nikula 
27279aec6f76SJani Nikula 	return DISPLAY_VER(display) >= 5 || IS_G4X(i915);
2728b90b6e41SVille Syrjälä }
2729b90b6e41SVille Syrjälä 
parse_ddi_ports(struct intel_display * display)27309aec6f76SJani Nikula static void parse_ddi_ports(struct intel_display *display)
2731df0566a6SJani Nikula {
27323162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2733df0566a6SJani Nikula 
27349aec6f76SJani Nikula 	if (!has_ddi_port_info(display))
2735df0566a6SJani Nikula 		return;
2736df0566a6SJani Nikula 
27379aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node)
2738c78783f3SJani Nikula 		parse_ddi_port(devdata);
2739e61f294cSJani Nikula 
27409aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node)
2741021a62a5SVille Syrjälä 		print_ddi_port(devdata);
2742df0566a6SJani Nikula }
2743df0566a6SJani Nikula 
child_device_expected_size(u16 version)2744a1789b3fSJani Nikula static int child_device_expected_size(u16 version)
2745a1789b3fSJani Nikula {
2746a1789b3fSJani Nikula 	BUILD_BUG_ON(sizeof(struct child_device_config) < 40);
2747a1789b3fSJani Nikula 
2748a1789b3fSJani Nikula 	if (version > 256)
2749a1789b3fSJani Nikula 		return -ENOENT;
2750a1789b3fSJani Nikula 	else if (version >= 256)
2751a1789b3fSJani Nikula 		return 40;
2752a1789b3fSJani Nikula 	else if (version >= 216)
2753a1789b3fSJani Nikula 		return 39;
2754a1789b3fSJani Nikula 	else if (version >= 196)
2755a1789b3fSJani Nikula 		return 38;
2756a1789b3fSJani Nikula 	else if (version >= 195)
2757a1789b3fSJani Nikula 		return 37;
2758a1789b3fSJani Nikula 	else if (version >= 111)
2759a1789b3fSJani Nikula 		return LEGACY_CHILD_DEVICE_CONFIG_SIZE;
2760a1789b3fSJani Nikula 	else if (version >= 106)
2761a1789b3fSJani Nikula 		return 27;
2762a1789b3fSJani Nikula 	else
2763a1789b3fSJani Nikula 		return 22;
2764a1789b3fSJani Nikula }
2765a1789b3fSJani Nikula 
child_device_size_valid(struct intel_display * display,int size)27669aec6f76SJani Nikula static bool child_device_size_valid(struct intel_display *display, int size)
2767e396a06eSJani Nikula {
2768e396a06eSJani Nikula 	int expected_size;
2769e396a06eSJani Nikula 
27709aec6f76SJani Nikula 	expected_size = child_device_expected_size(display->vbt.version);
2771a1789b3fSJani Nikula 	if (expected_size < 0) {
2772e396a06eSJani Nikula 		expected_size = sizeof(struct child_device_config);
27739aec6f76SJani Nikula 		drm_dbg(display->drm,
2774e396a06eSJani Nikula 			"Expected child device config size for VBT version %u not known; assuming %d\n",
27759aec6f76SJani Nikula 			display->vbt.version, expected_size);
2776e396a06eSJani Nikula 	}
2777e396a06eSJani Nikula 
2778e396a06eSJani Nikula 	/* Flag an error for unexpected size, but continue anyway. */
2779e396a06eSJani Nikula 	if (size != expected_size)
27809aec6f76SJani Nikula 		drm_err(display->drm,
2781e396a06eSJani Nikula 			"Unexpected child device config size %d (expected %d for VBT version %u)\n",
27829aec6f76SJani Nikula 			size, expected_size, display->vbt.version);
2783e396a06eSJani Nikula 
2784e396a06eSJani Nikula 	/* The legacy sized child device config is the minimum we need. */
2785e396a06eSJani Nikula 	if (size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
27869aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2787e396a06eSJani Nikula 			    "Child device config size %d is too small.\n",
2788e396a06eSJani Nikula 			    size);
2789e396a06eSJani Nikula 		return false;
2790e396a06eSJani Nikula 	}
2791e396a06eSJani Nikula 
2792e396a06eSJani Nikula 	return true;
2793e396a06eSJani Nikula }
2794e396a06eSJani Nikula 
2795df0566a6SJani Nikula static void
parse_general_definitions(struct intel_display * display)27969aec6f76SJani Nikula parse_general_definitions(struct intel_display *display)
2797df0566a6SJani Nikula {
27989aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
2799df0566a6SJani Nikula 	const struct bdb_general_definitions *defs;
28003162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2801df0566a6SJani Nikula 	const struct child_device_config *child;
28020d9ef19bSJani Nikula 	int i, child_device_num;
2803df0566a6SJani Nikula 	u16 block_size;
2804df0566a6SJani Nikula 	int bus_pin;
2805df0566a6SJani Nikula 
28069aec6f76SJani Nikula 	defs = bdb_find_section(display, BDB_GENERAL_DEFINITIONS);
2807df0566a6SJani Nikula 	if (!defs) {
28089aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2809e92cbf38SWambui Karuga 			    "No general definition block is found, no devices defined.\n");
2810df0566a6SJani Nikula 		return;
2811df0566a6SJani Nikula 	}
2812df0566a6SJani Nikula 
2813df0566a6SJani Nikula 	block_size = get_blocksize(defs);
2814df0566a6SJani Nikula 	if (block_size < sizeof(*defs)) {
28159aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2816e92cbf38SWambui Karuga 			    "General definitions block too small (%u)\n",
2817df0566a6SJani Nikula 			    block_size);
2818df0566a6SJani Nikula 		return;
2819df0566a6SJani Nikula 	}
2820df0566a6SJani Nikula 
2821df0566a6SJani Nikula 	bus_pin = defs->crt_ddc_gmbus_pin;
28229aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "crt_ddc_bus_pin: %d\n", bus_pin);
2823dbd440d8SJani Nikula 	if (intel_gmbus_is_valid_pin(i915, bus_pin))
28249aec6f76SJani Nikula 		display->vbt.crt_ddc_pin = bus_pin;
2825df0566a6SJani Nikula 
28269aec6f76SJani Nikula 	if (!child_device_size_valid(display, defs->child_dev_size))
2827df0566a6SJani Nikula 		return;
2828df0566a6SJani Nikula 
2829df0566a6SJani Nikula 	/* get the number of child device */
2830df0566a6SJani Nikula 	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
2831df0566a6SJani Nikula 
2832df0566a6SJani Nikula 	for (i = 0; i < child_device_num; i++) {
2833df0566a6SJani Nikula 		child = child_device_ptr(defs, i);
2834df0566a6SJani Nikula 		if (!child->device_type)
2835df0566a6SJani Nikula 			continue;
2836df0566a6SJani Nikula 
28379aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2838e92cbf38SWambui Karuga 			    "Found VBT child device with type 0x%x\n",
2839bdeb18dbSMatt Roper 			    child->device_type);
2840bdeb18dbSMatt Roper 
28410d9ef19bSJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
28420d9ef19bSJani Nikula 		if (!devdata)
28430d9ef19bSJani Nikula 			break;
28440d9ef19bSJani Nikula 
28459aec6f76SJani Nikula 		devdata->display = display;
28467371fa34SJani Nikula 
2847df0566a6SJani Nikula 		/*
2848df0566a6SJani Nikula 		 * Copy as much as we know (sizeof) and is available
28490d9ef19bSJani Nikula 		 * (child_dev_size) of the child device config. Accessing the
28500d9ef19bSJani Nikula 		 * data must depend on VBT version.
2851df0566a6SJani Nikula 		 */
28520d9ef19bSJani Nikula 		memcpy(&devdata->child, child,
2853df0566a6SJani Nikula 		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
28540d9ef19bSJani Nikula 
28559aec6f76SJani Nikula 		list_add_tail(&devdata->node, &display->vbt.display_devices);
2856df0566a6SJani Nikula 	}
28570d9ef19bSJani Nikula 
28589aec6f76SJani Nikula 	if (list_empty(&display->vbt.display_devices))
28599aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2860e92cbf38SWambui Karuga 			    "no child dev is parsed from VBT\n");
2861df0566a6SJani Nikula }
2862df0566a6SJani Nikula 
2863df0566a6SJani Nikula /* Common defaults which may be overridden by VBT. */
2864df0566a6SJani Nikula static void
init_vbt_defaults(struct intel_display * display)28659aec6f76SJani Nikula init_vbt_defaults(struct intel_display *display)
2866df0566a6SJani Nikula {
28679aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
28689aec6f76SJani Nikula 
28699aec6f76SJani Nikula 	display->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
2870df0566a6SJani Nikula 
2871df0566a6SJani Nikula 	/* general features */
28729aec6f76SJani Nikula 	display->vbt.int_tv_support = 1;
28739aec6f76SJani Nikula 	display->vbt.int_crt_support = 1;
2874df0566a6SJani Nikula 
2875df0566a6SJani Nikula 	/* driver features */
28769aec6f76SJani Nikula 	display->vbt.int_lvds_support = 1;
2877df0566a6SJani Nikula 
2878df0566a6SJani Nikula 	/* Default to using SSC */
28799aec6f76SJani Nikula 	display->vbt.lvds_use_ssc = 1;
2880df0566a6SJani Nikula 	/*
2881df0566a6SJani Nikula 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
2882df0566a6SJani Nikula 	 * clock for LVDS.
2883df0566a6SJani Nikula 	 */
28849aec6f76SJani Nikula 	display->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(display,
2885dbd440d8SJani Nikula 							      !HAS_PCH_SPLIT(i915));
28869aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "Set default to SSC at %d kHz\n",
28879aec6f76SJani Nikula 		    display->vbt.lvds_ssc_freq);
2888df0566a6SJani Nikula }
2889df0566a6SJani Nikula 
28903cf05076SVille Syrjälä /* Common defaults which may be overridden by VBT. */
28913cf05076SVille Syrjälä static void
init_vbt_panel_defaults(struct intel_panel * panel)28923cf05076SVille Syrjälä init_vbt_panel_defaults(struct intel_panel *panel)
28933cf05076SVille Syrjälä {
28943cf05076SVille Syrjälä 	/* Default to having backlight */
28953cf05076SVille Syrjälä 	panel->vbt.backlight.present = true;
28963cf05076SVille Syrjälä 
28973cf05076SVille Syrjälä 	/* LFP panel data */
28983cf05076SVille Syrjälä 	panel->vbt.lvds_dither = true;
28993cf05076SVille Syrjälä }
29003cf05076SVille Syrjälä 
2901df0566a6SJani Nikula /* Defaults to initialize only if there is no VBT. */
2902df0566a6SJani Nikula static void
init_vbt_missing_defaults(struct intel_display * display)29039aec6f76SJani Nikula init_vbt_missing_defaults(struct intel_display *display)
2904df0566a6SJani Nikula {
29059aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
29069aec6f76SJani Nikula 	unsigned int ports = DISPLAY_RUNTIME_INFO(display)->port_mask;
2907df0566a6SJani Nikula 	enum port port;
2908df0566a6SJani Nikula 
29099aec6f76SJani Nikula 	if (!HAS_DDI(display) && !IS_CHERRYVIEW(i915))
2910e20e4037SJani Nikula 		return;
2911e20e4037SJani Nikula 
29123ae04c0cSJani Nikula 	for_each_port_masked(port, ports) {
29133162d057SJani Nikula 		struct intel_bios_encoder_data *devdata;
291451f57481SJani Nikula 		struct child_device_config *child;
2915dbd440d8SJani Nikula 		enum phy phy = intel_port_to_phy(i915, port);
2916df0566a6SJani Nikula 
2917df0566a6SJani Nikula 		/*
2918df0566a6SJani Nikula 		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
2919df0566a6SJani Nikula 		 * to detect it.
2920df0566a6SJani Nikula 		 */
2921dbd440d8SJani Nikula 		if (intel_phy_is_tc(i915, phy))
2922df0566a6SJani Nikula 			continue;
2923df0566a6SJani Nikula 
292451f57481SJani Nikula 		/* Create fake child device config */
292551f57481SJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
292651f57481SJani Nikula 		if (!devdata)
292751f57481SJani Nikula 			break;
292851f57481SJani Nikula 
29299aec6f76SJani Nikula 		devdata->display = display;
293051f57481SJani Nikula 		child = &devdata->child;
293151f57481SJani Nikula 
293251f57481SJani Nikula 		if (port == PORT_F)
293351f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIF;
293451f57481SJani Nikula 		else if (port == PORT_E)
293551f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIE;
293651f57481SJani Nikula 		else
293751f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIA + port;
293851f57481SJani Nikula 
293951f57481SJani Nikula 		if (port != PORT_A && port != PORT_E)
294051f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_TMDS_DVI_SIGNALING;
294151f57481SJani Nikula 
294251f57481SJani Nikula 		if (port != PORT_E)
294351f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_DISPLAYPORT_OUTPUT;
294451f57481SJani Nikula 
294551f57481SJani Nikula 		if (port == PORT_A)
294651f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_INTERNAL_CONNECTOR;
294751f57481SJani Nikula 
29489aec6f76SJani Nikula 		list_add_tail(&devdata->node, &display->vbt.display_devices);
294951f57481SJani Nikula 
29509aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
2951*02895076SJani Nikula 			    "Generating default VBT child device with type 0x%04x on port %c\n",
295251f57481SJani Nikula 			    child->device_type, port_name(port));
2953df0566a6SJani Nikula 	}
295451f57481SJani Nikula 
295551f57481SJani Nikula 	/* Bypass some minimum baseline VBT version checks */
29569aec6f76SJani Nikula 	display->vbt.version = 155;
2957df0566a6SJani Nikula }
2958df0566a6SJani Nikula 
get_bdb_header(const struct vbt_header * vbt)2959df0566a6SJani Nikula static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
2960df0566a6SJani Nikula {
2961df0566a6SJani Nikula 	const void *_vbt = vbt;
2962df0566a6SJani Nikula 
2963df0566a6SJani Nikula 	return _vbt + vbt->bdb_offset;
2964df0566a6SJani Nikula }
2965df0566a6SJani Nikula 
2966df0566a6SJani Nikula /**
2967df0566a6SJani Nikula  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
29689aec6f76SJani Nikula  * @display:	display device
2969df0566a6SJani Nikula  * @buf:	pointer to a buffer to validate
2970df0566a6SJani Nikula  * @size:	size of the buffer
2971df0566a6SJani Nikula  *
2972df0566a6SJani Nikula  * Returns true on valid VBT.
2973df0566a6SJani Nikula  */
intel_bios_is_valid_vbt(struct intel_display * display,const void * buf,size_t size)29749aec6f76SJani Nikula bool intel_bios_is_valid_vbt(struct intel_display *display,
2975ff9bc20cSVille Syrjälä 			     const void *buf, size_t size)
2976df0566a6SJani Nikula {
2977df0566a6SJani Nikula 	const struct vbt_header *vbt = buf;
2978df0566a6SJani Nikula 	const struct bdb_header *bdb;
2979df0566a6SJani Nikula 
2980df0566a6SJani Nikula 	if (!vbt)
2981df0566a6SJani Nikula 		return false;
2982df0566a6SJani Nikula 
2983df0566a6SJani Nikula 	if (sizeof(struct vbt_header) > size) {
29849aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "VBT header incomplete\n");
2985df0566a6SJani Nikula 		return false;
2986df0566a6SJani Nikula 	}
2987df0566a6SJani Nikula 
2988df0566a6SJani Nikula 	if (memcmp(vbt->signature, "$VBT", 4)) {
29899aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "VBT invalid signature\n");
2990df0566a6SJani Nikula 		return false;
2991df0566a6SJani Nikula 	}
2992df0566a6SJani Nikula 
2993ff00ff96SLucas De Marchi 	if (vbt->vbt_size > size) {
29949aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
29959aec6f76SJani Nikula 			    "VBT incomplete (vbt_size overflows)\n");
2996ff00ff96SLucas De Marchi 		return false;
2997ff00ff96SLucas De Marchi 	}
2998ff00ff96SLucas De Marchi 
2999ff00ff96SLucas De Marchi 	size = vbt->vbt_size;
3000ff00ff96SLucas De Marchi 
3001df0566a6SJani Nikula 	if (range_overflows_t(size_t,
3002df0566a6SJani Nikula 			      vbt->bdb_offset,
3003df0566a6SJani Nikula 			      sizeof(struct bdb_header),
3004df0566a6SJani Nikula 			      size)) {
30059aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "BDB header incomplete\n");
3006df0566a6SJani Nikula 		return false;
3007df0566a6SJani Nikula 	}
3008df0566a6SJani Nikula 
3009df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
3010df0566a6SJani Nikula 	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
30119aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "BDB incomplete\n");
3012df0566a6SJani Nikula 		return false;
3013df0566a6SJani Nikula 	}
3014df0566a6SJani Nikula 
3015df0566a6SJani Nikula 	return vbt;
3016df0566a6SJani Nikula }
3017df0566a6SJani Nikula 
firmware_get_vbt(struct intel_display * display,size_t * size)30189aec6f76SJani Nikula static struct vbt_header *firmware_get_vbt(struct intel_display *display,
3019d962f0afSRadhakrishna Sripada 					   size_t *size)
3020d962f0afSRadhakrishna Sripada {
3021d962f0afSRadhakrishna Sripada 	struct vbt_header *vbt = NULL;
3022d962f0afSRadhakrishna Sripada 	const struct firmware *fw = NULL;
30239aec6f76SJani Nikula 	const char *name = display->params.vbt_firmware;
3024d962f0afSRadhakrishna Sripada 	int ret;
3025d962f0afSRadhakrishna Sripada 
3026d962f0afSRadhakrishna Sripada 	if (!name || !*name)
3027d962f0afSRadhakrishna Sripada 		return NULL;
3028d962f0afSRadhakrishna Sripada 
30299aec6f76SJani Nikula 	ret = request_firmware(&fw, name, display->drm->dev);
3030d962f0afSRadhakrishna Sripada 	if (ret) {
30319aec6f76SJani Nikula 		drm_err(display->drm,
3032d962f0afSRadhakrishna Sripada 			"Requesting VBT firmware \"%s\" failed (%d)\n",
3033d962f0afSRadhakrishna Sripada 			name, ret);
3034d962f0afSRadhakrishna Sripada 		return NULL;
3035d962f0afSRadhakrishna Sripada 	}
3036d962f0afSRadhakrishna Sripada 
30379aec6f76SJani Nikula 	if (intel_bios_is_valid_vbt(display, fw->data, fw->size)) {
3038d962f0afSRadhakrishna Sripada 		vbt = kmemdup(fw->data, fw->size, GFP_KERNEL);
3039d962f0afSRadhakrishna Sripada 		if (vbt) {
30409aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
3041d962f0afSRadhakrishna Sripada 				    "Found valid VBT firmware \"%s\"\n", name);
3042d962f0afSRadhakrishna Sripada 			if (size)
3043d962f0afSRadhakrishna Sripada 				*size = fw->size;
3044d962f0afSRadhakrishna Sripada 		}
3045d962f0afSRadhakrishna Sripada 	} else {
30469aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "Invalid VBT firmware \"%s\"\n",
3047d962f0afSRadhakrishna Sripada 			    name);
3048d962f0afSRadhakrishna Sripada 	}
3049d962f0afSRadhakrishna Sripada 
3050d962f0afSRadhakrishna Sripada 	release_firmware(fw);
3051d962f0afSRadhakrishna Sripada 
3052d962f0afSRadhakrishna Sripada 	return vbt;
3053d962f0afSRadhakrishna Sripada }
3054d962f0afSRadhakrishna Sripada 
intel_spi_read(struct intel_uncore * uncore,u32 offset)30553631c363SJani Nikula static u32 intel_spi_read(struct intel_uncore *uncore, u32 offset)
30563631c363SJani Nikula {
30573631c363SJani Nikula 	intel_uncore_write(uncore, PRIMARY_SPI_ADDRESS, offset);
30583631c363SJani Nikula 
30593631c363SJani Nikula 	return intel_uncore_read(uncore, PRIMARY_SPI_TRIGGER);
30603631c363SJani Nikula }
30613631c363SJani Nikula 
spi_oprom_get_vbt(struct intel_display * display,size_t * size)30629aec6f76SJani Nikula static struct vbt_header *spi_oprom_get_vbt(struct intel_display *display,
30638612f91eSRadhakrishna Sripada 					    size_t *size)
3064a36e7dc0SClint Taylor {
30659aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
3066a36e7dc0SClint Taylor 	u32 count, data, found, store = 0;
3067a36e7dc0SClint Taylor 	u32 static_region, oprom_offset;
3068a36e7dc0SClint Taylor 	u32 oprom_size = 0x200000;
3069a36e7dc0SClint Taylor 	u16 vbt_size;
3070a36e7dc0SClint Taylor 	u32 *vbt;
3071a36e7dc0SClint Taylor 
3072a36e7dc0SClint Taylor 	static_region = intel_uncore_read(&i915->uncore, SPI_STATIC_REGIONS);
3073a36e7dc0SClint Taylor 	static_region &= OPTIONROM_SPI_REGIONID_MASK;
3074a36e7dc0SClint Taylor 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_REGIONID, static_region);
3075a36e7dc0SClint Taylor 
3076a36e7dc0SClint Taylor 	oprom_offset = intel_uncore_read(&i915->uncore, OROM_OFFSET);
3077a36e7dc0SClint Taylor 	oprom_offset &= OROM_OFFSET_MASK;
3078a36e7dc0SClint Taylor 
3079a36e7dc0SClint Taylor 	for (count = 0; count < oprom_size; count += 4) {
30803631c363SJani Nikula 		data = intel_spi_read(&i915->uncore, oprom_offset + count);
3081a36e7dc0SClint Taylor 		if (data == *((const u32 *)"$VBT")) {
3082a36e7dc0SClint Taylor 			found = oprom_offset + count;
3083a36e7dc0SClint Taylor 			break;
3084a36e7dc0SClint Taylor 		}
3085a36e7dc0SClint Taylor 	}
3086a36e7dc0SClint Taylor 
3087a36e7dc0SClint Taylor 	if (count >= oprom_size)
3088a36e7dc0SClint Taylor 		goto err_not_found;
3089a36e7dc0SClint Taylor 
3090a36e7dc0SClint Taylor 	/* Get VBT size and allocate space for the VBT */
30913631c363SJani Nikula 	vbt_size = intel_spi_read(&i915->uncore,
30923631c363SJani Nikula 				  found + offsetof(struct vbt_header, vbt_size));
3093a36e7dc0SClint Taylor 	vbt_size &= 0xffff;
3094a36e7dc0SClint Taylor 
3095980f42e7SJani Nikula 	vbt = kzalloc(round_up(vbt_size, 4), GFP_KERNEL);
3096a36e7dc0SClint Taylor 	if (!vbt)
3097a36e7dc0SClint Taylor 		goto err_not_found;
3098a36e7dc0SClint Taylor 
30993631c363SJani Nikula 	for (count = 0; count < vbt_size; count += 4)
31003631c363SJani Nikula 		*(vbt + store++) = intel_spi_read(&i915->uncore, found + count);
3101a36e7dc0SClint Taylor 
31029aec6f76SJani Nikula 	if (!intel_bios_is_valid_vbt(display, vbt, vbt_size))
3103a36e7dc0SClint Taylor 		goto err_free_vbt;
3104a36e7dc0SClint Taylor 
31059aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "Found valid VBT in SPI flash\n");
3106a36e7dc0SClint Taylor 
31078612f91eSRadhakrishna Sripada 	if (size)
31088612f91eSRadhakrishna Sripada 		*size = vbt_size;
31098612f91eSRadhakrishna Sripada 
3110a36e7dc0SClint Taylor 	return (struct vbt_header *)vbt;
3111a36e7dc0SClint Taylor 
3112a36e7dc0SClint Taylor err_free_vbt:
3113a36e7dc0SClint Taylor 	kfree(vbt);
3114a36e7dc0SClint Taylor err_not_found:
3115a36e7dc0SClint Taylor 	return NULL;
3116a36e7dc0SClint Taylor }
3117a36e7dc0SClint Taylor 
oprom_get_vbt(struct intel_display * display,size_t * sizep)31189aec6f76SJani Nikula static struct vbt_header *oprom_get_vbt(struct intel_display *display,
3119b4c9ee84SRadhakrishna Sripada 					size_t *sizep)
3120df0566a6SJani Nikula {
31219aec6f76SJani Nikula 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
31222cded152SLucas De Marchi 	void __iomem *p = NULL, *oprom;
3123fd0186ceSLucas De Marchi 	struct vbt_header *vbt;
3124fd0186ceSLucas De Marchi 	u16 vbt_size;
31252cded152SLucas De Marchi 	size_t i, size;
31262cded152SLucas De Marchi 
31272cded152SLucas De Marchi 	oprom = pci_map_rom(pdev, &size);
31282cded152SLucas De Marchi 	if (!oprom)
31292cded152SLucas De Marchi 		return NULL;
3130df0566a6SJani Nikula 
3131df0566a6SJani Nikula 	/* Scour memory looking for the VBT signature. */
313298cf5c9aSLucas De Marchi 	for (i = 0; i + 4 < size; i += 4) {
3133496f50a6SLucas De Marchi 		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
3134df0566a6SJani Nikula 			continue;
3135df0566a6SJani Nikula 
3136fd0186ceSLucas De Marchi 		p = oprom + i;
3137fd0186ceSLucas De Marchi 		size -= i;
3138df0566a6SJani Nikula 		break;
3139df0566a6SJani Nikula 	}
3140df0566a6SJani Nikula 
3141fd0186ceSLucas De Marchi 	if (!p)
31422cded152SLucas De Marchi 		goto err_unmap_oprom;
3143fd0186ceSLucas De Marchi 
3144fd0186ceSLucas De Marchi 	if (sizeof(struct vbt_header) > size) {
31459aec6f76SJani Nikula 		drm_dbg(display->drm, "VBT header incomplete\n");
31462cded152SLucas De Marchi 		goto err_unmap_oprom;
3147fd0186ceSLucas De Marchi 	}
3148fd0186ceSLucas De Marchi 
3149fd0186ceSLucas De Marchi 	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
3150fd0186ceSLucas De Marchi 	if (vbt_size > size) {
31519aec6f76SJani Nikula 		drm_dbg(display->drm,
3152e92cbf38SWambui Karuga 			"VBT incomplete (vbt_size overflows)\n");
31532cded152SLucas De Marchi 		goto err_unmap_oprom;
3154fd0186ceSLucas De Marchi 	}
3155fd0186ceSLucas De Marchi 
3156fd0186ceSLucas De Marchi 	/* The rest will be validated by intel_bios_is_valid_vbt() */
3157fd0186ceSLucas De Marchi 	vbt = kmalloc(vbt_size, GFP_KERNEL);
3158fd0186ceSLucas De Marchi 	if (!vbt)
31592cded152SLucas De Marchi 		goto err_unmap_oprom;
3160fd0186ceSLucas De Marchi 
3161fd0186ceSLucas De Marchi 	memcpy_fromio(vbt, p, vbt_size);
3162fd0186ceSLucas De Marchi 
31639aec6f76SJani Nikula 	if (!intel_bios_is_valid_vbt(display, vbt, vbt_size))
3164fd0186ceSLucas De Marchi 		goto err_free_vbt;
3165fd0186ceSLucas De Marchi 
31662cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
31672cded152SLucas De Marchi 
3168b4c9ee84SRadhakrishna Sripada 	if (sizep)
3169b4c9ee84SRadhakrishna Sripada 		*sizep = vbt_size;
3170b4c9ee84SRadhakrishna Sripada 
31719aec6f76SJani Nikula 	drm_dbg_kms(display->drm, "Found valid VBT in PCI ROM\n");
3172a36e7dc0SClint Taylor 
3173fd0186ceSLucas De Marchi 	return vbt;
3174fd0186ceSLucas De Marchi 
3175fd0186ceSLucas De Marchi err_free_vbt:
3176fd0186ceSLucas De Marchi 	kfree(vbt);
31772cded152SLucas De Marchi err_unmap_oprom:
31782cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
3179fd0186ceSLucas De Marchi 
3180df0566a6SJani Nikula 	return NULL;
3181df0566a6SJani Nikula }
3182df0566a6SJani Nikula 
intel_bios_get_vbt(struct intel_display * display,size_t * sizep)31839aec6f76SJani Nikula static const struct vbt_header *intel_bios_get_vbt(struct intel_display *display,
3184a2596003SRadhakrishna Sripada 						   size_t *sizep)
3185a2596003SRadhakrishna Sripada {
31869aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
3187a2596003SRadhakrishna Sripada 	const struct vbt_header *vbt = NULL;
3188a2596003SRadhakrishna Sripada 	intel_wakeref_t wakeref;
3189a2596003SRadhakrishna Sripada 
31909aec6f76SJani Nikula 	vbt = firmware_get_vbt(display, sizep);
3191a2596003SRadhakrishna Sripada 
3192a2596003SRadhakrishna Sripada 	if (!vbt)
3193769b081cSJani Nikula 		vbt = intel_opregion_get_vbt(display, sizep);
3194a2596003SRadhakrishna Sripada 
3195a2596003SRadhakrishna Sripada 	/*
3196a2596003SRadhakrishna Sripada 	 * If the OpRegion does not have VBT, look in SPI flash
3197a2596003SRadhakrishna Sripada 	 * through MMIO or PCI mapping
3198a2596003SRadhakrishna Sripada 	 */
3199a2596003SRadhakrishna Sripada 	if (!vbt && IS_DGFX(i915))
3200a2596003SRadhakrishna Sripada 		with_intel_runtime_pm(&i915->runtime_pm, wakeref)
32019aec6f76SJani Nikula 			vbt = spi_oprom_get_vbt(display, sizep);
3202a2596003SRadhakrishna Sripada 
3203a2596003SRadhakrishna Sripada 	if (!vbt)
3204a2596003SRadhakrishna Sripada 		with_intel_runtime_pm(&i915->runtime_pm, wakeref)
32059aec6f76SJani Nikula 			vbt = oprom_get_vbt(display, sizep);
3206a2596003SRadhakrishna Sripada 
3207a2596003SRadhakrishna Sripada 	return vbt;
3208a2596003SRadhakrishna Sripada }
3209a2596003SRadhakrishna Sripada 
3210df0566a6SJani Nikula /**
3211df0566a6SJani Nikula  * intel_bios_init - find VBT and initialize settings from the BIOS
32129aec6f76SJani Nikula  * @display: display device instance
3213df0566a6SJani Nikula  *
3214df0566a6SJani Nikula  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
3215df0566a6SJani Nikula  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
3216df0566a6SJani Nikula  * initialize some defaults if the VBT is not present at all.
3217df0566a6SJani Nikula  */
intel_bios_init(struct intel_display * display)32189aec6f76SJani Nikula void intel_bios_init(struct intel_display *display)
3219df0566a6SJani Nikula {
322037e21003SJani Nikula 	const struct vbt_header *vbt;
3221df0566a6SJani Nikula 	const struct bdb_header *bdb;
3222df0566a6SJani Nikula 
32239aec6f76SJani Nikula 	INIT_LIST_HEAD(&display->vbt.display_devices);
32249aec6f76SJani Nikula 	INIT_LIST_HEAD(&display->vbt.bdb_blocks);
32250d9ef19bSJani Nikula 
32269aec6f76SJani Nikula 	if (!HAS_DISPLAY(display)) {
32279aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
3228e92cbf38SWambui Karuga 			    "Skipping VBT init due to disabled display.\n");
3229df0566a6SJani Nikula 		return;
3230df0566a6SJani Nikula 	}
3231df0566a6SJani Nikula 
32329aec6f76SJani Nikula 	init_vbt_defaults(display);
3233df0566a6SJani Nikula 
32349aec6f76SJani Nikula 	vbt = intel_bios_get_vbt(display, NULL);
3235df0566a6SJani Nikula 
3236a36e7dc0SClint Taylor 	if (!vbt)
3237a36e7dc0SClint Taylor 		goto out;
3238a36e7dc0SClint Taylor 
3239df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
32409aec6f76SJani Nikula 	display->vbt.version = bdb->version;
3241df0566a6SJani Nikula 
32429aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
3243e92cbf38SWambui Karuga 		    "VBT signature \"%.*s\", BDB version %d\n",
32449aec6f76SJani Nikula 		    (int)sizeof(vbt->signature), vbt->signature,
32459aec6f76SJani Nikula 		    display->vbt.version);
3246df0566a6SJani Nikula 
32479aec6f76SJani Nikula 	init_bdb_blocks(display, bdb);
3248e163cfb4SVille Syrjälä 
3249df0566a6SJani Nikula 	/* Grab useful general definitions */
32509aec6f76SJani Nikula 	parse_general_features(display);
32519aec6f76SJani Nikula 	parse_general_definitions(display);
32529aec6f76SJani Nikula 	parse_driver_features(display);
3253df0566a6SJani Nikula 
32546e0d46e9SJani Nikula 	/* Depends on child device list */
32559aec6f76SJani Nikula 	parse_compression_parameters(display);
32566e0d46e9SJani Nikula 
3257df0566a6SJani Nikula out:
3258df0566a6SJani Nikula 	if (!vbt) {
32599aec6f76SJani Nikula 		drm_info(display->drm,
3260e92cbf38SWambui Karuga 			 "Failed to find VBIOS tables (VBT)\n");
32619aec6f76SJani Nikula 		init_vbt_missing_defaults(display);
3262df0566a6SJani Nikula 	}
3263df0566a6SJani Nikula 
326451f57481SJani Nikula 	/* Further processing on pre-parsed or generated child device data */
32659aec6f76SJani Nikula 	parse_sdvo_device_mapping(display);
32669aec6f76SJani Nikula 	parse_ddi_ports(display);
326751f57481SJani Nikula 
3268a2596003SRadhakrishna Sripada 	kfree(vbt);
3269df0566a6SJani Nikula }
3270df0566a6SJani Nikula 
intel_bios_init_panel(struct intel_display * display,struct intel_panel * panel,const struct intel_bios_encoder_data * devdata,const struct drm_edid * drm_edid,bool use_fallback)32719aec6f76SJani Nikula static void intel_bios_init_panel(struct intel_display *display,
3272c518a775SVille Syrjälä 				  struct intel_panel *panel,
32736434cf63SAnimesh Manna 				  const struct intel_bios_encoder_data *devdata,
3274c36225a1SJani Nikula 				  const struct drm_edid *drm_edid,
32753f9ffce5SVille Syrjälä 				  bool use_fallback)
3276c2fdb424SVille Syrjälä {
32773f9ffce5SVille Syrjälä 	/* already have it? */
32783f9ffce5SVille Syrjälä 	if (panel->vbt.panel_type >= 0) {
32799aec6f76SJani Nikula 		drm_WARN_ON(display->drm, !use_fallback);
32803f9ffce5SVille Syrjälä 		return;
32813f9ffce5SVille Syrjälä 	}
32823cf05076SVille Syrjälä 
32839aec6f76SJani Nikula 	panel->vbt.panel_type = get_panel_type(display, devdata,
3284c36225a1SJani Nikula 					       drm_edid, use_fallback);
32853f9ffce5SVille Syrjälä 	if (panel->vbt.panel_type < 0) {
32869aec6f76SJani Nikula 		drm_WARN_ON(display->drm, use_fallback);
32873f9ffce5SVille Syrjälä 		return;
32883f9ffce5SVille Syrjälä 	}
32893f9ffce5SVille Syrjälä 
32903f9ffce5SVille Syrjälä 	init_vbt_panel_defaults(panel);
32910256ea13SVille Syrjälä 
32929aec6f76SJani Nikula 	parse_panel_options(display, panel);
32939aec6f76SJani Nikula 	parse_generic_dtd(display, panel);
32949aec6f76SJani Nikula 	parse_lfp_data(display, panel);
32959aec6f76SJani Nikula 	parse_lfp_backlight(display, panel);
32969aec6f76SJani Nikula 	parse_sdvo_lvds_data(display, panel);
32979aec6f76SJani Nikula 	parse_panel_driver_features(display, panel);
32989aec6f76SJani Nikula 	parse_power_conservation_features(display, panel);
32999aec6f76SJani Nikula 	parse_edp(display, panel);
33009aec6f76SJani Nikula 	parse_psr(display, panel);
33019aec6f76SJani Nikula 	parse_mipi_config(display, panel);
33029aec6f76SJani Nikula 	parse_mipi_sequence(display, panel);
3303c2fdb424SVille Syrjälä }
3304c2fdb424SVille Syrjälä 
intel_bios_init_panel_early(struct intel_display * display,struct intel_panel * panel,const struct intel_bios_encoder_data * devdata)33059aec6f76SJani Nikula void intel_bios_init_panel_early(struct intel_display *display,
33063f9ffce5SVille Syrjälä 				 struct intel_panel *panel,
33073f9ffce5SVille Syrjälä 				 const struct intel_bios_encoder_data *devdata)
33083f9ffce5SVille Syrjälä {
33099aec6f76SJani Nikula 	intel_bios_init_panel(display, panel, devdata, NULL, false);
33103f9ffce5SVille Syrjälä }
33113f9ffce5SVille Syrjälä 
intel_bios_init_panel_late(struct intel_display * display,struct intel_panel * panel,const struct intel_bios_encoder_data * devdata,const struct drm_edid * drm_edid)33129aec6f76SJani Nikula void intel_bios_init_panel_late(struct intel_display *display,
33133f9ffce5SVille Syrjälä 				struct intel_panel *panel,
33143f9ffce5SVille Syrjälä 				const struct intel_bios_encoder_data *devdata,
3315c36225a1SJani Nikula 				const struct drm_edid *drm_edid)
33163f9ffce5SVille Syrjälä {
33179aec6f76SJani Nikula 	intel_bios_init_panel(display, panel, devdata, drm_edid, true);
33183f9ffce5SVille Syrjälä }
33193f9ffce5SVille Syrjälä 
3320df0566a6SJani Nikula /**
332178dae1acSJanusz Krzysztofik  * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
33229aec6f76SJani Nikula  * @display: display device instance
3323df0566a6SJani Nikula  */
intel_bios_driver_remove(struct intel_display * display)33249aec6f76SJani Nikula void intel_bios_driver_remove(struct intel_display *display)
3325df0566a6SJani Nikula {
3326e163cfb4SVille Syrjälä 	struct intel_bios_encoder_data *devdata, *nd;
3327e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry, *ne;
33280d9ef19bSJani Nikula 
33299aec6f76SJani Nikula 	list_for_each_entry_safe(devdata, nd, &display->vbt.display_devices,
33309aec6f76SJani Nikula 				 node) {
33310d9ef19bSJani Nikula 		list_del(&devdata->node);
33326e0d46e9SJani Nikula 		kfree(devdata->dsc);
33330d9ef19bSJani Nikula 		kfree(devdata);
33340d9ef19bSJani Nikula 	}
33350d9ef19bSJani Nikula 
33369aec6f76SJani Nikula 	list_for_each_entry_safe(entry, ne, &display->vbt.bdb_blocks, node) {
3337e163cfb4SVille Syrjälä 		list_del(&entry->node);
3338e163cfb4SVille Syrjälä 		kfree(entry);
3339e163cfb4SVille Syrjälä 	}
33403cf05076SVille Syrjälä }
3341e163cfb4SVille Syrjälä 
intel_bios_fini_panel(struct intel_panel * panel)33423cf05076SVille Syrjälä void intel_bios_fini_panel(struct intel_panel *panel)
33433cf05076SVille Syrjälä {
33443cf05076SVille Syrjälä 	kfree(panel->vbt.sdvo_lvds_vbt_mode);
33453cf05076SVille Syrjälä 	panel->vbt.sdvo_lvds_vbt_mode = NULL;
33466ac67ccfSVille Syrjälä 	kfree(panel->vbt.lfp_vbt_mode);
33476ac67ccfSVille Syrjälä 	panel->vbt.lfp_vbt_mode = NULL;
33483cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.data);
33493cf05076SVille Syrjälä 	panel->vbt.dsi.data = NULL;
33503cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.pps);
33513cf05076SVille Syrjälä 	panel->vbt.dsi.pps = NULL;
33523cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.config);
33533cf05076SVille Syrjälä 	panel->vbt.dsi.config = NULL;
33543cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.deassert_seq);
33553cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq = NULL;
3356df0566a6SJani Nikula }
3357df0566a6SJani Nikula 
3358df0566a6SJani Nikula /**
3359df0566a6SJani Nikula  * intel_bios_is_tv_present - is integrated TV present in VBT
33609aec6f76SJani Nikula  * @display: display device instance
3361df0566a6SJani Nikula  *
3362df0566a6SJani Nikula  * Return true if TV is present. If no child devices were parsed from VBT,
3363df0566a6SJani Nikula  * assume TV is present.
3364df0566a6SJani Nikula  */
intel_bios_is_tv_present(struct intel_display * display)33659aec6f76SJani Nikula bool intel_bios_is_tv_present(struct intel_display *display)
3366df0566a6SJani Nikula {
33673162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3368df0566a6SJani Nikula 
33699aec6f76SJani Nikula 	if (!display->vbt.int_tv_support)
3370df0566a6SJani Nikula 		return false;
3371df0566a6SJani Nikula 
33729aec6f76SJani Nikula 	if (list_empty(&display->vbt.display_devices))
3373df0566a6SJani Nikula 		return true;
3374df0566a6SJani Nikula 
33759aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
3376d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
33770d9ef19bSJani Nikula 
3378df0566a6SJani Nikula 		/*
3379df0566a6SJani Nikula 		 * If the device type is not TV, continue.
3380df0566a6SJani Nikula 		 */
3381df0566a6SJani Nikula 		switch (child->device_type) {
3382df0566a6SJani Nikula 		case DEVICE_TYPE_INT_TV:
3383df0566a6SJani Nikula 		case DEVICE_TYPE_TV:
3384df0566a6SJani Nikula 		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
3385df0566a6SJani Nikula 			break;
3386df0566a6SJani Nikula 		default:
3387df0566a6SJani Nikula 			continue;
3388df0566a6SJani Nikula 		}
3389df0566a6SJani Nikula 		/* Only when the addin_offset is non-zero, it is regarded
3390df0566a6SJani Nikula 		 * as present.
3391df0566a6SJani Nikula 		 */
3392df0566a6SJani Nikula 		if (child->addin_offset)
3393df0566a6SJani Nikula 			return true;
3394df0566a6SJani Nikula 	}
3395df0566a6SJani Nikula 
3396df0566a6SJani Nikula 	return false;
3397df0566a6SJani Nikula }
3398df0566a6SJani Nikula 
3399df0566a6SJani Nikula /**
3400df0566a6SJani Nikula  * intel_bios_is_lvds_present - is LVDS present in VBT
34019aec6f76SJani Nikula  * @display: display device instance
3402df0566a6SJani Nikula  * @i2c_pin:	i2c pin for LVDS if present
3403df0566a6SJani Nikula  *
3404df0566a6SJani Nikula  * Return true if LVDS is present. If no child devices were parsed from VBT,
3405df0566a6SJani Nikula  * assume LVDS is present.
3406df0566a6SJani Nikula  */
intel_bios_is_lvds_present(struct intel_display * display,u8 * i2c_pin)34079aec6f76SJani Nikula bool intel_bios_is_lvds_present(struct intel_display *display, u8 *i2c_pin)
3408df0566a6SJani Nikula {
34099aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
34103162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3411df0566a6SJani Nikula 
34129aec6f76SJani Nikula 	if (list_empty(&display->vbt.display_devices))
3413df0566a6SJani Nikula 		return true;
3414df0566a6SJani Nikula 
34159aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
3416d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3417df0566a6SJani Nikula 
3418df0566a6SJani Nikula 		/* If the device type is not LFP, continue.
3419df0566a6SJani Nikula 		 * We have to check both the new identifiers as well as the
3420df0566a6SJani Nikula 		 * old for compatibility with some BIOSes.
3421df0566a6SJani Nikula 		 */
3422df0566a6SJani Nikula 		if (child->device_type != DEVICE_TYPE_INT_LFP &&
3423df0566a6SJani Nikula 		    child->device_type != DEVICE_TYPE_LFP)
3424df0566a6SJani Nikula 			continue;
3425df0566a6SJani Nikula 
3426dbd440d8SJani Nikula 		if (intel_gmbus_is_valid_pin(i915, child->i2c_pin))
3427df0566a6SJani Nikula 			*i2c_pin = child->i2c_pin;
3428df0566a6SJani Nikula 
3429df0566a6SJani Nikula 		/* However, we cannot trust the BIOS writers to populate
3430df0566a6SJani Nikula 		 * the VBT correctly.  Since LVDS requires additional
3431df0566a6SJani Nikula 		 * information from AIM blocks, a non-zero addin offset is
3432df0566a6SJani Nikula 		 * a good indicator that the LVDS is actually present.
3433df0566a6SJani Nikula 		 */
3434df0566a6SJani Nikula 		if (child->addin_offset)
3435df0566a6SJani Nikula 			return true;
3436df0566a6SJani Nikula 
3437df0566a6SJani Nikula 		/* But even then some BIOS writers perform some black magic
3438df0566a6SJani Nikula 		 * and instantiate the device without reference to any
3439df0566a6SJani Nikula 		 * additional data.  Trust that if the VBT was written into
3440df0566a6SJani Nikula 		 * the OpRegion then they have validated the LVDS's existence.
3441df0566a6SJani Nikula 		 */
3442769b081cSJani Nikula 		return intel_opregion_vbt_present(display);
3443df0566a6SJani Nikula 	}
3444df0566a6SJani Nikula 
3445df0566a6SJani Nikula 	return false;
3446df0566a6SJani Nikula }
3447df0566a6SJani Nikula 
3448df0566a6SJani Nikula /**
3449df0566a6SJani Nikula  * intel_bios_is_port_present - is the specified digital port present
34509aec6f76SJani Nikula  * @display: display device instance
3451df0566a6SJani Nikula  * @port:	port to check
3452df0566a6SJani Nikula  *
3453df0566a6SJani Nikula  * Return true if the device in %port is present.
3454df0566a6SJani Nikula  */
intel_bios_is_port_present(struct intel_display * display,enum port port)34559aec6f76SJani Nikula bool intel_bios_is_port_present(struct intel_display *display, enum port port)
3456df0566a6SJani Nikula {
3457b17a15d6SVille Syrjälä 	const struct intel_bios_encoder_data *devdata;
3458b17a15d6SVille Syrjälä 
34599aec6f76SJani Nikula 	if (WARN_ON(!has_ddi_port_info(display)))
3460df0566a6SJani Nikula 		return true;
3461df0566a6SJani Nikula 
34629aec6f76SJani Nikula 	if (!is_port_valid(display, port))
3463b17a15d6SVille Syrjälä 		return false;
3464b17a15d6SVille Syrjälä 
34659aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
3466b17a15d6SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3467b17a15d6SVille Syrjälä 
34689aec6f76SJani Nikula 		if (dvo_port_to_port(display, child->dvo_port) == port)
3469b17a15d6SVille Syrjälä 			return true;
3470b17a15d6SVille Syrjälä 	}
3471b17a15d6SVille Syrjälä 
3472b17a15d6SVille Syrjälä 	return false;
3473df0566a6SJani Nikula }
3474df0566a6SJani Nikula 
intel_bios_encoder_supports_dp_dual_mode(const struct intel_bios_encoder_data * devdata)34752bea1d7cSVille Syrjälä bool intel_bios_encoder_supports_dp_dual_mode(const struct intel_bios_encoder_data *devdata)
347632c2bc89SVille Syrjälä {
3477044cbc7aSVille Syrjälä 	const struct child_device_config *child = &devdata->child;
3478044cbc7aSVille Syrjälä 
347926410896SVille Syrjälä 	if (!devdata)
348026410896SVille Syrjälä 		return false;
348126410896SVille Syrjälä 
3482044cbc7aSVille Syrjälä 	if (!intel_bios_encoder_supports_dp(devdata) ||
3483044cbc7aSVille Syrjälä 	    !intel_bios_encoder_supports_hdmi(devdata))
348432c2bc89SVille Syrjälä 		return false;
348532c2bc89SVille Syrjälä 
348632c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_DPA)
348732c2bc89SVille Syrjälä 		return true;
348832c2bc89SVille Syrjälä 
348932c2bc89SVille Syrjälä 	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
349032c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_HDMIA &&
349132c2bc89SVille Syrjälä 	    child->aux_channel != 0)
349232c2bc89SVille Syrjälä 		return true;
349332c2bc89SVille Syrjälä 
349432c2bc89SVille Syrjälä 	return false;
349532c2bc89SVille Syrjälä }
349632c2bc89SVille Syrjälä 
3497df0566a6SJani Nikula /**
3498df0566a6SJani Nikula  * intel_bios_is_dsi_present - is DSI present in VBT
34999aec6f76SJani Nikula  * @display: display device instance
3500df0566a6SJani Nikula  * @port:	port for DSI if present
3501df0566a6SJani Nikula  *
3502df0566a6SJani Nikula  * Return true if DSI is present, and return the port in %port.
3503df0566a6SJani Nikula  */
intel_bios_is_dsi_present(struct intel_display * display,enum port * port)35049aec6f76SJani Nikula bool intel_bios_is_dsi_present(struct intel_display *display,
3505df0566a6SJani Nikula 			       enum port *port)
3506df0566a6SJani Nikula {
35073162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3508df0566a6SJani Nikula 
35099aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
3510d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3511d24b3475SVille Syrjälä 		u8 dvo_port = child->dvo_port;
3512df0566a6SJani Nikula 
3513df0566a6SJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
3514df0566a6SJani Nikula 			continue;
3515df0566a6SJani Nikula 
35169aec6f76SJani Nikula 		if (dsi_dvo_port_to_port(display, dvo_port) == PORT_NONE) {
35179aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
3518e92cbf38SWambui Karuga 				    "VBT has unsupported DSI port %c\n",
3519df0566a6SJani Nikula 				    port_name(dvo_port - DVO_PORT_MIPIA));
3520118b5c13SVille Syrjälä 			continue;
3521df0566a6SJani Nikula 		}
3522118b5c13SVille Syrjälä 
3523118b5c13SVille Syrjälä 		if (port)
35249aec6f76SJani Nikula 			*port = dsi_dvo_port_to_port(display, dvo_port);
3525118b5c13SVille Syrjälä 		return true;
3526df0566a6SJani Nikula 	}
3527df0566a6SJani Nikula 
3528df0566a6SJani Nikula 	return false;
3529df0566a6SJani Nikula }
3530df0566a6SJani Nikula 
fill_dsc(struct intel_crtc_state * crtc_state,struct dsc_compression_parameters_entry * dsc,int dsc_max_bpc)35311bf2f3bfSJani Nikula static void fill_dsc(struct intel_crtc_state *crtc_state,
35321bf2f3bfSJani Nikula 		     struct dsc_compression_parameters_entry *dsc,
35331bf2f3bfSJani Nikula 		     int dsc_max_bpc)
35341bf2f3bfSJani Nikula {
35359aec6f76SJani Nikula 	struct intel_display *display = to_intel_display(crtc_state);
35361bf2f3bfSJani Nikula 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
35371bf2f3bfSJani Nikula 	int bpc = 8;
35381bf2f3bfSJani Nikula 
35391bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_major = dsc->version_major;
35401bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_minor = dsc->version_minor;
35411bf2f3bfSJani Nikula 
35421bf2f3bfSJani Nikula 	if (dsc->support_12bpc && dsc_max_bpc >= 12)
35431bf2f3bfSJani Nikula 		bpc = 12;
35441bf2f3bfSJani Nikula 	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
35451bf2f3bfSJani Nikula 		bpc = 10;
35461bf2f3bfSJani Nikula 	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
35471bf2f3bfSJani Nikula 		bpc = 8;
35481bf2f3bfSJani Nikula 	else
35499aec6f76SJani Nikula 		drm_dbg_kms(display->drm, "VBT: Unsupported BPC %d for DCS\n",
35501bf2f3bfSJani Nikula 			    dsc_max_bpc);
35511bf2f3bfSJani Nikula 
35521bf2f3bfSJani Nikula 	crtc_state->pipe_bpp = bpc * 3;
35531bf2f3bfSJani Nikula 
355431967638SImre Deak 	crtc_state->dsc.compressed_bpp_x16 = fxp_q4_from_int(min(crtc_state->pipe_bpp,
355559a266f0SAnkit Nautiyal 								 VBT_DSC_MAX_BPP(dsc->max_bpp)));
35561bf2f3bfSJani Nikula 
35571bf2f3bfSJani Nikula 	/*
35581bf2f3bfSJani Nikula 	 * FIXME: This is ugly, and slice count should take DSC engine
35591bf2f3bfSJani Nikula 	 * throughput etc. into account.
35601bf2f3bfSJani Nikula 	 *
35611bf2f3bfSJani Nikula 	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
35621bf2f3bfSJani Nikula 	 */
35631bf2f3bfSJani Nikula 	if (dsc->slices_per_line & BIT(2)) {
35641bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 4;
35651bf2f3bfSJani Nikula 	} else if (dsc->slices_per_line & BIT(1)) {
35661bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 2;
35671bf2f3bfSJani Nikula 	} else {
35681bf2f3bfSJani Nikula 		/* FIXME */
35691bf2f3bfSJani Nikula 		if (!(dsc->slices_per_line & BIT(0)))
35709aec6f76SJani Nikula 			drm_dbg_kms(display->drm,
35719aec6f76SJani Nikula 				    "VBT: Unsupported DSC slice count for DSI\n");
35721bf2f3bfSJani Nikula 
35731bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 1;
35741bf2f3bfSJani Nikula 	}
35751bf2f3bfSJani Nikula 
35761bf2f3bfSJani Nikula 	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
35771bf2f3bfSJani Nikula 	    crtc_state->dsc.slice_count != 0)
35789aec6f76SJani Nikula 		drm_dbg_kms(display->drm,
35799aec6f76SJani Nikula 			    "VBT: DSC hdisplay %d not divisible by slice count %d\n",
35801bf2f3bfSJani Nikula 			    crtc_state->hw.adjusted_mode.crtc_hdisplay,
35811bf2f3bfSJani Nikula 			    crtc_state->dsc.slice_count);
35821bf2f3bfSJani Nikula 
35831bf2f3bfSJani Nikula 	/*
35841bf2f3bfSJani Nikula 	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
3585fd8a5b27SJani Nikula 	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63.
35861bf2f3bfSJani Nikula 	 */
3587fd8a5b27SJani Nikula 	vdsc_cfg->rc_model_size = drm_dsc_dp_rc_buffer_size(dsc->rc_buffer_block_size,
3588fd8a5b27SJani Nikula 							    dsc->rc_buffer_size);
35891bf2f3bfSJani Nikula 
35901bf2f3bfSJani Nikula 	/* FIXME: DSI spec says bpc + 1 for this one */
35911bf2f3bfSJani Nikula 	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
35921bf2f3bfSJani Nikula 
35931bf2f3bfSJani Nikula 	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
35941bf2f3bfSJani Nikula 
35951bf2f3bfSJani Nikula 	vdsc_cfg->slice_height = dsc->slice_height;
35961bf2f3bfSJani Nikula }
35971bf2f3bfSJani Nikula 
35981bf2f3bfSJani Nikula /* FIXME: initially DSI specific */
intel_bios_get_dsc_params(struct intel_encoder * encoder,struct intel_crtc_state * crtc_state,int dsc_max_bpc)35991bf2f3bfSJani Nikula bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
36001bf2f3bfSJani Nikula 			       struct intel_crtc_state *crtc_state,
36011bf2f3bfSJani Nikula 			       int dsc_max_bpc)
36021bf2f3bfSJani Nikula {
36039aec6f76SJani Nikula 	struct intel_display *display = to_intel_display(encoder);
36043162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
36051bf2f3bfSJani Nikula 
36069aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
3607d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
36081bf2f3bfSJani Nikula 
36091bf2f3bfSJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
36101bf2f3bfSJani Nikula 			continue;
36111bf2f3bfSJani Nikula 
36129aec6f76SJani Nikula 		if (dsi_dvo_port_to_port(display, child->dvo_port) == encoder->port) {
36131bf2f3bfSJani Nikula 			if (!devdata->dsc)
36141bf2f3bfSJani Nikula 				return false;
36151bf2f3bfSJani Nikula 
36161bf2f3bfSJani Nikula 			fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
36171bf2f3bfSJani Nikula 
36181bf2f3bfSJani Nikula 			return true;
36191bf2f3bfSJani Nikula 		}
36201bf2f3bfSJani Nikula 	}
36211bf2f3bfSJani Nikula 
36221bf2f3bfSJani Nikula 	return false;
36231bf2f3bfSJani Nikula }
36241bf2f3bfSJani Nikula 
36255a0fc7a0SVille Syrjälä static const u8 adlp_aux_ch_map[] = {
36265a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
36275a0fc7a0SVille Syrjälä 	[AUX_CH_B] = DP_AUX_B,
36285a0fc7a0SVille Syrjälä 	[AUX_CH_C] = DP_AUX_C,
36295a0fc7a0SVille Syrjälä 	[AUX_CH_D_XELPD] = DP_AUX_D,
36305a0fc7a0SVille Syrjälä 	[AUX_CH_E_XELPD] = DP_AUX_E,
36315a0fc7a0SVille Syrjälä 	[AUX_CH_USBC1] = DP_AUX_F,
36325a0fc7a0SVille Syrjälä 	[AUX_CH_USBC2] = DP_AUX_G,
36335a0fc7a0SVille Syrjälä 	[AUX_CH_USBC3] = DP_AUX_H,
36345a0fc7a0SVille Syrjälä 	[AUX_CH_USBC4] = DP_AUX_I,
36355a0fc7a0SVille Syrjälä };
36365a0fc7a0SVille Syrjälä 
36375a0fc7a0SVille Syrjälä /*
36385a0fc7a0SVille Syrjälä  * ADL-S VBT uses PHY based mapping. Combo PHYs A,B,C,D,E
36395a0fc7a0SVille Syrjälä  * map to DDI A,TC1,TC2,TC3,TC4 respectively.
36405a0fc7a0SVille Syrjälä  */
36415a0fc7a0SVille Syrjälä static const u8 adls_aux_ch_map[] = {
36425a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
36435a0fc7a0SVille Syrjälä 	[AUX_CH_USBC1] = DP_AUX_B,
36445a0fc7a0SVille Syrjälä 	[AUX_CH_USBC2] = DP_AUX_C,
36455a0fc7a0SVille Syrjälä 	[AUX_CH_USBC3] = DP_AUX_D,
36465a0fc7a0SVille Syrjälä 	[AUX_CH_USBC4] = DP_AUX_E,
36475a0fc7a0SVille Syrjälä };
3648df0566a6SJani Nikula 
364918c283dfSAditya Swarup /*
365018c283dfSAditya Swarup  * RKL/DG1 VBT uses PHY based mapping. Combo PHYs A,B,C,D
365118c283dfSAditya Swarup  * map to DDI A,B,TC1,TC2 respectively.
365218c283dfSAditya Swarup  */
36535a0fc7a0SVille Syrjälä static const u8 rkl_aux_ch_map[] = {
36545a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
36555a0fc7a0SVille Syrjälä 	[AUX_CH_B] = DP_AUX_B,
36565a0fc7a0SVille Syrjälä 	[AUX_CH_USBC1] = DP_AUX_C,
36575a0fc7a0SVille Syrjälä 	[AUX_CH_USBC2] = DP_AUX_D,
36585a0fc7a0SVille Syrjälä };
36595a0fc7a0SVille Syrjälä 
36605a0fc7a0SVille Syrjälä static const u8 direct_aux_ch_map[] = {
36615a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
36625a0fc7a0SVille Syrjälä 	[AUX_CH_B] = DP_AUX_B,
36635a0fc7a0SVille Syrjälä 	[AUX_CH_C] = DP_AUX_C,
36645a0fc7a0SVille Syrjälä 	[AUX_CH_D] = DP_AUX_D, /* aka AUX_CH_USBC1 */
36655a0fc7a0SVille Syrjälä 	[AUX_CH_E] = DP_AUX_E, /* aka AUX_CH_USBC2 */
36665a0fc7a0SVille Syrjälä 	[AUX_CH_F] = DP_AUX_F, /* aka AUX_CH_USBC3 */
36675a0fc7a0SVille Syrjälä 	[AUX_CH_G] = DP_AUX_G, /* aka AUX_CH_USBC4 */
36685a0fc7a0SVille Syrjälä 	[AUX_CH_H] = DP_AUX_H, /* aka AUX_CH_USBC5 */
36695a0fc7a0SVille Syrjälä 	[AUX_CH_I] = DP_AUX_I, /* aka AUX_CH_USBC6 */
36705a0fc7a0SVille Syrjälä };
36715a0fc7a0SVille Syrjälä 
map_aux_ch(struct intel_display * display,u8 aux_channel)36729aec6f76SJani Nikula static enum aux_ch map_aux_ch(struct intel_display *display, u8 aux_channel)
36735a0fc7a0SVille Syrjälä {
36749aec6f76SJani Nikula 	struct drm_i915_private *i915 = to_i915(display->drm);
36755a0fc7a0SVille Syrjälä 	const u8 *aux_ch_map;
36765a0fc7a0SVille Syrjälä 	int i, n_entries;
36775a0fc7a0SVille Syrjälä 
36789aec6f76SJani Nikula 	if (DISPLAY_VER(display) >= 13) {
36795a0fc7a0SVille Syrjälä 		aux_ch_map = adlp_aux_ch_map;
36805a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(adlp_aux_ch_map);
36815a0fc7a0SVille Syrjälä 	} else if (IS_ALDERLAKE_S(i915)) {
36825a0fc7a0SVille Syrjälä 		aux_ch_map = adls_aux_ch_map;
36835a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(adls_aux_ch_map);
36845a0fc7a0SVille Syrjälä 	} else if (IS_DG1(i915) || IS_ROCKETLAKE(i915)) {
36855a0fc7a0SVille Syrjälä 		aux_ch_map = rkl_aux_ch_map;
36865a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(rkl_aux_ch_map);
36875a0fc7a0SVille Syrjälä 	} else {
36885a0fc7a0SVille Syrjälä 		aux_ch_map = direct_aux_ch_map;
36895a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(direct_aux_ch_map);
3690df0566a6SJani Nikula 	}
3691df0566a6SJani Nikula 
36925a0fc7a0SVille Syrjälä 	for (i = 0; i < n_entries; i++) {
36935a0fc7a0SVille Syrjälä 		if (aux_ch_map[i] == aux_channel)
36945a0fc7a0SVille Syrjälä 			return i;
36955a0fc7a0SVille Syrjälä 	}
36965a0fc7a0SVille Syrjälä 
36979aec6f76SJani Nikula 	drm_dbg_kms(display->drm,
36985a0fc7a0SVille Syrjälä 		    "Ignoring alternate AUX CH: VBT claims AUX 0x%x, which is not valid for this platform\n",
36995a0fc7a0SVille Syrjälä 		    aux_channel);
37005a0fc7a0SVille Syrjälä 
37015a0fc7a0SVille Syrjälä 	return AUX_CH_NONE;
3702df0566a6SJani Nikula }
3703d9ee2111SJani Nikula 
intel_bios_dp_aux_ch(const struct intel_bios_encoder_data * devdata)3704bb45217fSVille Syrjälä enum aux_ch intel_bios_dp_aux_ch(const struct intel_bios_encoder_data *devdata)
3705bb45217fSVille Syrjälä {
3706bb45217fSVille Syrjälä 	if (!devdata || !devdata->child.aux_channel)
3707bb45217fSVille Syrjälä 		return AUX_CH_NONE;
3708bb45217fSVille Syrjälä 
37099aec6f76SJani Nikula 	return map_aux_ch(devdata->display, devdata->child.aux_channel);
3710bb45217fSVille Syrjälä }
3711d9ee2111SJani Nikula 
intel_bios_dp_has_shared_aux_ch(const struct intel_bios_encoder_data * devdata)371270052100SVille Syrjälä bool intel_bios_dp_has_shared_aux_ch(const struct intel_bios_encoder_data *devdata)
371370052100SVille Syrjälä {
37149aec6f76SJani Nikula 	struct intel_display *display;
371570052100SVille Syrjälä 	u8 aux_channel;
371670052100SVille Syrjälä 	int count = 0;
371770052100SVille Syrjälä 
371870052100SVille Syrjälä 	if (!devdata || !devdata->child.aux_channel)
371970052100SVille Syrjälä 		return false;
372070052100SVille Syrjälä 
37219aec6f76SJani Nikula 	display = devdata->display;
372270052100SVille Syrjälä 	aux_channel = devdata->child.aux_channel;
372370052100SVille Syrjälä 
37249aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
372570052100SVille Syrjälä 		if (intel_bios_encoder_supports_dp(devdata) &&
372670052100SVille Syrjälä 		    aux_channel == devdata->child.aux_channel)
372770052100SVille Syrjälä 			count++;
372870052100SVille Syrjälä 	}
372970052100SVille Syrjälä 
373070052100SVille Syrjälä 	return count > 1;
373170052100SVille Syrjälä }
373270052100SVille Syrjälä 
intel_bios_dp_boost_level(const struct intel_bios_encoder_data * devdata)373302107ef1SVille Syrjälä int intel_bios_dp_boost_level(const struct intel_bios_encoder_data *devdata)
3734605a1872SJani Nikula {
37359aec6f76SJani Nikula 	if (!devdata || devdata->display->vbt.version < 196 || !devdata->child.iboost)
3736c0a950d1SJani Nikula 		return 0;
3737605a1872SJani Nikula 
37389aec6f76SJani Nikula 	return translate_iboost(devdata->display, devdata->child.dp_iboost_level);
3739605a1872SJani Nikula }
374001a60883SJani Nikula 
intel_bios_hdmi_boost_level(const struct intel_bios_encoder_data * devdata)374102107ef1SVille Syrjälä int intel_bios_hdmi_boost_level(const struct intel_bios_encoder_data *devdata)
374201a60883SJani Nikula {
37439aec6f76SJani Nikula 	if (!devdata || devdata->display->vbt.version < 196 || !devdata->child.iboost)
3744c0a950d1SJani Nikula 		return 0;
374501a60883SJani Nikula 
37469aec6f76SJani Nikula 	return translate_iboost(devdata->display, devdata->child.hdmi_iboost_level);
374701a60883SJani Nikula }
3748f83acdabSJani Nikula 
intel_bios_hdmi_ddc_pin(const struct intel_bios_encoder_data * devdata)374902107ef1SVille Syrjälä int intel_bios_hdmi_ddc_pin(const struct intel_bios_encoder_data *devdata)
3750f83acdabSJani Nikula {
3751dab8477bSJani Nikula 	if (!devdata || !devdata->child.ddc_pin)
3752dab8477bSJani Nikula 		return 0;
3753dab8477bSJani Nikula 
37549aec6f76SJani Nikula 	return map_ddc_pin(devdata->display, devdata->child.ddc_pin);
375517004bfbSJani Nikula }
3756c5faae5aSJani Nikula 
intel_bios_encoder_supports_typec_usb(const struct intel_bios_encoder_data * devdata)3757f08fbe6aSJani Nikula bool intel_bios_encoder_supports_typec_usb(const struct intel_bios_encoder_data *devdata)
3758c5faae5aSJani Nikula {
37599aec6f76SJani Nikula 	return devdata->display->vbt.version >= 195 && devdata->child.dp_usb_type_c;
3760c5faae5aSJani Nikula }
3761c5faae5aSJani Nikula 
intel_bios_encoder_supports_tbt(const struct intel_bios_encoder_data * devdata)3762f08fbe6aSJani Nikula bool intel_bios_encoder_supports_tbt(const struct intel_bios_encoder_data *devdata)
3763c5faae5aSJani Nikula {
37649aec6f76SJani Nikula 	return devdata->display->vbt.version >= 209 && devdata->child.tbt;
3765c5faae5aSJani Nikula }
376645c0673aSJani Nikula 
intel_bios_encoder_lane_reversal(const struct intel_bios_encoder_data * devdata)37675f42196dSVille Syrjälä bool intel_bios_encoder_lane_reversal(const struct intel_bios_encoder_data *devdata)
37685f42196dSVille Syrjälä {
37695f42196dSVille Syrjälä 	return devdata && devdata->child.lane_reversal;
37705f42196dSVille Syrjälä }
37715f42196dSVille Syrjälä 
intel_bios_encoder_hpd_invert(const struct intel_bios_encoder_data * devdata)37729151c85cSVille Syrjälä bool intel_bios_encoder_hpd_invert(const struct intel_bios_encoder_data *devdata)
37739151c85cSVille Syrjälä {
37749151c85cSVille Syrjälä 	return devdata && devdata->child.hpd_invert;
37759151c85cSVille Syrjälä }
37769151c85cSVille Syrjälä 
377745c0673aSJani Nikula const struct intel_bios_encoder_data *
intel_bios_encoder_data_lookup(struct intel_display * display,enum port port)37789aec6f76SJani Nikula intel_bios_encoder_data_lookup(struct intel_display *display, enum port port)
377945c0673aSJani Nikula {
3780021a62a5SVille Syrjälä 	struct intel_bios_encoder_data *devdata;
3781021a62a5SVille Syrjälä 
37829aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node) {
3783021a62a5SVille Syrjälä 		if (intel_bios_encoder_port(devdata) == port)
3784021a62a5SVille Syrjälä 			return devdata;
3785021a62a5SVille Syrjälä 	}
3786021a62a5SVille Syrjälä 
3787021a62a5SVille Syrjälä 	return NULL;
3788021a62a5SVille Syrjälä }
3789021a62a5SVille Syrjälä 
intel_bios_for_each_encoder(struct intel_display * display,void (* func)(struct intel_display * display,const struct intel_bios_encoder_data * devdata))37909aec6f76SJani Nikula void intel_bios_for_each_encoder(struct intel_display *display,
37919aec6f76SJani Nikula 				 void (*func)(struct intel_display *display,
3792021a62a5SVille Syrjälä 					      const struct intel_bios_encoder_data *devdata))
3793021a62a5SVille Syrjälä {
3794021a62a5SVille Syrjälä 	struct intel_bios_encoder_data *devdata;
3795021a62a5SVille Syrjälä 
37969aec6f76SJani Nikula 	list_for_each_entry(devdata, &display->vbt.display_devices, node)
37979aec6f76SJani Nikula 		func(display, devdata);
379845c0673aSJani Nikula }
379930ef2627SJani Nikula 
intel_bios_vbt_show(struct seq_file * m,void * unused)380030ef2627SJani Nikula static int intel_bios_vbt_show(struct seq_file *m, void *unused)
380130ef2627SJani Nikula {
38029aec6f76SJani Nikula 	struct intel_display *display = m->private;
380337e21003SJani Nikula 	const void *vbt;
380437e21003SJani Nikula 	size_t vbt_size;
380530ef2627SJani Nikula 
38069aec6f76SJani Nikula 	vbt = intel_bios_get_vbt(display, &vbt_size);
3807a2596003SRadhakrishna Sripada 
3808a2596003SRadhakrishna Sripada 	if (vbt) {
380937e21003SJani Nikula 		seq_write(m, vbt, vbt_size);
3810a2596003SRadhakrishna Sripada 		kfree(vbt);
3811a2596003SRadhakrishna Sripada 	}
381230ef2627SJani Nikula 
381330ef2627SJani Nikula 	return 0;
381430ef2627SJani Nikula }
381530ef2627SJani Nikula 
381630ef2627SJani Nikula DEFINE_SHOW_ATTRIBUTE(intel_bios_vbt);
381730ef2627SJani Nikula 
intel_bios_debugfs_register(struct intel_display * display)38189aec6f76SJani Nikula void intel_bios_debugfs_register(struct intel_display *display)
381930ef2627SJani Nikula {
38209aec6f76SJani Nikula 	struct drm_minor *minor = display->drm->primary;
382130ef2627SJani Nikula 
382230ef2627SJani Nikula 	debugfs_create_file("i915_vbt", 0444, minor->debugfs_root,
38239aec6f76SJani Nikula 			    display, &intel_bios_vbt_fops);
382430ef2627SJani Nikula }
3825