xref: /linux/drivers/gpu/drm/i915/display/intel_bios.c (revision ef0096e40139f40d96d36b753d09664bc0708432)
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 
28df0566a6SJani Nikula #include <drm/drm_dp_helper.h>
29df0566a6SJani Nikula 
30d8fe2ab6SMatt Roper #include "display/intel_display.h"
311bf2f3bfSJani Nikula #include "display/intel_display_types.h"
32df0566a6SJani Nikula #include "display/intel_gmbus.h"
33df0566a6SJani Nikula 
34df0566a6SJani Nikula #include "i915_drv.h"
35df0566a6SJani Nikula 
36df0566a6SJani Nikula #define _INTEL_BIOS_PRIVATE
37df0566a6SJani Nikula #include "intel_vbt_defs.h"
38df0566a6SJani Nikula 
39df0566a6SJani Nikula /**
40df0566a6SJani Nikula  * DOC: Video BIOS Table (VBT)
41df0566a6SJani Nikula  *
42df0566a6SJani Nikula  * The Video BIOS Table, or VBT, provides platform and board specific
43df0566a6SJani Nikula  * configuration information to the driver that is not discoverable or available
44df0566a6SJani Nikula  * through other means. The configuration is mostly related to display
45df0566a6SJani Nikula  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
46df0566a6SJani Nikula  * the PCI ROM.
47df0566a6SJani Nikula  *
48df0566a6SJani Nikula  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
49df0566a6SJani Nikula  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
50df0566a6SJani Nikula  * contain the actual configuration information. The VBT Header, and thus the
51df0566a6SJani Nikula  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
52df0566a6SJani Nikula  * BDB Header. The data blocks are concatenated after the BDB Header. The data
53df0566a6SJani Nikula  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
54df0566a6SJani Nikula  * data. (Block 53, the MIPI Sequence Block is an exception.)
55df0566a6SJani Nikula  *
56df0566a6SJani Nikula  * The driver parses the VBT during load. The relevant information is stored in
57df0566a6SJani Nikula  * driver private data for ease of use, and the actual VBT is not read after
58df0566a6SJani Nikula  * that.
59df0566a6SJani Nikula  */
60df0566a6SJani Nikula 
610d9ef19bSJani Nikula /* Wrapper for VBT child device config */
620d9ef19bSJani Nikula struct display_device_data {
630d9ef19bSJani Nikula 	struct child_device_config child;
646e0d46e9SJani Nikula 	struct dsc_compression_parameters_entry *dsc;
650d9ef19bSJani Nikula 	struct list_head node;
660d9ef19bSJani Nikula };
670d9ef19bSJani Nikula 
68df0566a6SJani Nikula #define	SLAVE_ADDR1	0x70
69df0566a6SJani Nikula #define	SLAVE_ADDR2	0x72
70df0566a6SJani Nikula 
71df0566a6SJani Nikula /* Get BDB block size given a pointer to Block ID. */
72df0566a6SJani Nikula static u32 _get_blocksize(const u8 *block_base)
73df0566a6SJani Nikula {
74df0566a6SJani Nikula 	/* The MIPI Sequence Block v3+ has a separate size field. */
75df0566a6SJani Nikula 	if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
76df0566a6SJani Nikula 		return *((const u32 *)(block_base + 4));
77df0566a6SJani Nikula 	else
78df0566a6SJani Nikula 		return *((const u16 *)(block_base + 1));
79df0566a6SJani Nikula }
80df0566a6SJani Nikula 
81df0566a6SJani Nikula /* Get BDB block size give a pointer to data after Block ID and Block Size. */
82df0566a6SJani Nikula static u32 get_blocksize(const void *block_data)
83df0566a6SJani Nikula {
84df0566a6SJani Nikula 	return _get_blocksize(block_data - 3);
85df0566a6SJani Nikula }
86df0566a6SJani Nikula 
87df0566a6SJani Nikula static const void *
88df0566a6SJani Nikula find_section(const void *_bdb, enum bdb_block_id section_id)
89df0566a6SJani Nikula {
90df0566a6SJani Nikula 	const struct bdb_header *bdb = _bdb;
91df0566a6SJani Nikula 	const u8 *base = _bdb;
92df0566a6SJani Nikula 	int index = 0;
93df0566a6SJani Nikula 	u32 total, current_size;
94df0566a6SJani Nikula 	enum bdb_block_id current_id;
95df0566a6SJani Nikula 
96df0566a6SJani Nikula 	/* skip to first section */
97df0566a6SJani Nikula 	index += bdb->header_size;
98df0566a6SJani Nikula 	total = bdb->bdb_size;
99df0566a6SJani Nikula 
100df0566a6SJani Nikula 	/* walk the sections looking for section_id */
101df0566a6SJani Nikula 	while (index + 3 < total) {
102df0566a6SJani Nikula 		current_id = *(base + index);
103df0566a6SJani Nikula 		current_size = _get_blocksize(base + index);
104df0566a6SJani Nikula 		index += 3;
105df0566a6SJani Nikula 
106df0566a6SJani Nikula 		if (index + current_size > total)
107df0566a6SJani Nikula 			return NULL;
108df0566a6SJani Nikula 
109df0566a6SJani Nikula 		if (current_id == section_id)
110df0566a6SJani Nikula 			return base + index;
111df0566a6SJani Nikula 
112df0566a6SJani Nikula 		index += current_size;
113df0566a6SJani Nikula 	}
114df0566a6SJani Nikula 
115df0566a6SJani Nikula 	return NULL;
116df0566a6SJani Nikula }
117df0566a6SJani Nikula 
118df0566a6SJani Nikula static void
119df0566a6SJani Nikula fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
120df0566a6SJani Nikula 			const struct lvds_dvo_timing *dvo_timing)
121df0566a6SJani Nikula {
122df0566a6SJani Nikula 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
123df0566a6SJani Nikula 		dvo_timing->hactive_lo;
124df0566a6SJani Nikula 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
125df0566a6SJani Nikula 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
126df0566a6SJani Nikula 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
127df0566a6SJani Nikula 		((dvo_timing->hsync_pulse_width_hi << 8) |
128df0566a6SJani Nikula 			dvo_timing->hsync_pulse_width_lo);
129df0566a6SJani Nikula 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
130df0566a6SJani Nikula 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
131df0566a6SJani Nikula 
132df0566a6SJani Nikula 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
133df0566a6SJani Nikula 		dvo_timing->vactive_lo;
134df0566a6SJani Nikula 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
135df0566a6SJani Nikula 		((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
136df0566a6SJani Nikula 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
137df0566a6SJani Nikula 		((dvo_timing->vsync_pulse_width_hi << 4) |
138df0566a6SJani Nikula 			dvo_timing->vsync_pulse_width_lo);
139df0566a6SJani Nikula 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
140df0566a6SJani Nikula 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
141df0566a6SJani Nikula 	panel_fixed_mode->clock = dvo_timing->clock * 10;
142df0566a6SJani Nikula 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
143df0566a6SJani Nikula 
144df0566a6SJani Nikula 	if (dvo_timing->hsync_positive)
145df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
146df0566a6SJani Nikula 	else
147df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
148df0566a6SJani Nikula 
149df0566a6SJani Nikula 	if (dvo_timing->vsync_positive)
150df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
151df0566a6SJani Nikula 	else
152df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
153df0566a6SJani Nikula 
154df0566a6SJani Nikula 	panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
155df0566a6SJani Nikula 		dvo_timing->himage_lo;
156df0566a6SJani Nikula 	panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
157df0566a6SJani Nikula 		dvo_timing->vimage_lo;
158df0566a6SJani Nikula 
159df0566a6SJani Nikula 	/* Some VBTs have bogus h/vtotal values */
160df0566a6SJani Nikula 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
161df0566a6SJani Nikula 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
162df0566a6SJani Nikula 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
163df0566a6SJani Nikula 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
164df0566a6SJani Nikula 
165df0566a6SJani Nikula 	drm_mode_set_name(panel_fixed_mode);
166df0566a6SJani Nikula }
167df0566a6SJani Nikula 
168df0566a6SJani Nikula static const struct lvds_dvo_timing *
169df0566a6SJani Nikula get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *lvds_lfp_data,
170df0566a6SJani Nikula 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs,
171df0566a6SJani Nikula 		    int index)
172df0566a6SJani Nikula {
173df0566a6SJani Nikula 	/*
174df0566a6SJani Nikula 	 * the size of fp_timing varies on the different platform.
175df0566a6SJani Nikula 	 * So calculate the DVO timing relative offset in LVDS data
176df0566a6SJani Nikula 	 * entry to get the DVO timing entry
177df0566a6SJani Nikula 	 */
178df0566a6SJani Nikula 
179df0566a6SJani Nikula 	int lfp_data_size =
180df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[1].dvo_timing_offset -
181df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset;
182df0566a6SJani Nikula 	int dvo_timing_offset =
183df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset -
184df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[0].fp_timing_offset;
185df0566a6SJani Nikula 	char *entry = (char *)lvds_lfp_data->data + lfp_data_size * index;
186df0566a6SJani Nikula 
187df0566a6SJani Nikula 	return (struct lvds_dvo_timing *)(entry + dvo_timing_offset);
188df0566a6SJani Nikula }
189df0566a6SJani Nikula 
190df0566a6SJani Nikula /* get lvds_fp_timing entry
191df0566a6SJani Nikula  * this function may return NULL if the corresponding entry is invalid
192df0566a6SJani Nikula  */
193df0566a6SJani Nikula static const struct lvds_fp_timing *
194df0566a6SJani Nikula get_lvds_fp_timing(const struct bdb_header *bdb,
195df0566a6SJani Nikula 		   const struct bdb_lvds_lfp_data *data,
196df0566a6SJani Nikula 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
197df0566a6SJani Nikula 		   int index)
198df0566a6SJani Nikula {
199df0566a6SJani Nikula 	size_t data_ofs = (const u8 *)data - (const u8 *)bdb;
200df0566a6SJani Nikula 	u16 data_size = ((const u16 *)data)[-1]; /* stored in header */
201df0566a6SJani Nikula 	size_t ofs;
202df0566a6SJani Nikula 
203df0566a6SJani Nikula 	if (index >= ARRAY_SIZE(ptrs->ptr))
204df0566a6SJani Nikula 		return NULL;
205df0566a6SJani Nikula 	ofs = ptrs->ptr[index].fp_timing_offset;
206df0566a6SJani Nikula 	if (ofs < data_ofs ||
207df0566a6SJani Nikula 	    ofs + sizeof(struct lvds_fp_timing) > data_ofs + data_size)
208df0566a6SJani Nikula 		return NULL;
209df0566a6SJani Nikula 	return (const struct lvds_fp_timing *)((const u8 *)bdb + ofs);
210df0566a6SJani Nikula }
211df0566a6SJani Nikula 
2129e7ecedfSMatt Roper /* Parse general panel options */
213df0566a6SJani Nikula static void
214dbd440d8SJani Nikula parse_panel_options(struct drm_i915_private *i915,
215df0566a6SJani Nikula 		    const struct bdb_header *bdb)
216df0566a6SJani Nikula {
217df0566a6SJani Nikula 	const struct bdb_lvds_options *lvds_options;
218df0566a6SJani Nikula 	int panel_type;
219df0566a6SJani Nikula 	int drrs_mode;
220df0566a6SJani Nikula 	int ret;
221df0566a6SJani Nikula 
222df0566a6SJani Nikula 	lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
223df0566a6SJani Nikula 	if (!lvds_options)
224df0566a6SJani Nikula 		return;
225df0566a6SJani Nikula 
226dbd440d8SJani Nikula 	i915->vbt.lvds_dither = lvds_options->pixel_dither;
227df0566a6SJani Nikula 
228dbd440d8SJani Nikula 	ret = intel_opregion_get_panel_type(i915);
229df0566a6SJani Nikula 	if (ret >= 0) {
230dbd440d8SJani Nikula 		drm_WARN_ON(&i915->drm, ret > 0xf);
231df0566a6SJani Nikula 		panel_type = ret;
232dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Panel type: %d (OpRegion)\n",
233e92cbf38SWambui Karuga 			    panel_type);
234df0566a6SJani Nikula 	} else {
235df0566a6SJani Nikula 		if (lvds_options->panel_type > 0xf) {
236dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
237e92cbf38SWambui Karuga 				    "Invalid VBT panel type 0x%x\n",
238df0566a6SJani Nikula 				    lvds_options->panel_type);
239df0566a6SJani Nikula 			return;
240df0566a6SJani Nikula 		}
241df0566a6SJani Nikula 		panel_type = lvds_options->panel_type;
242dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Panel type: %d (VBT)\n",
243e92cbf38SWambui Karuga 			    panel_type);
244df0566a6SJani Nikula 	}
245df0566a6SJani Nikula 
246dbd440d8SJani Nikula 	i915->vbt.panel_type = panel_type;
247df0566a6SJani Nikula 
248df0566a6SJani Nikula 	drrs_mode = (lvds_options->dps_panel_type_bits
249df0566a6SJani Nikula 				>> (panel_type * 2)) & MODE_MASK;
250df0566a6SJani Nikula 	/*
251df0566a6SJani Nikula 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
252df0566a6SJani Nikula 	 * The below piece of code is required to adjust vbt.drrs_type
253df0566a6SJani Nikula 	 * to match the enum drrs_support_type.
254df0566a6SJani Nikula 	 */
255df0566a6SJani Nikula 	switch (drrs_mode) {
256df0566a6SJani Nikula 	case 0:
257dbd440d8SJani Nikula 		i915->vbt.drrs_type = STATIC_DRRS_SUPPORT;
258dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS supported mode is static\n");
259df0566a6SJani Nikula 		break;
260df0566a6SJani Nikula 	case 2:
261dbd440d8SJani Nikula 		i915->vbt.drrs_type = SEAMLESS_DRRS_SUPPORT;
262dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
263e92cbf38SWambui Karuga 			    "DRRS supported mode is seamless\n");
264df0566a6SJani Nikula 		break;
265df0566a6SJani Nikula 	default:
266dbd440d8SJani Nikula 		i915->vbt.drrs_type = DRRS_NOT_SUPPORTED;
267dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
268e92cbf38SWambui Karuga 			    "DRRS not supported (VBT input)\n");
269df0566a6SJani Nikula 		break;
270df0566a6SJani Nikula 	}
2719e7ecedfSMatt Roper }
2729e7ecedfSMatt Roper 
2739e7ecedfSMatt Roper /* Try to find integrated panel timing data */
2749e7ecedfSMatt Roper static void
275dbd440d8SJani Nikula parse_lfp_panel_dtd(struct drm_i915_private *i915,
2769e7ecedfSMatt Roper 		    const struct bdb_header *bdb)
2779e7ecedfSMatt Roper {
2789e7ecedfSMatt Roper 	const struct bdb_lvds_lfp_data *lvds_lfp_data;
2799e7ecedfSMatt Roper 	const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs;
2809e7ecedfSMatt Roper 	const struct lvds_dvo_timing *panel_dvo_timing;
2819e7ecedfSMatt Roper 	const struct lvds_fp_timing *fp_timing;
2829e7ecedfSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
283dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
284df0566a6SJani Nikula 
285df0566a6SJani Nikula 	lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
286df0566a6SJani Nikula 	if (!lvds_lfp_data)
287df0566a6SJani Nikula 		return;
288df0566a6SJani Nikula 
289df0566a6SJani Nikula 	lvds_lfp_data_ptrs = find_section(bdb, BDB_LVDS_LFP_DATA_PTRS);
290df0566a6SJani Nikula 	if (!lvds_lfp_data_ptrs)
291df0566a6SJani Nikula 		return;
292df0566a6SJani Nikula 
293df0566a6SJani Nikula 	panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
294df0566a6SJani Nikula 					       lvds_lfp_data_ptrs,
295df0566a6SJani Nikula 					       panel_type);
296df0566a6SJani Nikula 
297df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
298df0566a6SJani Nikula 	if (!panel_fixed_mode)
299df0566a6SJani Nikula 		return;
300df0566a6SJani Nikula 
301df0566a6SJani Nikula 	fill_detail_timing_data(panel_fixed_mode, panel_dvo_timing);
302df0566a6SJani Nikula 
303dbd440d8SJani Nikula 	i915->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
304df0566a6SJani Nikula 
305dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
306e92cbf38SWambui Karuga 		    "Found panel mode in BIOS VBT legacy lfp table:\n");
307df0566a6SJani Nikula 	drm_mode_debug_printmodeline(panel_fixed_mode);
308df0566a6SJani Nikula 
309df0566a6SJani Nikula 	fp_timing = get_lvds_fp_timing(bdb, lvds_lfp_data,
310df0566a6SJani Nikula 				       lvds_lfp_data_ptrs,
311df0566a6SJani Nikula 				       panel_type);
312df0566a6SJani Nikula 	if (fp_timing) {
313df0566a6SJani Nikula 		/* check the resolution, just to be sure */
314df0566a6SJani Nikula 		if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
315df0566a6SJani Nikula 		    fp_timing->y_res == panel_fixed_mode->vdisplay) {
316dbd440d8SJani Nikula 			i915->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
317dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
318e92cbf38SWambui Karuga 				    "VBT initial LVDS value %x\n",
319dbd440d8SJani Nikula 				    i915->vbt.bios_lvds_val);
320df0566a6SJani Nikula 		}
321df0566a6SJani Nikula 	}
322df0566a6SJani Nikula }
323df0566a6SJani Nikula 
324df0566a6SJani Nikula static void
325dbd440d8SJani Nikula parse_generic_dtd(struct drm_i915_private *i915,
32633ef6d4fSMatt Roper 		  const struct bdb_header *bdb)
32733ef6d4fSMatt Roper {
32833ef6d4fSMatt Roper 	const struct bdb_generic_dtd *generic_dtd;
32933ef6d4fSMatt Roper 	const struct generic_dtd_entry *dtd;
33033ef6d4fSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
33133ef6d4fSMatt Roper 	int num_dtd;
33233ef6d4fSMatt Roper 
33333ef6d4fSMatt Roper 	generic_dtd = find_section(bdb, BDB_GENERIC_DTD);
33433ef6d4fSMatt Roper 	if (!generic_dtd)
33533ef6d4fSMatt Roper 		return;
33633ef6d4fSMatt Roper 
33733ef6d4fSMatt Roper 	if (generic_dtd->gdtd_size < sizeof(struct generic_dtd_entry)) {
338dbd440d8SJani Nikula 		drm_err(&i915->drm, "GDTD size %u is too small.\n",
33933ef6d4fSMatt Roper 			generic_dtd->gdtd_size);
34033ef6d4fSMatt Roper 		return;
34133ef6d4fSMatt Roper 	} else if (generic_dtd->gdtd_size !=
34233ef6d4fSMatt Roper 		   sizeof(struct generic_dtd_entry)) {
343dbd440d8SJani Nikula 		drm_err(&i915->drm, "Unexpected GDTD size %u\n",
344e92cbf38SWambui Karuga 			generic_dtd->gdtd_size);
34533ef6d4fSMatt Roper 		/* DTD has unknown fields, but keep going */
34633ef6d4fSMatt Roper 	}
34733ef6d4fSMatt Roper 
34833ef6d4fSMatt Roper 	num_dtd = (get_blocksize(generic_dtd) -
34933ef6d4fSMatt Roper 		   sizeof(struct bdb_generic_dtd)) / generic_dtd->gdtd_size;
350dbd440d8SJani Nikula 	if (i915->vbt.panel_type >= num_dtd) {
351dbd440d8SJani Nikula 		drm_err(&i915->drm,
352e92cbf38SWambui Karuga 			"Panel type %d not found in table of %d DTD's\n",
353dbd440d8SJani Nikula 			i915->vbt.panel_type, num_dtd);
35433ef6d4fSMatt Roper 		return;
35533ef6d4fSMatt Roper 	}
35633ef6d4fSMatt Roper 
357dbd440d8SJani Nikula 	dtd = &generic_dtd->dtd[i915->vbt.panel_type];
35833ef6d4fSMatt Roper 
35933ef6d4fSMatt Roper 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
36033ef6d4fSMatt Roper 	if (!panel_fixed_mode)
36133ef6d4fSMatt Roper 		return;
36233ef6d4fSMatt Roper 
36333ef6d4fSMatt Roper 	panel_fixed_mode->hdisplay = dtd->hactive;
36433ef6d4fSMatt Roper 	panel_fixed_mode->hsync_start =
36533ef6d4fSMatt Roper 		panel_fixed_mode->hdisplay + dtd->hfront_porch;
36633ef6d4fSMatt Roper 	panel_fixed_mode->hsync_end =
36733ef6d4fSMatt Roper 		panel_fixed_mode->hsync_start + dtd->hsync;
368ad278f35SVandita Kulkarni 	panel_fixed_mode->htotal =
369ad278f35SVandita Kulkarni 		panel_fixed_mode->hdisplay + dtd->hblank;
37033ef6d4fSMatt Roper 
37133ef6d4fSMatt Roper 	panel_fixed_mode->vdisplay = dtd->vactive;
37233ef6d4fSMatt Roper 	panel_fixed_mode->vsync_start =
37333ef6d4fSMatt Roper 		panel_fixed_mode->vdisplay + dtd->vfront_porch;
37433ef6d4fSMatt Roper 	panel_fixed_mode->vsync_end =
37533ef6d4fSMatt Roper 		panel_fixed_mode->vsync_start + dtd->vsync;
376ad278f35SVandita Kulkarni 	panel_fixed_mode->vtotal =
377ad278f35SVandita Kulkarni 		panel_fixed_mode->vdisplay + dtd->vblank;
37833ef6d4fSMatt Roper 
37933ef6d4fSMatt Roper 	panel_fixed_mode->clock = dtd->pixel_clock;
38033ef6d4fSMatt Roper 	panel_fixed_mode->width_mm = dtd->width_mm;
38133ef6d4fSMatt Roper 	panel_fixed_mode->height_mm = dtd->height_mm;
38233ef6d4fSMatt Roper 
38333ef6d4fSMatt Roper 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
38433ef6d4fSMatt Roper 	drm_mode_set_name(panel_fixed_mode);
38533ef6d4fSMatt Roper 
38633ef6d4fSMatt Roper 	if (dtd->hsync_positive_polarity)
38733ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
38833ef6d4fSMatt Roper 	else
38933ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
39033ef6d4fSMatt Roper 
39133ef6d4fSMatt Roper 	if (dtd->vsync_positive_polarity)
39233ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
39333ef6d4fSMatt Roper 	else
39433ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
39533ef6d4fSMatt Roper 
396dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
397e92cbf38SWambui Karuga 		    "Found panel mode in BIOS VBT generic dtd table:\n");
39833ef6d4fSMatt Roper 	drm_mode_debug_printmodeline(panel_fixed_mode);
39933ef6d4fSMatt Roper 
400dbd440d8SJani Nikula 	i915->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
40133ef6d4fSMatt Roper }
40233ef6d4fSMatt Roper 
40333ef6d4fSMatt Roper static void
404dbd440d8SJani Nikula parse_panel_dtd(struct drm_i915_private *i915,
40533ef6d4fSMatt Roper 		const struct bdb_header *bdb)
40633ef6d4fSMatt Roper {
40733ef6d4fSMatt Roper 	/*
40833ef6d4fSMatt Roper 	 * Older VBTs provided provided DTD information for internal displays
40933ef6d4fSMatt Roper 	 * through the "LFP panel DTD" block (42).  As of VBT revision 229,
41033ef6d4fSMatt Roper 	 * that block is now deprecated and DTD information should be provided
41133ef6d4fSMatt Roper 	 * via a newer "generic DTD" block (58).  Just to be safe, we'll
41233ef6d4fSMatt Roper 	 * try the new generic DTD block first on VBT >= 229, but still fall
41333ef6d4fSMatt Roper 	 * back to trying the old LFP block if that fails.
41433ef6d4fSMatt Roper 	 */
41533ef6d4fSMatt Roper 	if (bdb->version >= 229)
416dbd440d8SJani Nikula 		parse_generic_dtd(i915, bdb);
417dbd440d8SJani Nikula 	if (!i915->vbt.lfp_lvds_vbt_mode)
418dbd440d8SJani Nikula 		parse_lfp_panel_dtd(i915, bdb);
41933ef6d4fSMatt Roper }
42033ef6d4fSMatt Roper 
42133ef6d4fSMatt Roper static void
422dbd440d8SJani Nikula parse_lfp_backlight(struct drm_i915_private *i915,
423df0566a6SJani Nikula 		    const struct bdb_header *bdb)
424df0566a6SJani Nikula {
425df0566a6SJani Nikula 	const struct bdb_lfp_backlight_data *backlight_data;
426df0566a6SJani Nikula 	const struct lfp_backlight_data_entry *entry;
427dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
428d381baadSJosé Roberto de Souza 	u16 level;
429df0566a6SJani Nikula 
430df0566a6SJani Nikula 	backlight_data = find_section(bdb, BDB_LVDS_BACKLIGHT);
431df0566a6SJani Nikula 	if (!backlight_data)
432df0566a6SJani Nikula 		return;
433df0566a6SJani Nikula 
434df0566a6SJani Nikula 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
435dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
436e92cbf38SWambui Karuga 			    "Unsupported backlight data entry size %u\n",
437df0566a6SJani Nikula 			    backlight_data->entry_size);
438df0566a6SJani Nikula 		return;
439df0566a6SJani Nikula 	}
440df0566a6SJani Nikula 
441df0566a6SJani Nikula 	entry = &backlight_data->data[panel_type];
442df0566a6SJani Nikula 
443dbd440d8SJani Nikula 	i915->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
444dbd440d8SJani Nikula 	if (!i915->vbt.backlight.present) {
445dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
446e92cbf38SWambui Karuga 			    "PWM backlight not present in VBT (type %u)\n",
447df0566a6SJani Nikula 			    entry->type);
448df0566a6SJani Nikula 		return;
449df0566a6SJani Nikula 	}
450df0566a6SJani Nikula 
451dbd440d8SJani Nikula 	i915->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
452df0566a6SJani Nikula 	if (bdb->version >= 191 &&
453df0566a6SJani Nikula 	    get_blocksize(backlight_data) >= sizeof(*backlight_data)) {
454df0566a6SJani Nikula 		const struct lfp_backlight_control_method *method;
455df0566a6SJani Nikula 
456df0566a6SJani Nikula 		method = &backlight_data->backlight_control[panel_type];
457dbd440d8SJani Nikula 		i915->vbt.backlight.type = method->type;
458dbd440d8SJani Nikula 		i915->vbt.backlight.controller = method->controller;
459df0566a6SJani Nikula 	}
460df0566a6SJani Nikula 
461dbd440d8SJani Nikula 	i915->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
462dbd440d8SJani Nikula 	i915->vbt.backlight.active_low_pwm = entry->active_low_pwm;
463d381baadSJosé Roberto de Souza 
464d381baadSJosé Roberto de Souza 	if (bdb->version >= 234) {
465d381baadSJosé Roberto de Souza 		u16 min_level;
466d381baadSJosé Roberto de Souza 		bool scale;
467d381baadSJosé Roberto de Souza 
468d381baadSJosé Roberto de Souza 		level = backlight_data->brightness_level[panel_type].level;
469d381baadSJosé Roberto de Souza 		min_level = backlight_data->brightness_min_level[panel_type].level;
470d381baadSJosé Roberto de Souza 
471d381baadSJosé Roberto de Souza 		if (bdb->version >= 236)
472d381baadSJosé Roberto de Souza 			scale = backlight_data->brightness_precision_bits[panel_type] == 16;
473d381baadSJosé Roberto de Souza 		else
474d381baadSJosé Roberto de Souza 			scale = level > 255;
475d381baadSJosé Roberto de Souza 
476d381baadSJosé Roberto de Souza 		if (scale)
477d381baadSJosé Roberto de Souza 			min_level = min_level / 255;
478d381baadSJosé Roberto de Souza 
479d381baadSJosé Roberto de Souza 		if (min_level > 255) {
480dbd440d8SJani Nikula 			drm_warn(&i915->drm, "Brightness min level > 255\n");
481d381baadSJosé Roberto de Souza 			level = 255;
482d381baadSJosé Roberto de Souza 		}
483dbd440d8SJani Nikula 		i915->vbt.backlight.min_brightness = min_level;
484d381baadSJosé Roberto de Souza 	} else {
485d381baadSJosé Roberto de Souza 		level = backlight_data->level[panel_type];
486dbd440d8SJani Nikula 		i915->vbt.backlight.min_brightness = entry->min_brightness;
487d381baadSJosé Roberto de Souza 	}
488d381baadSJosé Roberto de Souza 
489dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
490e92cbf38SWambui Karuga 		    "VBT backlight PWM modulation frequency %u Hz, "
491df0566a6SJani Nikula 		    "active %s, min brightness %u, level %u, controller %u\n",
492dbd440d8SJani Nikula 		    i915->vbt.backlight.pwm_freq_hz,
493dbd440d8SJani Nikula 		    i915->vbt.backlight.active_low_pwm ? "low" : "high",
494dbd440d8SJani Nikula 		    i915->vbt.backlight.min_brightness,
495d381baadSJosé Roberto de Souza 		    level,
496dbd440d8SJani Nikula 		    i915->vbt.backlight.controller);
497df0566a6SJani Nikula }
498df0566a6SJani Nikula 
499df0566a6SJani Nikula /* Try to find sdvo panel data */
500df0566a6SJani Nikula static void
501dbd440d8SJani Nikula parse_sdvo_panel_data(struct drm_i915_private *i915,
502df0566a6SJani Nikula 		      const struct bdb_header *bdb)
503df0566a6SJani Nikula {
504df0566a6SJani Nikula 	const struct bdb_sdvo_panel_dtds *dtds;
505df0566a6SJani Nikula 	struct drm_display_mode *panel_fixed_mode;
506df0566a6SJani Nikula 	int index;
507df0566a6SJani Nikula 
508dbd440d8SJani Nikula 	index = i915->params.vbt_sdvo_panel_type;
509df0566a6SJani Nikula 	if (index == -2) {
510dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
511e92cbf38SWambui Karuga 			    "Ignore SDVO panel mode from BIOS VBT tables.\n");
512df0566a6SJani Nikula 		return;
513df0566a6SJani Nikula 	}
514df0566a6SJani Nikula 
515df0566a6SJani Nikula 	if (index == -1) {
516df0566a6SJani Nikula 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
517df0566a6SJani Nikula 
518df0566a6SJani Nikula 		sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
519df0566a6SJani Nikula 		if (!sdvo_lvds_options)
520df0566a6SJani Nikula 			return;
521df0566a6SJani Nikula 
522df0566a6SJani Nikula 		index = sdvo_lvds_options->panel_type;
523df0566a6SJani Nikula 	}
524df0566a6SJani Nikula 
525df0566a6SJani Nikula 	dtds = find_section(bdb, BDB_SDVO_PANEL_DTDS);
526df0566a6SJani Nikula 	if (!dtds)
527df0566a6SJani Nikula 		return;
528df0566a6SJani Nikula 
529df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
530df0566a6SJani Nikula 	if (!panel_fixed_mode)
531df0566a6SJani Nikula 		return;
532df0566a6SJani Nikula 
533df0566a6SJani Nikula 	fill_detail_timing_data(panel_fixed_mode, &dtds->dtds[index]);
534df0566a6SJani Nikula 
535dbd440d8SJani Nikula 	i915->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
536df0566a6SJani Nikula 
537dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
538e92cbf38SWambui Karuga 		    "Found SDVO panel mode in BIOS VBT tables:\n");
539df0566a6SJani Nikula 	drm_mode_debug_printmodeline(panel_fixed_mode);
540df0566a6SJani Nikula }
541df0566a6SJani Nikula 
542dbd440d8SJani Nikula static int intel_bios_ssc_frequency(struct drm_i915_private *i915,
543df0566a6SJani Nikula 				    bool alternate)
544df0566a6SJani Nikula {
545dbd440d8SJani Nikula 	switch (INTEL_GEN(i915)) {
546df0566a6SJani Nikula 	case 2:
547df0566a6SJani Nikula 		return alternate ? 66667 : 48000;
548df0566a6SJani Nikula 	case 3:
549df0566a6SJani Nikula 	case 4:
550df0566a6SJani Nikula 		return alternate ? 100000 : 96000;
551df0566a6SJani Nikula 	default:
552df0566a6SJani Nikula 		return alternate ? 100000 : 120000;
553df0566a6SJani Nikula 	}
554df0566a6SJani Nikula }
555df0566a6SJani Nikula 
556df0566a6SJani Nikula static void
557dbd440d8SJani Nikula parse_general_features(struct drm_i915_private *i915,
558df0566a6SJani Nikula 		       const struct bdb_header *bdb)
559df0566a6SJani Nikula {
560df0566a6SJani Nikula 	const struct bdb_general_features *general;
561df0566a6SJani Nikula 
562df0566a6SJani Nikula 	general = find_section(bdb, BDB_GENERAL_FEATURES);
563df0566a6SJani Nikula 	if (!general)
564df0566a6SJani Nikula 		return;
565df0566a6SJani Nikula 
566dbd440d8SJani Nikula 	i915->vbt.int_tv_support = general->int_tv_support;
567df0566a6SJani Nikula 	/* int_crt_support can't be trusted on earlier platforms */
568df0566a6SJani Nikula 	if (bdb->version >= 155 &&
569dbd440d8SJani Nikula 	    (HAS_DDI(i915) || IS_VALLEYVIEW(i915)))
570dbd440d8SJani Nikula 		i915->vbt.int_crt_support = general->int_crt_support;
571dbd440d8SJani Nikula 	i915->vbt.lvds_use_ssc = general->enable_ssc;
572dbd440d8SJani Nikula 	i915->vbt.lvds_ssc_freq =
573dbd440d8SJani Nikula 		intel_bios_ssc_frequency(i915, general->ssc_freq);
574dbd440d8SJani Nikula 	i915->vbt.display_clock_mode = general->display_clock_mode;
575dbd440d8SJani Nikula 	i915->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
576df0566a6SJani Nikula 	if (bdb->version >= 181) {
577dbd440d8SJani Nikula 		i915->vbt.orientation = general->rotate_180 ?
578df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
579df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_NORMAL;
580df0566a6SJani Nikula 	} else {
581dbd440d8SJani Nikula 		i915->vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
582df0566a6SJani Nikula 	}
583dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
584e92cbf38SWambui 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",
585dbd440d8SJani Nikula 		    i915->vbt.int_tv_support,
586dbd440d8SJani Nikula 		    i915->vbt.int_crt_support,
587dbd440d8SJani Nikula 		    i915->vbt.lvds_use_ssc,
588dbd440d8SJani Nikula 		    i915->vbt.lvds_ssc_freq,
589dbd440d8SJani Nikula 		    i915->vbt.display_clock_mode,
590dbd440d8SJani Nikula 		    i915->vbt.fdi_rx_polarity_inverted);
591df0566a6SJani Nikula }
592df0566a6SJani Nikula 
593df0566a6SJani Nikula static const struct child_device_config *
594df0566a6SJani Nikula child_device_ptr(const struct bdb_general_definitions *defs, int i)
595df0566a6SJani Nikula {
596df0566a6SJani Nikula 	return (const void *) &defs->devices[i * defs->child_dev_size];
597df0566a6SJani Nikula }
598df0566a6SJani Nikula 
599df0566a6SJani Nikula static void
600*ef0096e4SJani Nikula parse_sdvo_device_mapping(struct drm_i915_private *i915)
601df0566a6SJani Nikula {
602df0566a6SJani Nikula 	struct sdvo_device_mapping *mapping;
6030d9ef19bSJani Nikula 	const struct display_device_data *devdata;
604df0566a6SJani Nikula 	const struct child_device_config *child;
6050d9ef19bSJani Nikula 	int count = 0;
606df0566a6SJani Nikula 
607df0566a6SJani Nikula 	/*
608df0566a6SJani Nikula 	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
609df0566a6SJani Nikula 	 * accurate and doesn't have to be, as long as it's not too strict.
610df0566a6SJani Nikula 	 */
611dbd440d8SJani Nikula 	if (!IS_GEN_RANGE(i915, 3, 7)) {
612dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Skipping SDVO device mapping\n");
613df0566a6SJani Nikula 		return;
614df0566a6SJani Nikula 	}
615df0566a6SJani Nikula 
616dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
6170d9ef19bSJani Nikula 		child = &devdata->child;
618df0566a6SJani Nikula 
619df0566a6SJani Nikula 		if (child->slave_addr != SLAVE_ADDR1 &&
620df0566a6SJani Nikula 		    child->slave_addr != SLAVE_ADDR2) {
621df0566a6SJani Nikula 			/*
622df0566a6SJani Nikula 			 * If the slave address is neither 0x70 nor 0x72,
623df0566a6SJani Nikula 			 * it is not a SDVO device. Skip it.
624df0566a6SJani Nikula 			 */
625df0566a6SJani Nikula 			continue;
626df0566a6SJani Nikula 		}
627df0566a6SJani Nikula 		if (child->dvo_port != DEVICE_PORT_DVOB &&
628df0566a6SJani Nikula 		    child->dvo_port != DEVICE_PORT_DVOC) {
629df0566a6SJani Nikula 			/* skip the incorrect SDVO port */
630dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
631e92cbf38SWambui Karuga 				    "Incorrect SDVO port. Skip it\n");
632df0566a6SJani Nikula 			continue;
633df0566a6SJani Nikula 		}
634dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
635e92cbf38SWambui Karuga 			    "the SDVO device with slave addr %2x is found on"
636df0566a6SJani Nikula 			    " %s port\n",
637df0566a6SJani Nikula 			    child->slave_addr,
638df0566a6SJani Nikula 			    (child->dvo_port == DEVICE_PORT_DVOB) ?
639df0566a6SJani Nikula 			    "SDVOB" : "SDVOC");
640dbd440d8SJani Nikula 		mapping = &i915->vbt.sdvo_mappings[child->dvo_port - 1];
641df0566a6SJani Nikula 		if (!mapping->initialized) {
642df0566a6SJani Nikula 			mapping->dvo_port = child->dvo_port;
643df0566a6SJani Nikula 			mapping->slave_addr = child->slave_addr;
644df0566a6SJani Nikula 			mapping->dvo_wiring = child->dvo_wiring;
645df0566a6SJani Nikula 			mapping->ddc_pin = child->ddc_pin;
646df0566a6SJani Nikula 			mapping->i2c_pin = child->i2c_pin;
647df0566a6SJani Nikula 			mapping->initialized = 1;
648dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
649e92cbf38SWambui Karuga 				    "SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
650e92cbf38SWambui Karuga 				    mapping->dvo_port, mapping->slave_addr,
651e92cbf38SWambui Karuga 				    mapping->dvo_wiring, mapping->ddc_pin,
652df0566a6SJani Nikula 				    mapping->i2c_pin);
653df0566a6SJani Nikula 		} else {
654dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
655e92cbf38SWambui Karuga 				    "Maybe one SDVO port is shared by "
656df0566a6SJani Nikula 				    "two SDVO device.\n");
657df0566a6SJani Nikula 		}
658df0566a6SJani Nikula 		if (child->slave2_addr) {
659df0566a6SJani Nikula 			/* Maybe this is a SDVO device with multiple inputs */
660df0566a6SJani Nikula 			/* And the mapping info is not added */
661dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
662e92cbf38SWambui Karuga 				    "there exists the slave2_addr. Maybe this"
663df0566a6SJani Nikula 				    " is a SDVO device with multiple inputs.\n");
664df0566a6SJani Nikula 		}
665df0566a6SJani Nikula 		count++;
666df0566a6SJani Nikula 	}
667df0566a6SJani Nikula 
668df0566a6SJani Nikula 	if (!count) {
669df0566a6SJani Nikula 		/* No SDVO device info is found */
670dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
671e92cbf38SWambui Karuga 			    "No SDVO device info is found in VBT\n");
672df0566a6SJani Nikula 	}
673df0566a6SJani Nikula }
674df0566a6SJani Nikula 
675df0566a6SJani Nikula static void
676dbd440d8SJani Nikula parse_driver_features(struct drm_i915_private *i915,
677df0566a6SJani Nikula 		      const struct bdb_header *bdb)
678df0566a6SJani Nikula {
679df0566a6SJani Nikula 	const struct bdb_driver_features *driver;
680df0566a6SJani Nikula 
681df0566a6SJani Nikula 	driver = find_section(bdb, BDB_DRIVER_FEATURES);
682df0566a6SJani Nikula 	if (!driver)
683df0566a6SJani Nikula 		return;
684df0566a6SJani Nikula 
685dbd440d8SJani Nikula 	if (INTEL_GEN(i915) >= 5) {
686df0566a6SJani Nikula 		/*
687df0566a6SJani Nikula 		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
688df0566a6SJani Nikula 		 * to mean "eDP". The VBT spec doesn't agree with that
689df0566a6SJani Nikula 		 * interpretation, but real world VBTs seem to.
690df0566a6SJani Nikula 		 */
691df0566a6SJani Nikula 		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
692dbd440d8SJani Nikula 			i915->vbt.int_lvds_support = 0;
693df0566a6SJani Nikula 	} else {
694df0566a6SJani Nikula 		/*
695df0566a6SJani Nikula 		 * FIXME it's not clear which BDB version has the LVDS config
696df0566a6SJani Nikula 		 * bits defined. Revision history in the VBT spec says:
697df0566a6SJani Nikula 		 * "0.92 | Add two definitions for VBT value of LVDS Active
698df0566a6SJani Nikula 		 *  Config (00b and 11b values defined) | 06/13/2005"
699df0566a6SJani Nikula 		 * but does not the specify the BDB version.
700df0566a6SJani Nikula 		 *
701df0566a6SJani Nikula 		 * So far version 134 (on i945gm) is the oldest VBT observed
702df0566a6SJani Nikula 		 * in the wild with the bits correctly populated. Version
703df0566a6SJani Nikula 		 * 108 (on i85x) does not have the bits correctly populated.
704df0566a6SJani Nikula 		 */
705df0566a6SJani Nikula 		if (bdb->version >= 134 &&
706df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
707df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
708dbd440d8SJani Nikula 			i915->vbt.int_lvds_support = 0;
709df0566a6SJani Nikula 	}
710df0566a6SJani Nikula 
711551fb93dSJosé Roberto de Souza 	if (bdb->version < 228) {
712dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS State Enabled:%d\n",
713e92cbf38SWambui Karuga 			    driver->drrs_enabled);
714df0566a6SJani Nikula 		/*
715df0566a6SJani Nikula 		 * If DRRS is not supported, drrs_type has to be set to 0.
716df0566a6SJani Nikula 		 * This is because, VBT is configured in such a way that
717df0566a6SJani Nikula 		 * static DRRS is 0 and DRRS not supported is represented by
718df0566a6SJani Nikula 		 * driver->drrs_enabled=false
719df0566a6SJani Nikula 		 */
720df0566a6SJani Nikula 		if (!driver->drrs_enabled)
721dbd440d8SJani Nikula 			i915->vbt.drrs_type = DRRS_NOT_SUPPORTED;
722551fb93dSJosé Roberto de Souza 
723dbd440d8SJani Nikula 		i915->vbt.psr.enable = driver->psr_enabled;
724df0566a6SJani Nikula 	}
725551fb93dSJosé Roberto de Souza }
726551fb93dSJosé Roberto de Souza 
727551fb93dSJosé Roberto de Souza static void
728dbd440d8SJani Nikula parse_power_conservation_features(struct drm_i915_private *i915,
729551fb93dSJosé Roberto de Souza 				  const struct bdb_header *bdb)
730551fb93dSJosé Roberto de Souza {
731551fb93dSJosé Roberto de Souza 	const struct bdb_lfp_power *power;
732dbd440d8SJani Nikula 	u8 panel_type = i915->vbt.panel_type;
733551fb93dSJosé Roberto de Souza 
734551fb93dSJosé Roberto de Souza 	if (bdb->version < 228)
735551fb93dSJosé Roberto de Souza 		return;
736551fb93dSJosé Roberto de Souza 
7374ec5abe9SJosé Roberto de Souza 	power = find_section(bdb, BDB_LFP_POWER);
738551fb93dSJosé Roberto de Souza 	if (!power)
739551fb93dSJosé Roberto de Souza 		return;
740551fb93dSJosé Roberto de Souza 
741dbd440d8SJani Nikula 	i915->vbt.psr.enable = power->psr & BIT(panel_type);
742551fb93dSJosé Roberto de Souza 
743551fb93dSJosé Roberto de Souza 	/*
744551fb93dSJosé Roberto de Souza 	 * If DRRS is not supported, drrs_type has to be set to 0.
745551fb93dSJosé Roberto de Souza 	 * This is because, VBT is configured in such a way that
746551fb93dSJosé Roberto de Souza 	 * static DRRS is 0 and DRRS not supported is represented by
747551fb93dSJosé Roberto de Souza 	 * power->drrs & BIT(panel_type)=false
748551fb93dSJosé Roberto de Souza 	 */
749551fb93dSJosé Roberto de Souza 	if (!(power->drrs & BIT(panel_type)))
750dbd440d8SJani Nikula 		i915->vbt.drrs_type = DRRS_NOT_SUPPORTED;
751f615cb6aSJosé Roberto de Souza 
752f615cb6aSJosé Roberto de Souza 	if (bdb->version >= 232)
753dbd440d8SJani Nikula 		i915->vbt.edp.hobl = power->hobl & BIT(panel_type);
754551fb93dSJosé Roberto de Souza }
755df0566a6SJani Nikula 
756df0566a6SJani Nikula static void
757dbd440d8SJani Nikula parse_edp(struct drm_i915_private *i915, const struct bdb_header *bdb)
758df0566a6SJani Nikula {
759df0566a6SJani Nikula 	const struct bdb_edp *edp;
760df0566a6SJani Nikula 	const struct edp_power_seq *edp_pps;
761df0566a6SJani Nikula 	const struct edp_fast_link_params *edp_link_params;
762dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
763df0566a6SJani Nikula 
764df0566a6SJani Nikula 	edp = find_section(bdb, BDB_EDP);
765df0566a6SJani Nikula 	if (!edp)
766df0566a6SJani Nikula 		return;
767df0566a6SJani Nikula 
768df0566a6SJani Nikula 	switch ((edp->color_depth >> (panel_type * 2)) & 3) {
769df0566a6SJani Nikula 	case EDP_18BPP:
770dbd440d8SJani Nikula 		i915->vbt.edp.bpp = 18;
771df0566a6SJani Nikula 		break;
772df0566a6SJani Nikula 	case EDP_24BPP:
773dbd440d8SJani Nikula 		i915->vbt.edp.bpp = 24;
774df0566a6SJani Nikula 		break;
775df0566a6SJani Nikula 	case EDP_30BPP:
776dbd440d8SJani Nikula 		i915->vbt.edp.bpp = 30;
777df0566a6SJani Nikula 		break;
778df0566a6SJani Nikula 	}
779df0566a6SJani Nikula 
780df0566a6SJani Nikula 	/* Get the eDP sequencing and link info */
781df0566a6SJani Nikula 	edp_pps = &edp->power_seqs[panel_type];
782df0566a6SJani Nikula 	edp_link_params = &edp->fast_link_params[panel_type];
783df0566a6SJani Nikula 
784dbd440d8SJani Nikula 	i915->vbt.edp.pps = *edp_pps;
785df0566a6SJani Nikula 
786df0566a6SJani Nikula 	switch (edp_link_params->rate) {
787df0566a6SJani Nikula 	case EDP_RATE_1_62:
788dbd440d8SJani Nikula 		i915->vbt.edp.rate = DP_LINK_BW_1_62;
789df0566a6SJani Nikula 		break;
790df0566a6SJani Nikula 	case EDP_RATE_2_7:
791dbd440d8SJani Nikula 		i915->vbt.edp.rate = DP_LINK_BW_2_7;
792df0566a6SJani Nikula 		break;
793df0566a6SJani Nikula 	default:
794dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
795e92cbf38SWambui Karuga 			    "VBT has unknown eDP link rate value %u\n",
796df0566a6SJani Nikula 			     edp_link_params->rate);
797df0566a6SJani Nikula 		break;
798df0566a6SJani Nikula 	}
799df0566a6SJani Nikula 
800df0566a6SJani Nikula 	switch (edp_link_params->lanes) {
801df0566a6SJani Nikula 	case EDP_LANE_1:
802dbd440d8SJani Nikula 		i915->vbt.edp.lanes = 1;
803df0566a6SJani Nikula 		break;
804df0566a6SJani Nikula 	case EDP_LANE_2:
805dbd440d8SJani Nikula 		i915->vbt.edp.lanes = 2;
806df0566a6SJani Nikula 		break;
807df0566a6SJani Nikula 	case EDP_LANE_4:
808dbd440d8SJani Nikula 		i915->vbt.edp.lanes = 4;
809df0566a6SJani Nikula 		break;
810df0566a6SJani Nikula 	default:
811dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
812e92cbf38SWambui Karuga 			    "VBT has unknown eDP lane count value %u\n",
813df0566a6SJani Nikula 			    edp_link_params->lanes);
814df0566a6SJani Nikula 		break;
815df0566a6SJani Nikula 	}
816df0566a6SJani Nikula 
817df0566a6SJani Nikula 	switch (edp_link_params->preemphasis) {
818df0566a6SJani Nikula 	case EDP_PREEMPHASIS_NONE:
819dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
820df0566a6SJani Nikula 		break;
821df0566a6SJani Nikula 	case EDP_PREEMPHASIS_3_5dB:
822dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
823df0566a6SJani Nikula 		break;
824df0566a6SJani Nikula 	case EDP_PREEMPHASIS_6dB:
825dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
826df0566a6SJani Nikula 		break;
827df0566a6SJani Nikula 	case EDP_PREEMPHASIS_9_5dB:
828dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
829df0566a6SJani Nikula 		break;
830df0566a6SJani Nikula 	default:
831dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
832e92cbf38SWambui Karuga 			    "VBT has unknown eDP pre-emphasis value %u\n",
833df0566a6SJani Nikula 			    edp_link_params->preemphasis);
834df0566a6SJani Nikula 		break;
835df0566a6SJani Nikula 	}
836df0566a6SJani Nikula 
837df0566a6SJani Nikula 	switch (edp_link_params->vswing) {
838df0566a6SJani Nikula 	case EDP_VSWING_0_4V:
839dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
840df0566a6SJani Nikula 		break;
841df0566a6SJani Nikula 	case EDP_VSWING_0_6V:
842dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
843df0566a6SJani Nikula 		break;
844df0566a6SJani Nikula 	case EDP_VSWING_0_8V:
845dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
846df0566a6SJani Nikula 		break;
847df0566a6SJani Nikula 	case EDP_VSWING_1_2V:
848dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
849df0566a6SJani Nikula 		break;
850df0566a6SJani Nikula 	default:
851dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
852e92cbf38SWambui Karuga 			    "VBT has unknown eDP voltage swing value %u\n",
853df0566a6SJani Nikula 			    edp_link_params->vswing);
854df0566a6SJani Nikula 		break;
855df0566a6SJani Nikula 	}
856df0566a6SJani Nikula 
857df0566a6SJani Nikula 	if (bdb->version >= 173) {
858df0566a6SJani Nikula 		u8 vswing;
859df0566a6SJani Nikula 
860df0566a6SJani Nikula 		/* Don't read from VBT if module parameter has valid value*/
861dbd440d8SJani Nikula 		if (i915->params.edp_vswing) {
862dbd440d8SJani Nikula 			i915->vbt.edp.low_vswing =
863dbd440d8SJani Nikula 				i915->params.edp_vswing == 1;
864df0566a6SJani Nikula 		} else {
865df0566a6SJani Nikula 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
866dbd440d8SJani Nikula 			i915->vbt.edp.low_vswing = vswing == 0;
867df0566a6SJani Nikula 		}
868df0566a6SJani Nikula 	}
869df0566a6SJani Nikula }
870df0566a6SJani Nikula 
871df0566a6SJani Nikula static void
872dbd440d8SJani Nikula parse_psr(struct drm_i915_private *i915, const struct bdb_header *bdb)
873df0566a6SJani Nikula {
874df0566a6SJani Nikula 	const struct bdb_psr *psr;
875df0566a6SJani Nikula 	const struct psr_table *psr_table;
876dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
877df0566a6SJani Nikula 
878df0566a6SJani Nikula 	psr = find_section(bdb, BDB_PSR);
879df0566a6SJani Nikula 	if (!psr) {
880dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No PSR BDB found.\n");
881df0566a6SJani Nikula 		return;
882df0566a6SJani Nikula 	}
883df0566a6SJani Nikula 
884df0566a6SJani Nikula 	psr_table = &psr->psr_table[panel_type];
885df0566a6SJani Nikula 
886dbd440d8SJani Nikula 	i915->vbt.psr.full_link = psr_table->full_link;
887dbd440d8SJani Nikula 	i915->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
888df0566a6SJani Nikula 
889df0566a6SJani Nikula 	/* Allowed VBT values goes from 0 to 15 */
890dbd440d8SJani Nikula 	i915->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
891df0566a6SJani Nikula 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
892df0566a6SJani Nikula 
893df0566a6SJani Nikula 	switch (psr_table->lines_to_wait) {
894df0566a6SJani Nikula 	case 0:
895dbd440d8SJani Nikula 		i915->vbt.psr.lines_to_wait = PSR_0_LINES_TO_WAIT;
896df0566a6SJani Nikula 		break;
897df0566a6SJani Nikula 	case 1:
898dbd440d8SJani Nikula 		i915->vbt.psr.lines_to_wait = PSR_1_LINE_TO_WAIT;
899df0566a6SJani Nikula 		break;
900df0566a6SJani Nikula 	case 2:
901dbd440d8SJani Nikula 		i915->vbt.psr.lines_to_wait = PSR_4_LINES_TO_WAIT;
902df0566a6SJani Nikula 		break;
903df0566a6SJani Nikula 	case 3:
904dbd440d8SJani Nikula 		i915->vbt.psr.lines_to_wait = PSR_8_LINES_TO_WAIT;
905df0566a6SJani Nikula 		break;
906df0566a6SJani Nikula 	default:
907dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
908e92cbf38SWambui Karuga 			    "VBT has unknown PSR lines to wait %u\n",
909df0566a6SJani Nikula 			    psr_table->lines_to_wait);
910df0566a6SJani Nikula 		break;
911df0566a6SJani Nikula 	}
912df0566a6SJani Nikula 
913df0566a6SJani Nikula 	/*
914df0566a6SJani Nikula 	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
915df0566a6SJani Nikula 	 * Old decimal value is wake up time in multiples of 100 us.
916df0566a6SJani Nikula 	 */
917df0566a6SJani Nikula 	if (bdb->version >= 205 &&
918dbd440d8SJani Nikula 	    (IS_GEN9_BC(i915) || IS_GEMINILAKE(i915) ||
919dbd440d8SJani Nikula 	     INTEL_GEN(i915) >= 10)) {
920df0566a6SJani Nikula 		switch (psr_table->tp1_wakeup_time) {
921df0566a6SJani Nikula 		case 0:
922dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 500;
923df0566a6SJani Nikula 			break;
924df0566a6SJani Nikula 		case 1:
925dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 100;
926df0566a6SJani Nikula 			break;
927df0566a6SJani Nikula 		case 3:
928dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 0;
929df0566a6SJani Nikula 			break;
930df0566a6SJani Nikula 		default:
931dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
932e92cbf38SWambui Karuga 				    "VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
933df0566a6SJani Nikula 				    psr_table->tp1_wakeup_time);
934df561f66SGustavo A. R. Silva 			fallthrough;
935df0566a6SJani Nikula 		case 2:
936dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 2500;
937df0566a6SJani Nikula 			break;
938df0566a6SJani Nikula 		}
939df0566a6SJani Nikula 
940df0566a6SJani Nikula 		switch (psr_table->tp2_tp3_wakeup_time) {
941df0566a6SJani Nikula 		case 0:
942dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 500;
943df0566a6SJani Nikula 			break;
944df0566a6SJani Nikula 		case 1:
945dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 100;
946df0566a6SJani Nikula 			break;
947df0566a6SJani Nikula 		case 3:
948dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 0;
949df0566a6SJani Nikula 			break;
950df0566a6SJani Nikula 		default:
951dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
952e92cbf38SWambui Karuga 				    "VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
953df0566a6SJani Nikula 				    psr_table->tp2_tp3_wakeup_time);
954df561f66SGustavo A. R. Silva 			fallthrough;
955df0566a6SJani Nikula 		case 2:
956dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
957df0566a6SJani Nikula 		break;
958df0566a6SJani Nikula 		}
959df0566a6SJani Nikula 	} else {
960dbd440d8SJani Nikula 		i915->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
961dbd440d8SJani Nikula 		i915->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
962df0566a6SJani Nikula 	}
963df0566a6SJani Nikula 
964df0566a6SJani Nikula 	if (bdb->version >= 226) {
965b5ea9c93SDhinakaran Pandiyan 		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
966df0566a6SJani Nikula 
967df0566a6SJani Nikula 		wakeup_time = (wakeup_time >> (2 * panel_type)) & 0x3;
968df0566a6SJani Nikula 		switch (wakeup_time) {
969df0566a6SJani Nikula 		case 0:
970df0566a6SJani Nikula 			wakeup_time = 500;
971df0566a6SJani Nikula 			break;
972df0566a6SJani Nikula 		case 1:
973df0566a6SJani Nikula 			wakeup_time = 100;
974df0566a6SJani Nikula 			break;
975df0566a6SJani Nikula 		case 3:
976df0566a6SJani Nikula 			wakeup_time = 50;
977df0566a6SJani Nikula 			break;
978df0566a6SJani Nikula 		default:
979df0566a6SJani Nikula 		case 2:
980df0566a6SJani Nikula 			wakeup_time = 2500;
981df0566a6SJani Nikula 			break;
982df0566a6SJani Nikula 		}
983dbd440d8SJani Nikula 		i915->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
984df0566a6SJani Nikula 	} else {
985df0566a6SJani Nikula 		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
986dbd440d8SJani Nikula 		i915->vbt.psr.psr2_tp2_tp3_wakeup_time_us = i915->vbt.psr.tp2_tp3_wakeup_time_us;
987df0566a6SJani Nikula 	}
988df0566a6SJani Nikula }
989df0566a6SJani Nikula 
990dbd440d8SJani Nikula static void parse_dsi_backlight_ports(struct drm_i915_private *i915,
991df0566a6SJani Nikula 				      u16 version, enum port port)
992df0566a6SJani Nikula {
993dbd440d8SJani Nikula 	if (!i915->vbt.dsi.config->dual_link || version < 197) {
994dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(port);
995dbd440d8SJani Nikula 		if (i915->vbt.dsi.config->cabc_supported)
996dbd440d8SJani Nikula 			i915->vbt.dsi.cabc_ports = BIT(port);
997df0566a6SJani Nikula 
998df0566a6SJani Nikula 		return;
999df0566a6SJani Nikula 	}
1000df0566a6SJani Nikula 
1001dbd440d8SJani Nikula 	switch (i915->vbt.dsi.config->dl_dcs_backlight_ports) {
1002df0566a6SJani Nikula 	case DL_DCS_PORT_A:
1003dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(PORT_A);
1004df0566a6SJani Nikula 		break;
1005df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1006dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(PORT_C);
1007df0566a6SJani Nikula 		break;
1008df0566a6SJani Nikula 	default:
1009df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
1010dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(PORT_C);
1011df0566a6SJani Nikula 		break;
1012df0566a6SJani Nikula 	}
1013df0566a6SJani Nikula 
1014dbd440d8SJani Nikula 	if (!i915->vbt.dsi.config->cabc_supported)
1015df0566a6SJani Nikula 		return;
1016df0566a6SJani Nikula 
1017dbd440d8SJani Nikula 	switch (i915->vbt.dsi.config->dl_dcs_cabc_ports) {
1018df0566a6SJani Nikula 	case DL_DCS_PORT_A:
1019dbd440d8SJani Nikula 		i915->vbt.dsi.cabc_ports = BIT(PORT_A);
1020df0566a6SJani Nikula 		break;
1021df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1022dbd440d8SJani Nikula 		i915->vbt.dsi.cabc_ports = BIT(PORT_C);
1023df0566a6SJani Nikula 		break;
1024df0566a6SJani Nikula 	default:
1025df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
1026dbd440d8SJani Nikula 		i915->vbt.dsi.cabc_ports =
1027df0566a6SJani Nikula 					BIT(PORT_A) | BIT(PORT_C);
1028df0566a6SJani Nikula 		break;
1029df0566a6SJani Nikula 	}
1030df0566a6SJani Nikula }
1031df0566a6SJani Nikula 
1032df0566a6SJani Nikula static void
1033dbd440d8SJani Nikula parse_mipi_config(struct drm_i915_private *i915,
1034df0566a6SJani Nikula 		  const struct bdb_header *bdb)
1035df0566a6SJani Nikula {
1036df0566a6SJani Nikula 	const struct bdb_mipi_config *start;
1037df0566a6SJani Nikula 	const struct mipi_config *config;
1038df0566a6SJani Nikula 	const struct mipi_pps_data *pps;
1039dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
1040df0566a6SJani Nikula 	enum port port;
1041df0566a6SJani Nikula 
1042df0566a6SJani Nikula 	/* parse MIPI blocks only if LFP type is MIPI */
1043dbd440d8SJani Nikula 	if (!intel_bios_is_dsi_present(i915, &port))
1044df0566a6SJani Nikula 		return;
1045df0566a6SJani Nikula 
1046df0566a6SJani Nikula 	/* Initialize this to undefined indicating no generic MIPI support */
1047dbd440d8SJani Nikula 	i915->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
1048df0566a6SJani Nikula 
1049df0566a6SJani Nikula 	/* Block #40 is already parsed and panel_fixed_mode is
1050dbd440d8SJani Nikula 	 * stored in i915->lfp_lvds_vbt_mode
1051df0566a6SJani Nikula 	 * resuse this when needed
1052df0566a6SJani Nikula 	 */
1053df0566a6SJani Nikula 
1054df0566a6SJani Nikula 	/* Parse #52 for panel index used from panel_type already
1055df0566a6SJani Nikula 	 * parsed
1056df0566a6SJani Nikula 	 */
1057df0566a6SJani Nikula 	start = find_section(bdb, BDB_MIPI_CONFIG);
1058df0566a6SJani Nikula 	if (!start) {
1059dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No MIPI config BDB found");
1060df0566a6SJani Nikula 		return;
1061df0566a6SJani Nikula 	}
1062df0566a6SJani Nikula 
1063dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI Config block, panel index = %d\n",
1064df0566a6SJani Nikula 		panel_type);
1065df0566a6SJani Nikula 
1066df0566a6SJani Nikula 	/*
1067df0566a6SJani Nikula 	 * get hold of the correct configuration block and pps data as per
1068df0566a6SJani Nikula 	 * the panel_type as index
1069df0566a6SJani Nikula 	 */
1070df0566a6SJani Nikula 	config = &start->config[panel_type];
1071df0566a6SJani Nikula 	pps = &start->pps[panel_type];
1072df0566a6SJani Nikula 
1073df0566a6SJani Nikula 	/* store as of now full data. Trim when we realise all is not needed */
1074dbd440d8SJani Nikula 	i915->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
1075dbd440d8SJani Nikula 	if (!i915->vbt.dsi.config)
1076df0566a6SJani Nikula 		return;
1077df0566a6SJani Nikula 
1078dbd440d8SJani Nikula 	i915->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
1079dbd440d8SJani Nikula 	if (!i915->vbt.dsi.pps) {
1080dbd440d8SJani Nikula 		kfree(i915->vbt.dsi.config);
1081df0566a6SJani Nikula 		return;
1082df0566a6SJani Nikula 	}
1083df0566a6SJani Nikula 
1084dbd440d8SJani Nikula 	parse_dsi_backlight_ports(i915, bdb->version, port);
1085df0566a6SJani Nikula 
1086df0566a6SJani Nikula 	/* FIXME is the 90 vs. 270 correct? */
1087df0566a6SJani Nikula 	switch (config->rotation) {
1088df0566a6SJani Nikula 	case ENABLE_ROTATION_0:
1089df0566a6SJani Nikula 		/*
1090df0566a6SJani Nikula 		 * Most (all?) VBTs claim 0 degrees despite having
1091df0566a6SJani Nikula 		 * an upside down panel, thus we do not trust this.
1092df0566a6SJani Nikula 		 */
1093dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1094df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1095df0566a6SJani Nikula 		break;
1096df0566a6SJani Nikula 	case ENABLE_ROTATION_90:
1097dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1098df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1099df0566a6SJani Nikula 		break;
1100df0566a6SJani Nikula 	case ENABLE_ROTATION_180:
1101dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1102df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1103df0566a6SJani Nikula 		break;
1104df0566a6SJani Nikula 	case ENABLE_ROTATION_270:
1105dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1106df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1107df0566a6SJani Nikula 		break;
1108df0566a6SJani Nikula 	}
1109df0566a6SJani Nikula 
1110df0566a6SJani Nikula 	/* We have mandatory mipi config blocks. Initialize as generic panel */
1111dbd440d8SJani Nikula 	i915->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
1112df0566a6SJani Nikula }
1113df0566a6SJani Nikula 
1114df0566a6SJani Nikula /* Find the sequence block and size for the given panel. */
1115df0566a6SJani Nikula static const u8 *
1116df0566a6SJani Nikula find_panel_sequence_block(const struct bdb_mipi_sequence *sequence,
1117df0566a6SJani Nikula 			  u16 panel_id, u32 *seq_size)
1118df0566a6SJani Nikula {
1119df0566a6SJani Nikula 	u32 total = get_blocksize(sequence);
1120df0566a6SJani Nikula 	const u8 *data = &sequence->data[0];
1121df0566a6SJani Nikula 	u8 current_id;
1122df0566a6SJani Nikula 	u32 current_size;
1123df0566a6SJani Nikula 	int header_size = sequence->version >= 3 ? 5 : 3;
1124df0566a6SJani Nikula 	int index = 0;
1125df0566a6SJani Nikula 	int i;
1126df0566a6SJani Nikula 
1127df0566a6SJani Nikula 	/* skip new block size */
1128df0566a6SJani Nikula 	if (sequence->version >= 3)
1129df0566a6SJani Nikula 		data += 4;
1130df0566a6SJani Nikula 
1131df0566a6SJani Nikula 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
1132df0566a6SJani Nikula 		if (index + header_size > total) {
1133df0566a6SJani Nikula 			DRM_ERROR("Invalid sequence block (header)\n");
1134df0566a6SJani Nikula 			return NULL;
1135df0566a6SJani Nikula 		}
1136df0566a6SJani Nikula 
1137df0566a6SJani Nikula 		current_id = *(data + index);
1138df0566a6SJani Nikula 		if (sequence->version >= 3)
1139df0566a6SJani Nikula 			current_size = *((const u32 *)(data + index + 1));
1140df0566a6SJani Nikula 		else
1141df0566a6SJani Nikula 			current_size = *((const u16 *)(data + index + 1));
1142df0566a6SJani Nikula 
1143df0566a6SJani Nikula 		index += header_size;
1144df0566a6SJani Nikula 
1145df0566a6SJani Nikula 		if (index + current_size > total) {
1146df0566a6SJani Nikula 			DRM_ERROR("Invalid sequence block\n");
1147df0566a6SJani Nikula 			return NULL;
1148df0566a6SJani Nikula 		}
1149df0566a6SJani Nikula 
1150df0566a6SJani Nikula 		if (current_id == panel_id) {
1151df0566a6SJani Nikula 			*seq_size = current_size;
1152df0566a6SJani Nikula 			return data + index;
1153df0566a6SJani Nikula 		}
1154df0566a6SJani Nikula 
1155df0566a6SJani Nikula 		index += current_size;
1156df0566a6SJani Nikula 	}
1157df0566a6SJani Nikula 
1158df0566a6SJani Nikula 	DRM_ERROR("Sequence block detected but no valid configuration\n");
1159df0566a6SJani Nikula 
1160df0566a6SJani Nikula 	return NULL;
1161df0566a6SJani Nikula }
1162df0566a6SJani Nikula 
1163df0566a6SJani Nikula static int goto_next_sequence(const u8 *data, int index, int total)
1164df0566a6SJani Nikula {
1165df0566a6SJani Nikula 	u16 len;
1166df0566a6SJani Nikula 
1167df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1168df0566a6SJani Nikula 	for (index = index + 1; index < total; index += len) {
1169df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1170df0566a6SJani Nikula 		index++;
1171df0566a6SJani Nikula 
1172df0566a6SJani Nikula 		switch (operation_byte) {
1173df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_END:
1174df0566a6SJani Nikula 			return index;
1175df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1176df0566a6SJani Nikula 			if (index + 4 > total)
1177df0566a6SJani Nikula 				return 0;
1178df0566a6SJani Nikula 
1179df0566a6SJani Nikula 			len = *((const u16 *)(data + index + 2)) + 4;
1180df0566a6SJani Nikula 			break;
1181df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1182df0566a6SJani Nikula 			len = 4;
1183df0566a6SJani Nikula 			break;
1184df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1185df0566a6SJani Nikula 			len = 2;
1186df0566a6SJani Nikula 			break;
1187df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1188df0566a6SJani Nikula 			if (index + 7 > total)
1189df0566a6SJani Nikula 				return 0;
1190df0566a6SJani Nikula 			len = *(data + index + 6) + 7;
1191df0566a6SJani Nikula 			break;
1192df0566a6SJani Nikula 		default:
1193df0566a6SJani Nikula 			DRM_ERROR("Unknown operation byte\n");
1194df0566a6SJani Nikula 			return 0;
1195df0566a6SJani Nikula 		}
1196df0566a6SJani Nikula 	}
1197df0566a6SJani Nikula 
1198df0566a6SJani Nikula 	return 0;
1199df0566a6SJani Nikula }
1200df0566a6SJani Nikula 
1201df0566a6SJani Nikula static int goto_next_sequence_v3(const u8 *data, int index, int total)
1202df0566a6SJani Nikula {
1203df0566a6SJani Nikula 	int seq_end;
1204df0566a6SJani Nikula 	u16 len;
1205df0566a6SJani Nikula 	u32 size_of_sequence;
1206df0566a6SJani Nikula 
1207df0566a6SJani Nikula 	/*
1208df0566a6SJani Nikula 	 * Could skip sequence based on Size of Sequence alone, but also do some
1209df0566a6SJani Nikula 	 * checking on the structure.
1210df0566a6SJani Nikula 	 */
1211df0566a6SJani Nikula 	if (total < 5) {
1212df0566a6SJani Nikula 		DRM_ERROR("Too small sequence size\n");
1213df0566a6SJani Nikula 		return 0;
1214df0566a6SJani Nikula 	}
1215df0566a6SJani Nikula 
1216df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1217df0566a6SJani Nikula 	index++;
1218df0566a6SJani Nikula 
1219df0566a6SJani Nikula 	/*
1220df0566a6SJani Nikula 	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
1221df0566a6SJani Nikula 	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
1222df0566a6SJani Nikula 	 * byte.
1223df0566a6SJani Nikula 	 */
1224df0566a6SJani Nikula 	size_of_sequence = *((const u32 *)(data + index));
1225df0566a6SJani Nikula 	index += 4;
1226df0566a6SJani Nikula 
1227df0566a6SJani Nikula 	seq_end = index + size_of_sequence;
1228df0566a6SJani Nikula 	if (seq_end > total) {
1229df0566a6SJani Nikula 		DRM_ERROR("Invalid sequence size\n");
1230df0566a6SJani Nikula 		return 0;
1231df0566a6SJani Nikula 	}
1232df0566a6SJani Nikula 
1233df0566a6SJani Nikula 	for (; index < total; index += len) {
1234df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1235df0566a6SJani Nikula 		index++;
1236df0566a6SJani Nikula 
1237df0566a6SJani Nikula 		if (operation_byte == MIPI_SEQ_ELEM_END) {
1238df0566a6SJani Nikula 			if (index != seq_end) {
1239df0566a6SJani Nikula 				DRM_ERROR("Invalid element structure\n");
1240df0566a6SJani Nikula 				return 0;
1241df0566a6SJani Nikula 			}
1242df0566a6SJani Nikula 			return index;
1243df0566a6SJani Nikula 		}
1244df0566a6SJani Nikula 
1245df0566a6SJani Nikula 		len = *(data + index);
1246df0566a6SJani Nikula 		index++;
1247df0566a6SJani Nikula 
1248df0566a6SJani Nikula 		/*
1249df0566a6SJani Nikula 		 * FIXME: Would be nice to check elements like for v1/v2 in
1250df0566a6SJani Nikula 		 * goto_next_sequence() above.
1251df0566a6SJani Nikula 		 */
1252df0566a6SJani Nikula 		switch (operation_byte) {
1253df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1254df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1255df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1256df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1257df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SPI:
1258df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_PMIC:
1259df0566a6SJani Nikula 			break;
1260df0566a6SJani Nikula 		default:
1261df0566a6SJani Nikula 			DRM_ERROR("Unknown operation byte %u\n",
1262df0566a6SJani Nikula 				  operation_byte);
1263df0566a6SJani Nikula 			break;
1264df0566a6SJani Nikula 		}
1265df0566a6SJani Nikula 	}
1266df0566a6SJani Nikula 
1267df0566a6SJani Nikula 	return 0;
1268df0566a6SJani Nikula }
1269df0566a6SJani Nikula 
1270df0566a6SJani Nikula /*
1271df0566a6SJani Nikula  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
1272df0566a6SJani Nikula  * skip all delay + gpio operands and stop at the first DSI packet op.
1273df0566a6SJani Nikula  */
1274dbd440d8SJani Nikula static int get_init_otp_deassert_fragment_len(struct drm_i915_private *i915)
1275df0566a6SJani Nikula {
1276dbd440d8SJani Nikula 	const u8 *data = i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1277df0566a6SJani Nikula 	int index, len;
1278df0566a6SJani Nikula 
1279dbd440d8SJani Nikula 	if (drm_WARN_ON(&i915->drm,
1280dbd440d8SJani Nikula 			!data || i915->vbt.dsi.seq_version != 1))
1281df0566a6SJani Nikula 		return 0;
1282df0566a6SJani Nikula 
1283df0566a6SJani Nikula 	/* index = 1 to skip sequence byte */
1284df0566a6SJani Nikula 	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1285df0566a6SJani Nikula 		switch (data[index]) {
1286df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1287df0566a6SJani Nikula 			return index == 1 ? 0 : index;
1288df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1289df0566a6SJani Nikula 			len = 5; /* 1 byte for operand + uint32 */
1290df0566a6SJani Nikula 			break;
1291df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1292df0566a6SJani Nikula 			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1293df0566a6SJani Nikula 			break;
1294df0566a6SJani Nikula 		default:
1295df0566a6SJani Nikula 			return 0;
1296df0566a6SJani Nikula 		}
1297df0566a6SJani Nikula 	}
1298df0566a6SJani Nikula 
1299df0566a6SJani Nikula 	return 0;
1300df0566a6SJani Nikula }
1301df0566a6SJani Nikula 
1302df0566a6SJani Nikula /*
1303df0566a6SJani Nikula  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1304df0566a6SJani Nikula  * The deassert must be done before calling intel_dsi_device_ready, so for
1305df0566a6SJani Nikula  * these devices we split the init OTP sequence into a deassert sequence and
1306df0566a6SJani Nikula  * the actual init OTP part.
1307df0566a6SJani Nikula  */
1308dbd440d8SJani Nikula static void fixup_mipi_sequences(struct drm_i915_private *i915)
1309df0566a6SJani Nikula {
1310df0566a6SJani Nikula 	u8 *init_otp;
1311df0566a6SJani Nikula 	int len;
1312df0566a6SJani Nikula 
1313df0566a6SJani Nikula 	/* Limit this to VLV for now. */
1314dbd440d8SJani Nikula 	if (!IS_VALLEYVIEW(i915))
1315df0566a6SJani Nikula 		return;
1316df0566a6SJani Nikula 
1317df0566a6SJani Nikula 	/* Limit this to v1 vid-mode sequences */
1318dbd440d8SJani Nikula 	if (i915->vbt.dsi.config->is_cmd_mode ||
1319dbd440d8SJani Nikula 	    i915->vbt.dsi.seq_version != 1)
1320df0566a6SJani Nikula 		return;
1321df0566a6SJani Nikula 
1322df0566a6SJani Nikula 	/* Only do this if there are otp and assert seqs and no deassert seq */
1323dbd440d8SJani Nikula 	if (!i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
1324dbd440d8SJani Nikula 	    !i915->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
1325dbd440d8SJani Nikula 	    i915->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1326df0566a6SJani Nikula 		return;
1327df0566a6SJani Nikula 
1328df0566a6SJani Nikula 	/* The deassert-sequence ends at the first DSI packet */
1329dbd440d8SJani Nikula 	len = get_init_otp_deassert_fragment_len(i915);
1330df0566a6SJani Nikula 	if (!len)
1331df0566a6SJani Nikula 		return;
1332df0566a6SJani Nikula 
1333dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1334e92cbf38SWambui Karuga 		    "Using init OTP fragment to deassert reset\n");
1335df0566a6SJani Nikula 
1336df0566a6SJani Nikula 	/* Copy the fragment, update seq byte and terminate it */
1337dbd440d8SJani Nikula 	init_otp = (u8 *)i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1338dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
1339dbd440d8SJani Nikula 	if (!i915->vbt.dsi.deassert_seq)
1340df0566a6SJani Nikula 		return;
1341dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
1342dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1343df0566a6SJani Nikula 	/* Use the copy for deassert */
1344dbd440d8SJani Nikula 	i915->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
1345dbd440d8SJani Nikula 		i915->vbt.dsi.deassert_seq;
1346df0566a6SJani Nikula 	/* Replace the last byte of the fragment with init OTP seq byte */
1347df0566a6SJani Nikula 	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1348df0566a6SJani Nikula 	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
1349dbd440d8SJani Nikula 	i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
1350df0566a6SJani Nikula }
1351df0566a6SJani Nikula 
1352df0566a6SJani Nikula static void
1353dbd440d8SJani Nikula parse_mipi_sequence(struct drm_i915_private *i915,
1354df0566a6SJani Nikula 		    const struct bdb_header *bdb)
1355df0566a6SJani Nikula {
1356dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
1357df0566a6SJani Nikula 	const struct bdb_mipi_sequence *sequence;
1358df0566a6SJani Nikula 	const u8 *seq_data;
1359df0566a6SJani Nikula 	u32 seq_size;
1360df0566a6SJani Nikula 	u8 *data;
1361df0566a6SJani Nikula 	int index = 0;
1362df0566a6SJani Nikula 
1363df0566a6SJani Nikula 	/* Only our generic panel driver uses the sequence block. */
1364dbd440d8SJani Nikula 	if (i915->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
1365df0566a6SJani Nikula 		return;
1366df0566a6SJani Nikula 
1367df0566a6SJani Nikula 	sequence = find_section(bdb, BDB_MIPI_SEQUENCE);
1368df0566a6SJani Nikula 	if (!sequence) {
1369dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1370e92cbf38SWambui Karuga 			    "No MIPI Sequence found, parsing complete\n");
1371df0566a6SJani Nikula 		return;
1372df0566a6SJani Nikula 	}
1373df0566a6SJani Nikula 
1374df0566a6SJani Nikula 	/* Fail gracefully for forward incompatible sequence block. */
1375df0566a6SJani Nikula 	if (sequence->version >= 4) {
1376dbd440d8SJani Nikula 		drm_err(&i915->drm,
1377e92cbf38SWambui Karuga 			"Unable to parse MIPI Sequence Block v%u\n",
1378df0566a6SJani Nikula 			sequence->version);
1379df0566a6SJani Nikula 		return;
1380df0566a6SJani Nikula 	}
1381df0566a6SJani Nikula 
1382dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI sequence block v%u\n",
1383e92cbf38SWambui Karuga 		sequence->version);
1384df0566a6SJani Nikula 
1385df0566a6SJani Nikula 	seq_data = find_panel_sequence_block(sequence, panel_type, &seq_size);
1386df0566a6SJani Nikula 	if (!seq_data)
1387df0566a6SJani Nikula 		return;
1388df0566a6SJani Nikula 
1389df0566a6SJani Nikula 	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
1390df0566a6SJani Nikula 	if (!data)
1391df0566a6SJani Nikula 		return;
1392df0566a6SJani Nikula 
1393df0566a6SJani Nikula 	/* Parse the sequences, store pointers to each sequence. */
1394df0566a6SJani Nikula 	for (;;) {
1395df0566a6SJani Nikula 		u8 seq_id = *(data + index);
1396df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_END)
1397df0566a6SJani Nikula 			break;
1398df0566a6SJani Nikula 
1399df0566a6SJani Nikula 		if (seq_id >= MIPI_SEQ_MAX) {
1400dbd440d8SJani Nikula 			drm_err(&i915->drm, "Unknown sequence %u\n",
1401e92cbf38SWambui Karuga 				seq_id);
1402df0566a6SJani Nikula 			goto err;
1403df0566a6SJani Nikula 		}
1404df0566a6SJani Nikula 
1405df0566a6SJani Nikula 		/* Log about presence of sequences we won't run. */
1406df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
1407dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1408e92cbf38SWambui Karuga 				    "Unsupported sequence %u\n", seq_id);
1409df0566a6SJani Nikula 
1410dbd440d8SJani Nikula 		i915->vbt.dsi.sequence[seq_id] = data + index;
1411df0566a6SJani Nikula 
1412df0566a6SJani Nikula 		if (sequence->version >= 3)
1413df0566a6SJani Nikula 			index = goto_next_sequence_v3(data, index, seq_size);
1414df0566a6SJani Nikula 		else
1415df0566a6SJani Nikula 			index = goto_next_sequence(data, index, seq_size);
1416df0566a6SJani Nikula 		if (!index) {
1417dbd440d8SJani Nikula 			drm_err(&i915->drm, "Invalid sequence %u\n",
1418e92cbf38SWambui Karuga 				seq_id);
1419df0566a6SJani Nikula 			goto err;
1420df0566a6SJani Nikula 		}
1421df0566a6SJani Nikula 	}
1422df0566a6SJani Nikula 
1423dbd440d8SJani Nikula 	i915->vbt.dsi.data = data;
1424dbd440d8SJani Nikula 	i915->vbt.dsi.size = seq_size;
1425dbd440d8SJani Nikula 	i915->vbt.dsi.seq_version = sequence->version;
1426df0566a6SJani Nikula 
1427dbd440d8SJani Nikula 	fixup_mipi_sequences(i915);
1428df0566a6SJani Nikula 
1429dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "MIPI related VBT parsing complete\n");
1430df0566a6SJani Nikula 	return;
1431df0566a6SJani Nikula 
1432df0566a6SJani Nikula err:
1433df0566a6SJani Nikula 	kfree(data);
1434dbd440d8SJani Nikula 	memset(i915->vbt.dsi.sequence, 0, sizeof(i915->vbt.dsi.sequence));
1435df0566a6SJani Nikula }
1436df0566a6SJani Nikula 
14376e0d46e9SJani Nikula static void
14386e0d46e9SJani Nikula parse_compression_parameters(struct drm_i915_private *i915,
14396e0d46e9SJani Nikula 			     const struct bdb_header *bdb)
14406e0d46e9SJani Nikula {
14416e0d46e9SJani Nikula 	const struct bdb_compression_parameters *params;
14426e0d46e9SJani Nikula 	struct display_device_data *devdata;
14436e0d46e9SJani Nikula 	const struct child_device_config *child;
14446e0d46e9SJani Nikula 	u16 block_size;
14456e0d46e9SJani Nikula 	int index;
14466e0d46e9SJani Nikula 
14476e0d46e9SJani Nikula 	if (bdb->version < 198)
14486e0d46e9SJani Nikula 		return;
14496e0d46e9SJani Nikula 
14506e0d46e9SJani Nikula 	params = find_section(bdb, BDB_COMPRESSION_PARAMETERS);
14516e0d46e9SJani Nikula 	if (params) {
14526e0d46e9SJani Nikula 		/* Sanity checks */
14536e0d46e9SJani Nikula 		if (params->entry_size != sizeof(params->data[0])) {
1454e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1455e92cbf38SWambui Karuga 				    "VBT: unsupported compression param entry size\n");
14566e0d46e9SJani Nikula 			return;
14576e0d46e9SJani Nikula 		}
14586e0d46e9SJani Nikula 
14596e0d46e9SJani Nikula 		block_size = get_blocksize(params);
14606e0d46e9SJani Nikula 		if (block_size < sizeof(*params)) {
1461e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1462e92cbf38SWambui Karuga 				    "VBT: expected 16 compression param entries\n");
14636e0d46e9SJani Nikula 			return;
14646e0d46e9SJani Nikula 		}
14656e0d46e9SJani Nikula 	}
14666e0d46e9SJani Nikula 
14676e0d46e9SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
14686e0d46e9SJani Nikula 		child = &devdata->child;
14696e0d46e9SJani Nikula 
14706e0d46e9SJani Nikula 		if (!child->compression_enable)
14716e0d46e9SJani Nikula 			continue;
14726e0d46e9SJani Nikula 
14736e0d46e9SJani Nikula 		if (!params) {
1474e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1475e92cbf38SWambui Karuga 				    "VBT: compression params not available\n");
14766e0d46e9SJani Nikula 			continue;
14776e0d46e9SJani Nikula 		}
14786e0d46e9SJani Nikula 
14796e0d46e9SJani Nikula 		if (child->compression_method_cps) {
1480e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1481e92cbf38SWambui Karuga 				    "VBT: CPS compression not supported\n");
14826e0d46e9SJani Nikula 			continue;
14836e0d46e9SJani Nikula 		}
14846e0d46e9SJani Nikula 
14856e0d46e9SJani Nikula 		index = child->compression_structure_index;
14866e0d46e9SJani Nikula 
14876e0d46e9SJani Nikula 		devdata->dsc = kmemdup(&params->data[index],
14886e0d46e9SJani Nikula 				       sizeof(*devdata->dsc), GFP_KERNEL);
14896e0d46e9SJani Nikula 	}
14906e0d46e9SJani Nikula }
14916e0d46e9SJani Nikula 
1492df0566a6SJani Nikula static u8 translate_iboost(u8 val)
1493df0566a6SJani Nikula {
1494df0566a6SJani Nikula 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
1495df0566a6SJani Nikula 
1496df0566a6SJani Nikula 	if (val >= ARRAY_SIZE(mapping)) {
1497df0566a6SJani Nikula 		DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
1498df0566a6SJani Nikula 		return 0;
1499df0566a6SJani Nikula 	}
1500df0566a6SJani Nikula 	return mapping[val];
1501df0566a6SJani Nikula }
1502df0566a6SJani Nikula 
1503df0566a6SJani Nikula static enum port get_port_by_ddc_pin(struct drm_i915_private *i915, u8 ddc_pin)
1504df0566a6SJani Nikula {
1505df0566a6SJani Nikula 	const struct ddi_vbt_port_info *info;
1506df0566a6SJani Nikula 	enum port port;
1507df0566a6SJani Nikula 
1508c4a774c4SJani Nikula 	for_each_port(port) {
1509df0566a6SJani Nikula 		info = &i915->vbt.ddi_port_info[port];
1510df0566a6SJani Nikula 
1511df0566a6SJani Nikula 		if (info->child && ddc_pin == info->alternate_ddc_pin)
1512df0566a6SJani Nikula 			return port;
1513df0566a6SJani Nikula 	}
1514df0566a6SJani Nikula 
1515df0566a6SJani Nikula 	return PORT_NONE;
1516df0566a6SJani Nikula }
1517df0566a6SJani Nikula 
1518dbd440d8SJani Nikula static void sanitize_ddc_pin(struct drm_i915_private *i915,
1519df0566a6SJani Nikula 			     enum port port)
1520df0566a6SJani Nikula {
1521dbd440d8SJani Nikula 	struct ddi_vbt_port_info *info = &i915->vbt.ddi_port_info[port];
1522df0566a6SJani Nikula 	enum port p;
1523df0566a6SJani Nikula 
1524df0566a6SJani Nikula 	if (!info->alternate_ddc_pin)
1525df0566a6SJani Nikula 		return;
1526df0566a6SJani Nikula 
1527dbd440d8SJani Nikula 	p = get_port_by_ddc_pin(i915, info->alternate_ddc_pin);
1528df0566a6SJani Nikula 	if (p != PORT_NONE) {
1529dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1530e92cbf38SWambui Karuga 			    "port %c trying to use the same DDC pin (0x%x) as port %c, "
1531df0566a6SJani Nikula 			    "disabling port %c DVI/HDMI support\n",
1532df0566a6SJani Nikula 			    port_name(port), info->alternate_ddc_pin,
153341e35ffbSVille Syrjälä 			    port_name(p), port_name(p));
1534df0566a6SJani Nikula 
1535df0566a6SJani Nikula 		/*
1536df0566a6SJani Nikula 		 * If we have multiple ports supposedly sharing the
1537df0566a6SJani Nikula 		 * pin, then dvi/hdmi couldn't exist on the shared
1538df0566a6SJani Nikula 		 * port. Otherwise they share the same ddc bin and
1539df0566a6SJani Nikula 		 * system couldn't communicate with them separately.
1540df0566a6SJani Nikula 		 *
154141e35ffbSVille Syrjälä 		 * Give inverse child device order the priority,
154241e35ffbSVille Syrjälä 		 * last one wins. Yes, there are real machines
154341e35ffbSVille Syrjälä 		 * (eg. Asrock B250M-HDV) where VBT has both
154441e35ffbSVille Syrjälä 		 * port A and port E with the same AUX ch and
154541e35ffbSVille Syrjälä 		 * we must pick port E :(
1546df0566a6SJani Nikula 		 */
1547dbd440d8SJani Nikula 		info = &i915->vbt.ddi_port_info[p];
154841e35ffbSVille Syrjälä 
1549df0566a6SJani Nikula 		info->supports_dvi = false;
1550df0566a6SJani Nikula 		info->supports_hdmi = false;
1551df0566a6SJani Nikula 		info->alternate_ddc_pin = 0;
1552df0566a6SJani Nikula 	}
1553df0566a6SJani Nikula }
1554df0566a6SJani Nikula 
1555df0566a6SJani Nikula static enum port get_port_by_aux_ch(struct drm_i915_private *i915, u8 aux_ch)
1556df0566a6SJani Nikula {
1557df0566a6SJani Nikula 	const struct ddi_vbt_port_info *info;
1558df0566a6SJani Nikula 	enum port port;
1559df0566a6SJani Nikula 
1560c4a774c4SJani Nikula 	for_each_port(port) {
1561df0566a6SJani Nikula 		info = &i915->vbt.ddi_port_info[port];
1562df0566a6SJani Nikula 
1563df0566a6SJani Nikula 		if (info->child && aux_ch == info->alternate_aux_channel)
1564df0566a6SJani Nikula 			return port;
1565df0566a6SJani Nikula 	}
1566df0566a6SJani Nikula 
1567df0566a6SJani Nikula 	return PORT_NONE;
1568df0566a6SJani Nikula }
1569df0566a6SJani Nikula 
1570dbd440d8SJani Nikula static void sanitize_aux_ch(struct drm_i915_private *i915,
1571df0566a6SJani Nikula 			    enum port port)
1572df0566a6SJani Nikula {
1573dbd440d8SJani Nikula 	struct ddi_vbt_port_info *info = &i915->vbt.ddi_port_info[port];
1574df0566a6SJani Nikula 	enum port p;
1575df0566a6SJani Nikula 
1576df0566a6SJani Nikula 	if (!info->alternate_aux_channel)
1577df0566a6SJani Nikula 		return;
1578df0566a6SJani Nikula 
1579dbd440d8SJani Nikula 	p = get_port_by_aux_ch(i915, info->alternate_aux_channel);
1580df0566a6SJani Nikula 	if (p != PORT_NONE) {
1581dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1582e92cbf38SWambui Karuga 			    "port %c trying to use the same AUX CH (0x%x) as port %c, "
1583df0566a6SJani Nikula 			    "disabling port %c DP support\n",
1584df0566a6SJani Nikula 			    port_name(port), info->alternate_aux_channel,
158541e35ffbSVille Syrjälä 			    port_name(p), port_name(p));
1586df0566a6SJani Nikula 
1587df0566a6SJani Nikula 		/*
1588df0566a6SJani Nikula 		 * If we have multiple ports supposedlt sharing the
1589df0566a6SJani Nikula 		 * aux channel, then DP couldn't exist on the shared
1590df0566a6SJani Nikula 		 * port. Otherwise they share the same aux channel
1591df0566a6SJani Nikula 		 * and system couldn't communicate with them separately.
1592df0566a6SJani Nikula 		 *
159341e35ffbSVille Syrjälä 		 * Give inverse child device order the priority,
159441e35ffbSVille Syrjälä 		 * last one wins. Yes, there are real machines
159541e35ffbSVille Syrjälä 		 * (eg. Asrock B250M-HDV) where VBT has both
159641e35ffbSVille Syrjälä 		 * port A and port E with the same AUX ch and
159741e35ffbSVille Syrjälä 		 * we must pick port E :(
1598df0566a6SJani Nikula 		 */
1599dbd440d8SJani Nikula 		info = &i915->vbt.ddi_port_info[p];
160041e35ffbSVille Syrjälä 
1601df0566a6SJani Nikula 		info->supports_dp = false;
1602df0566a6SJani Nikula 		info->alternate_aux_channel = 0;
1603df0566a6SJani Nikula 	}
1604df0566a6SJani Nikula }
1605df0566a6SJani Nikula 
1606df0566a6SJani Nikula static const u8 cnp_ddc_pin_map[] = {
1607df0566a6SJani Nikula 	[0] = 0, /* N/A */
1608df0566a6SJani Nikula 	[DDC_BUS_DDI_B] = GMBUS_PIN_1_BXT,
1609df0566a6SJani Nikula 	[DDC_BUS_DDI_C] = GMBUS_PIN_2_BXT,
1610df0566a6SJani Nikula 	[DDC_BUS_DDI_D] = GMBUS_PIN_4_CNP, /* sic */
1611df0566a6SJani Nikula 	[DDC_BUS_DDI_F] = GMBUS_PIN_3_BXT, /* sic */
1612df0566a6SJani Nikula };
1613df0566a6SJani Nikula 
1614df0566a6SJani Nikula static const u8 icp_ddc_pin_map[] = {
1615df0566a6SJani Nikula 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
1616df0566a6SJani Nikula 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
1617d757535eSMahesh Kumar 	[TGL_DDC_BUS_DDI_C] = GMBUS_PIN_3_BXT,
1618d757535eSMahesh Kumar 	[ICL_DDC_BUS_PORT_1] = GMBUS_PIN_9_TC1_ICP,
1619d757535eSMahesh Kumar 	[ICL_DDC_BUS_PORT_2] = GMBUS_PIN_10_TC2_ICP,
1620d757535eSMahesh Kumar 	[ICL_DDC_BUS_PORT_3] = GMBUS_PIN_11_TC3_ICP,
1621d757535eSMahesh Kumar 	[ICL_DDC_BUS_PORT_4] = GMBUS_PIN_12_TC4_ICP,
1622d757535eSMahesh Kumar 	[TGL_DDC_BUS_PORT_5] = GMBUS_PIN_13_TC5_TGP,
1623d757535eSMahesh Kumar 	[TGL_DDC_BUS_PORT_6] = GMBUS_PIN_14_TC6_TGP,
1624d757535eSMahesh Kumar };
1625d757535eSMahesh Kumar 
1626956aee8fSLee Shawn C static const u8 rkl_pch_tgp_ddc_pin_map[] = {
1627956aee8fSLee Shawn C 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
1628956aee8fSLee Shawn C 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
1629956aee8fSLee Shawn C 	[RKL_DDC_BUS_DDI_D] = GMBUS_PIN_9_TC1_ICP,
1630956aee8fSLee Shawn C 	[RKL_DDC_BUS_DDI_E] = GMBUS_PIN_10_TC2_ICP,
1631956aee8fSLee Shawn C };
1632956aee8fSLee Shawn C 
16337dc1f92fSAditya Swarup static const u8 adls_ddc_pin_map[] = {
16347dc1f92fSAditya Swarup 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
16357dc1f92fSAditya Swarup 	[ADLS_DDC_BUS_PORT_TC1] = GMBUS_PIN_9_TC1_ICP,
16367dc1f92fSAditya Swarup 	[ADLS_DDC_BUS_PORT_TC2] = GMBUS_PIN_10_TC2_ICP,
16377dc1f92fSAditya Swarup 	[ADLS_DDC_BUS_PORT_TC3] = GMBUS_PIN_11_TC3_ICP,
16387dc1f92fSAditya Swarup 	[ADLS_DDC_BUS_PORT_TC4] = GMBUS_PIN_12_TC4_ICP,
16397dc1f92fSAditya Swarup };
16407dc1f92fSAditya Swarup 
1641d2063080SLyude Paul static const u8 gen9bc_tgp_ddc_pin_map[] = {
1642d2063080SLyude Paul 	[DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
1643d2063080SLyude Paul 	[DDC_BUS_DDI_C] = GMBUS_PIN_9_TC1_ICP,
1644d2063080SLyude Paul 	[DDC_BUS_DDI_D] = GMBUS_PIN_10_TC2_ICP,
1645d2063080SLyude Paul };
1646d2063080SLyude Paul 
1647dbd440d8SJani Nikula static u8 map_ddc_pin(struct drm_i915_private *i915, u8 vbt_pin)
1648df0566a6SJani Nikula {
1649df0566a6SJani Nikula 	const u8 *ddc_pin_map;
1650df0566a6SJani Nikula 	int n_entries;
1651df0566a6SJani Nikula 
1652dbd440d8SJani Nikula 	if (HAS_PCH_ADP(i915)) {
16537dc1f92fSAditya Swarup 		ddc_pin_map = adls_ddc_pin_map;
16547dc1f92fSAditya Swarup 		n_entries = ARRAY_SIZE(adls_ddc_pin_map);
1655dbd440d8SJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_DG1) {
1656fb7318c3SLucas De Marchi 		return vbt_pin;
1657dbd440d8SJani Nikula 	} else if (IS_ROCKETLAKE(i915) && INTEL_PCH_TYPE(i915) == PCH_TGP) {
1658956aee8fSLee Shawn C 		ddc_pin_map = rkl_pch_tgp_ddc_pin_map;
1659956aee8fSLee Shawn C 		n_entries = ARRAY_SIZE(rkl_pch_tgp_ddc_pin_map);
1660dbd440d8SJani Nikula 	} else if (HAS_PCH_TGP(i915) && IS_GEN9_BC(i915)) {
1661d2063080SLyude Paul 		ddc_pin_map = gen9bc_tgp_ddc_pin_map;
1662d2063080SLyude Paul 		n_entries = ARRAY_SIZE(gen9bc_tgp_ddc_pin_map);
1663dbd440d8SJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_ICP) {
1664df0566a6SJani Nikula 		ddc_pin_map = icp_ddc_pin_map;
1665df0566a6SJani Nikula 		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
1666dbd440d8SJani Nikula 	} else if (HAS_PCH_CNP(i915)) {
1667df0566a6SJani Nikula 		ddc_pin_map = cnp_ddc_pin_map;
1668df0566a6SJani Nikula 		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
1669df0566a6SJani Nikula 	} else {
1670df0566a6SJani Nikula 		/* Assuming direct map */
1671df0566a6SJani Nikula 		return vbt_pin;
1672df0566a6SJani Nikula 	}
1673df0566a6SJani Nikula 
1674df0566a6SJani Nikula 	if (vbt_pin < n_entries && ddc_pin_map[vbt_pin] != 0)
1675df0566a6SJani Nikula 		return ddc_pin_map[vbt_pin];
1676df0566a6SJani Nikula 
1677dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1678e92cbf38SWambui Karuga 		    "Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
1679df0566a6SJani Nikula 		    vbt_pin);
1680df0566a6SJani Nikula 	return 0;
1681df0566a6SJani Nikula }
1682df0566a6SJani Nikula 
16834628142aSLucas De Marchi static enum port __dvo_port_to_port(int n_ports, int n_dvo,
16844628142aSLucas De Marchi 				    const int port_mapping[][3], u8 dvo_port)
1685df0566a6SJani Nikula {
1686df0566a6SJani Nikula 	enum port port;
1687df0566a6SJani Nikula 	int i;
1688df0566a6SJani Nikula 
16894628142aSLucas De Marchi 	for (port = PORT_A; port < n_ports; port++) {
16904628142aSLucas De Marchi 		for (i = 0; i < n_dvo; i++) {
16914628142aSLucas De Marchi 			if (port_mapping[port][i] == -1)
1692df0566a6SJani Nikula 				break;
1693df0566a6SJani Nikula 
16944628142aSLucas De Marchi 			if (dvo_port == port_mapping[port][i])
1695df0566a6SJani Nikula 				return port;
1696df0566a6SJani Nikula 		}
1697df0566a6SJani Nikula 	}
1698df0566a6SJani Nikula 
1699df0566a6SJani Nikula 	return PORT_NONE;
1700df0566a6SJani Nikula }
1701df0566a6SJani Nikula 
1702dbd440d8SJani Nikula static enum port dvo_port_to_port(struct drm_i915_private *i915,
17034628142aSLucas De Marchi 				  u8 dvo_port)
17044628142aSLucas De Marchi {
17054628142aSLucas De Marchi 	/*
17064628142aSLucas De Marchi 	 * Each DDI port can have more than one value on the "DVO Port" field,
17074628142aSLucas De Marchi 	 * so look for all the possible values for each port.
17084628142aSLucas De Marchi 	 */
17094628142aSLucas De Marchi 	static const int port_mapping[][3] = {
17104628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
17114628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
17124628142aSLucas De Marchi 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
17134628142aSLucas De Marchi 		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
17148c1a8f12SMatt Roper 		[PORT_E] = { DVO_PORT_HDMIE, DVO_PORT_DPE, DVO_PORT_CRT },
17154628142aSLucas De Marchi 		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
17164628142aSLucas De Marchi 		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
1717176430ccSVille Syrjälä 		[PORT_H] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
1718176430ccSVille Syrjälä 		[PORT_I] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
17194628142aSLucas De Marchi 	};
17204628142aSLucas De Marchi 	/*
17211d8ca002SVille Syrjälä 	 * RKL VBT uses PHY based mapping. Combo PHYs A,B,C,D
17221d8ca002SVille Syrjälä 	 * map to DDI A,B,TC1,TC2 respectively.
17234628142aSLucas De Marchi 	 */
17244628142aSLucas De Marchi 	static const int rkl_port_mapping[][3] = {
17254628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
17264628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
17274628142aSLucas De Marchi 		[PORT_C] = { -1 },
17281d8ca002SVille Syrjälä 		[PORT_TC1] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
17291d8ca002SVille Syrjälä 		[PORT_TC2] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
17304628142aSLucas De Marchi 	};
173118c283dfSAditya Swarup 	/*
173218c283dfSAditya Swarup 	 * Alderlake S ports used in the driver are PORT_A, PORT_D, PORT_E,
173318c283dfSAditya Swarup 	 * PORT_F and PORT_G, we need to map that to correct VBT sections.
173418c283dfSAditya Swarup 	 */
173518c283dfSAditya Swarup 	static const int adls_port_mapping[][3] = {
173618c283dfSAditya Swarup 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
173718c283dfSAditya Swarup 		[PORT_B] = { -1 },
173818c283dfSAditya Swarup 		[PORT_C] = { -1 },
173918c283dfSAditya Swarup 		[PORT_TC1] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
174018c283dfSAditya Swarup 		[PORT_TC2] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
174118c283dfSAditya Swarup 		[PORT_TC3] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
174218c283dfSAditya Swarup 		[PORT_TC4] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
174318c283dfSAditya Swarup 	};
17444628142aSLucas De Marchi 
1745dbd440d8SJani Nikula 	if (IS_ALDERLAKE_S(i915))
174618c283dfSAditya Swarup 		return __dvo_port_to_port(ARRAY_SIZE(adls_port_mapping),
174718c283dfSAditya Swarup 					  ARRAY_SIZE(adls_port_mapping[0]),
174818c283dfSAditya Swarup 					  adls_port_mapping,
174918c283dfSAditya Swarup 					  dvo_port);
1750dbd440d8SJani Nikula 	else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
17514628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(rkl_port_mapping),
17524628142aSLucas De Marchi 					  ARRAY_SIZE(rkl_port_mapping[0]),
17534628142aSLucas De Marchi 					  rkl_port_mapping,
17544628142aSLucas De Marchi 					  dvo_port);
17554628142aSLucas De Marchi 	else
17564628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(port_mapping),
17574628142aSLucas De Marchi 					  ARRAY_SIZE(port_mapping[0]),
17584628142aSLucas De Marchi 					  port_mapping,
17594628142aSLucas De Marchi 					  dvo_port);
17604628142aSLucas De Marchi }
17614628142aSLucas De Marchi 
1762b60e320bSLee Shawn C static int parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)
1763b60e320bSLee Shawn C {
1764b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
1765b60e320bSLee Shawn C 	default:
1766b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_DEF:
1767b60e320bSLee Shawn C 		return 0;
1768b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR20:
1769b60e320bSLee Shawn C 		return 2000000;
1770b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR13P5:
1771b60e320bSLee Shawn C 		return 1350000;
1772b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR10:
1773b60e320bSLee Shawn C 		return 1000000;
1774b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR3:
1775b60e320bSLee Shawn C 		return 810000;
1776b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR2:
1777b60e320bSLee Shawn C 		return 540000;
1778b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR:
1779b60e320bSLee Shawn C 		return 270000;
1780b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_LBR:
1781b60e320bSLee Shawn C 		return 162000;
1782b60e320bSLee Shawn C 	}
1783b60e320bSLee Shawn C }
1784b60e320bSLee Shawn C 
1785b60e320bSLee Shawn C static int parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)
1786b60e320bSLee Shawn C {
1787b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
1788b60e320bSLee Shawn C 	default:
1789b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR3:
1790b60e320bSLee Shawn C 		return 810000;
1791b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR2:
1792b60e320bSLee Shawn C 		return 540000;
1793b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR:
1794b60e320bSLee Shawn C 		return 270000;
1795b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_LBR:
1796b60e320bSLee Shawn C 		return 162000;
1797b60e320bSLee Shawn C 	}
1798b60e320bSLee Shawn C }
1799b60e320bSLee Shawn C 
1800dbd440d8SJani Nikula static void parse_ddi_port(struct drm_i915_private *i915,
1801*ef0096e4SJani Nikula 			   struct display_device_data *devdata)
1802df0566a6SJani Nikula {
1803d1dad6f4SJani Nikula 	const struct child_device_config *child = &devdata->child;
1804df0566a6SJani Nikula 	struct ddi_vbt_port_info *info;
1805df0566a6SJani Nikula 	bool is_dvi, is_hdmi, is_dp, is_edp, is_crt;
1806df0566a6SJani Nikula 	enum port port;
1807df0566a6SJani Nikula 
1808dbd440d8SJani Nikula 	port = dvo_port_to_port(i915, child->dvo_port);
1809df0566a6SJani Nikula 	if (port == PORT_NONE)
1810df0566a6SJani Nikula 		return;
1811df0566a6SJani Nikula 
1812dbd440d8SJani Nikula 	info = &i915->vbt.ddi_port_info[port];
1813df0566a6SJani Nikula 
1814df0566a6SJani Nikula 	if (info->child) {
1815dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1816e92cbf38SWambui Karuga 			    "More than one child device for port %c in VBT, using the first.\n",
1817df0566a6SJani Nikula 			    port_name(port));
1818df0566a6SJani Nikula 		return;
1819df0566a6SJani Nikula 	}
1820df0566a6SJani Nikula 
1821df0566a6SJani Nikula 	is_dvi = child->device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
1822df0566a6SJani Nikula 	is_dp = child->device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
1823df0566a6SJani Nikula 	is_crt = child->device_type & DEVICE_TYPE_ANALOG_OUTPUT;
1824df0566a6SJani Nikula 	is_hdmi = is_dvi && (child->device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
1825df0566a6SJani Nikula 	is_edp = is_dp && (child->device_type & DEVICE_TYPE_INTERNAL_CONNECTOR);
1826df0566a6SJani Nikula 
1827dbd440d8SJani Nikula 	if (port == PORT_A && is_dvi && INTEL_GEN(i915) < 12) {
1828dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1829e92cbf38SWambui Karuga 			    "VBT claims port A supports DVI%s, ignoring\n",
1830df0566a6SJani Nikula 			    is_hdmi ? "/HDMI" : "");
1831df0566a6SJani Nikula 		is_dvi = false;
1832df0566a6SJani Nikula 		is_hdmi = false;
1833df0566a6SJani Nikula 	}
1834df0566a6SJani Nikula 
1835df0566a6SJani Nikula 	info->supports_dvi = is_dvi;
1836df0566a6SJani Nikula 	info->supports_hdmi = is_hdmi;
1837df0566a6SJani Nikula 	info->supports_dp = is_dp;
1838df0566a6SJani Nikula 	info->supports_edp = is_edp;
1839df0566a6SJani Nikula 
1840*ef0096e4SJani Nikula 	if (i915->vbt.version >= 195)
1841df0566a6SJani Nikula 		info->supports_typec_usb = child->dp_usb_type_c;
1842df0566a6SJani Nikula 
1843*ef0096e4SJani Nikula 	if (i915->vbt.version >= 209)
1844df0566a6SJani Nikula 		info->supports_tbt = child->tbt;
1845df0566a6SJani Nikula 
1846dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1847e92cbf38SWambui Karuga 		    "Port %c VBT info: CRT:%d DVI:%d HDMI:%d DP:%d eDP:%d LSPCON:%d USB-Type-C:%d TBT:%d DSC:%d\n",
1848df0566a6SJani Nikula 		    port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp,
1849dbd440d8SJani Nikula 		    HAS_LSPCON(i915) && child->lspcon,
18506e0d46e9SJani Nikula 		    info->supports_typec_usb, info->supports_tbt,
18516e0d46e9SJani Nikula 		    devdata->dsc != NULL);
1852df0566a6SJani Nikula 
1853df0566a6SJani Nikula 	if (is_dvi) {
1854df0566a6SJani Nikula 		u8 ddc_pin;
1855df0566a6SJani Nikula 
1856dbd440d8SJani Nikula 		ddc_pin = map_ddc_pin(i915, child->ddc_pin);
1857dbd440d8SJani Nikula 		if (intel_gmbus_is_valid_pin(i915, ddc_pin)) {
1858df0566a6SJani Nikula 			info->alternate_ddc_pin = ddc_pin;
1859dbd440d8SJani Nikula 			sanitize_ddc_pin(i915, port);
1860df0566a6SJani Nikula 		} else {
1861dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1862e92cbf38SWambui Karuga 				    "Port %c has invalid DDC pin %d, "
1863df0566a6SJani Nikula 				    "sticking to defaults\n",
1864df0566a6SJani Nikula 				    port_name(port), ddc_pin);
1865df0566a6SJani Nikula 		}
1866df0566a6SJani Nikula 	}
1867df0566a6SJani Nikula 
1868df0566a6SJani Nikula 	if (is_dp) {
1869df0566a6SJani Nikula 		info->alternate_aux_channel = child->aux_channel;
1870df0566a6SJani Nikula 
1871dbd440d8SJani Nikula 		sanitize_aux_ch(i915, port);
1872df0566a6SJani Nikula 	}
1873df0566a6SJani Nikula 
1874*ef0096e4SJani Nikula 	if (i915->vbt.version >= 158) {
1875df0566a6SJani Nikula 		/* The VBT HDMI level shift values match the table we have. */
1876df0566a6SJani Nikula 		u8 hdmi_level_shift = child->hdmi_level_shifter_value;
1877dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
18786ee8d381SJani Nikula 			    "Port %c VBT HDMI level shift: %d\n",
1879df0566a6SJani Nikula 			    port_name(port),
1880df0566a6SJani Nikula 			    hdmi_level_shift);
1881df0566a6SJani Nikula 		info->hdmi_level_shift = hdmi_level_shift;
18827a0073d6SJani Nikula 		info->hdmi_level_shift_set = true;
1883df0566a6SJani Nikula 	}
1884df0566a6SJani Nikula 
1885*ef0096e4SJani Nikula 	if (i915->vbt.version >= 204) {
1886df0566a6SJani Nikula 		int max_tmds_clock;
1887df0566a6SJani Nikula 
1888df0566a6SJani Nikula 		switch (child->hdmi_max_data_rate) {
1889df0566a6SJani Nikula 		default:
1890df0566a6SJani Nikula 			MISSING_CASE(child->hdmi_max_data_rate);
1891df561f66SGustavo A. R. Silva 			fallthrough;
1892df0566a6SJani Nikula 		case HDMI_MAX_DATA_RATE_PLATFORM:
1893df0566a6SJani Nikula 			max_tmds_clock = 0;
1894df0566a6SJani Nikula 			break;
1895df0566a6SJani Nikula 		case HDMI_MAX_DATA_RATE_297:
1896df0566a6SJani Nikula 			max_tmds_clock = 297000;
1897df0566a6SJani Nikula 			break;
1898df0566a6SJani Nikula 		case HDMI_MAX_DATA_RATE_165:
1899df0566a6SJani Nikula 			max_tmds_clock = 165000;
1900df0566a6SJani Nikula 			break;
1901df0566a6SJani Nikula 		}
1902df0566a6SJani Nikula 
1903df0566a6SJani Nikula 		if (max_tmds_clock)
1904dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
19056ee8d381SJani Nikula 				    "Port %c VBT HDMI max TMDS clock: %d kHz\n",
1906df0566a6SJani Nikula 				    port_name(port), max_tmds_clock);
1907df0566a6SJani Nikula 		info->max_tmds_clock = max_tmds_clock;
1908df0566a6SJani Nikula 	}
1909df0566a6SJani Nikula 
1910df0566a6SJani Nikula 	/* Parse the I_boost config for SKL and above */
1911*ef0096e4SJani Nikula 	if (i915->vbt.version >= 196 && child->iboost) {
1912df0566a6SJani Nikula 		info->dp_boost_level = translate_iboost(child->dp_iboost_level);
1913dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
19146ee8d381SJani Nikula 			    "Port %c VBT (e)DP boost level: %d\n",
1915df0566a6SJani Nikula 			    port_name(port), info->dp_boost_level);
1916df0566a6SJani Nikula 		info->hdmi_boost_level = translate_iboost(child->hdmi_iboost_level);
1917dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
19186ee8d381SJani Nikula 			    "Port %c VBT HDMI boost level: %d\n",
1919df0566a6SJani Nikula 			    port_name(port), info->hdmi_boost_level);
1920df0566a6SJani Nikula 	}
1921df0566a6SJani Nikula 
1922df0566a6SJani Nikula 	/* DP max link rate for CNL+ */
1923*ef0096e4SJani Nikula 	if (i915->vbt.version >= 216) {
1924*ef0096e4SJani Nikula 		if (i915->vbt.version >= 230)
1925b60e320bSLee Shawn C 			info->dp_max_link_rate = parse_bdb_230_dp_max_link_rate(child->dp_max_link_rate);
1926b60e320bSLee Shawn C 		else
1927b60e320bSLee Shawn C 			info->dp_max_link_rate = parse_bdb_216_dp_max_link_rate(child->dp_max_link_rate);
1928b60e320bSLee Shawn C 
1929dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
19306ee8d381SJani Nikula 			    "Port %c VBT DP max link rate: %d\n",
1931df0566a6SJani Nikula 			    port_name(port), info->dp_max_link_rate);
1932df0566a6SJani Nikula 	}
1933df0566a6SJani Nikula 
1934df0566a6SJani Nikula 	info->child = child;
1935df0566a6SJani Nikula }
1936df0566a6SJani Nikula 
1937*ef0096e4SJani Nikula static void parse_ddi_ports(struct drm_i915_private *i915)
1938df0566a6SJani Nikula {
1939d1dad6f4SJani Nikula 	struct display_device_data *devdata;
1940df0566a6SJani Nikula 
1941dbd440d8SJani Nikula 	if (!HAS_DDI(i915) && !IS_CHERRYVIEW(i915))
1942df0566a6SJani Nikula 		return;
1943df0566a6SJani Nikula 
1944*ef0096e4SJani Nikula 	if (i915->vbt.version < 155)
1945df0566a6SJani Nikula 		return;
1946df0566a6SJani Nikula 
1947dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node)
1948*ef0096e4SJani Nikula 		parse_ddi_port(i915, devdata);
1949df0566a6SJani Nikula }
1950df0566a6SJani Nikula 
1951df0566a6SJani Nikula static void
1952dbd440d8SJani Nikula parse_general_definitions(struct drm_i915_private *i915,
1953df0566a6SJani Nikula 			  const struct bdb_header *bdb)
1954df0566a6SJani Nikula {
1955df0566a6SJani Nikula 	const struct bdb_general_definitions *defs;
19560d9ef19bSJani Nikula 	struct display_device_data *devdata;
1957df0566a6SJani Nikula 	const struct child_device_config *child;
19580d9ef19bSJani Nikula 	int i, child_device_num;
1959df0566a6SJani Nikula 	u8 expected_size;
1960df0566a6SJani Nikula 	u16 block_size;
1961df0566a6SJani Nikula 	int bus_pin;
1962df0566a6SJani Nikula 
1963df0566a6SJani Nikula 	defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
1964df0566a6SJani Nikula 	if (!defs) {
1965dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1966e92cbf38SWambui Karuga 			    "No general definition block is found, no devices defined.\n");
1967df0566a6SJani Nikula 		return;
1968df0566a6SJani Nikula 	}
1969df0566a6SJani Nikula 
1970df0566a6SJani Nikula 	block_size = get_blocksize(defs);
1971df0566a6SJani Nikula 	if (block_size < sizeof(*defs)) {
1972dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1973e92cbf38SWambui Karuga 			    "General definitions block too small (%u)\n",
1974df0566a6SJani Nikula 			    block_size);
1975df0566a6SJani Nikula 		return;
1976df0566a6SJani Nikula 	}
1977df0566a6SJani Nikula 
1978df0566a6SJani Nikula 	bus_pin = defs->crt_ddc_gmbus_pin;
1979dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "crt_ddc_bus_pin: %d\n", bus_pin);
1980dbd440d8SJani Nikula 	if (intel_gmbus_is_valid_pin(i915, bus_pin))
1981dbd440d8SJani Nikula 		i915->vbt.crt_ddc_pin = bus_pin;
1982df0566a6SJani Nikula 
1983df0566a6SJani Nikula 	if (bdb->version < 106) {
1984df0566a6SJani Nikula 		expected_size = 22;
1985df0566a6SJani Nikula 	} else if (bdb->version < 111) {
1986df0566a6SJani Nikula 		expected_size = 27;
1987df0566a6SJani Nikula 	} else if (bdb->version < 195) {
1988df0566a6SJani Nikula 		expected_size = LEGACY_CHILD_DEVICE_CONFIG_SIZE;
1989df0566a6SJani Nikula 	} else if (bdb->version == 195) {
1990df0566a6SJani Nikula 		expected_size = 37;
1991df0566a6SJani Nikula 	} else if (bdb->version <= 215) {
1992df0566a6SJani Nikula 		expected_size = 38;
1993e4b3c3b3SJosé Roberto de Souza 	} else if (bdb->version <= 237) {
1994df0566a6SJani Nikula 		expected_size = 39;
1995df0566a6SJani Nikula 	} else {
1996df0566a6SJani Nikula 		expected_size = sizeof(*child);
1997df0566a6SJani Nikula 		BUILD_BUG_ON(sizeof(*child) < 39);
1998dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
1999e92cbf38SWambui Karuga 			"Expected child device config size for VBT version %u not known; assuming %u\n",
2000df0566a6SJani Nikula 			bdb->version, expected_size);
2001df0566a6SJani Nikula 	}
2002df0566a6SJani Nikula 
2003df0566a6SJani Nikula 	/* Flag an error for unexpected size, but continue anyway. */
2004df0566a6SJani Nikula 	if (defs->child_dev_size != expected_size)
2005dbd440d8SJani Nikula 		drm_err(&i915->drm,
2006e92cbf38SWambui Karuga 			"Unexpected child device config size %u (expected %u for VBT version %u)\n",
2007df0566a6SJani Nikula 			defs->child_dev_size, expected_size, bdb->version);
2008df0566a6SJani Nikula 
2009df0566a6SJani Nikula 	/* The legacy sized child device config is the minimum we need. */
2010df0566a6SJani Nikula 	if (defs->child_dev_size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
2011dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2012e92cbf38SWambui Karuga 			    "Child device config size %u is too small.\n",
2013df0566a6SJani Nikula 			    defs->child_dev_size);
2014df0566a6SJani Nikula 		return;
2015df0566a6SJani Nikula 	}
2016df0566a6SJani Nikula 
2017df0566a6SJani Nikula 	/* get the number of child device */
2018df0566a6SJani Nikula 	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
2019df0566a6SJani Nikula 
2020df0566a6SJani Nikula 	for (i = 0; i < child_device_num; i++) {
2021df0566a6SJani Nikula 		child = child_device_ptr(defs, i);
2022df0566a6SJani Nikula 		if (!child->device_type)
2023df0566a6SJani Nikula 			continue;
2024df0566a6SJani Nikula 
2025dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2026e92cbf38SWambui Karuga 			    "Found VBT child device with type 0x%x\n",
2027bdeb18dbSMatt Roper 			    child->device_type);
2028bdeb18dbSMatt Roper 
20290d9ef19bSJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
20300d9ef19bSJani Nikula 		if (!devdata)
20310d9ef19bSJani Nikula 			break;
20320d9ef19bSJani Nikula 
2033df0566a6SJani Nikula 		/*
2034df0566a6SJani Nikula 		 * Copy as much as we know (sizeof) and is available
20350d9ef19bSJani Nikula 		 * (child_dev_size) of the child device config. Accessing the
20360d9ef19bSJani Nikula 		 * data must depend on VBT version.
2037df0566a6SJani Nikula 		 */
20380d9ef19bSJani Nikula 		memcpy(&devdata->child, child,
2039df0566a6SJani Nikula 		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
20400d9ef19bSJani Nikula 
2041dbd440d8SJani Nikula 		list_add_tail(&devdata->node, &i915->vbt.display_devices);
2042df0566a6SJani Nikula 	}
20430d9ef19bSJani Nikula 
2044dbd440d8SJani Nikula 	if (list_empty(&i915->vbt.display_devices))
2045dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2046e92cbf38SWambui Karuga 			    "no child dev is parsed from VBT\n");
2047df0566a6SJani Nikula }
2048df0566a6SJani Nikula 
2049df0566a6SJani Nikula /* Common defaults which may be overridden by VBT. */
2050df0566a6SJani Nikula static void
2051dbd440d8SJani Nikula init_vbt_defaults(struct drm_i915_private *i915)
2052df0566a6SJani Nikula {
2053dbd440d8SJani Nikula 	i915->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
2054df0566a6SJani Nikula 
2055df0566a6SJani Nikula 	/* Default to having backlight */
2056dbd440d8SJani Nikula 	i915->vbt.backlight.present = true;
2057df0566a6SJani Nikula 
2058df0566a6SJani Nikula 	/* LFP panel data */
2059dbd440d8SJani Nikula 	i915->vbt.lvds_dither = 1;
2060df0566a6SJani Nikula 
2061df0566a6SJani Nikula 	/* SDVO panel data */
2062dbd440d8SJani Nikula 	i915->vbt.sdvo_lvds_vbt_mode = NULL;
2063df0566a6SJani Nikula 
2064df0566a6SJani Nikula 	/* general features */
2065dbd440d8SJani Nikula 	i915->vbt.int_tv_support = 1;
2066dbd440d8SJani Nikula 	i915->vbt.int_crt_support = 1;
2067df0566a6SJani Nikula 
2068df0566a6SJani Nikula 	/* driver features */
2069dbd440d8SJani Nikula 	i915->vbt.int_lvds_support = 1;
2070df0566a6SJani Nikula 
2071df0566a6SJani Nikula 	/* Default to using SSC */
2072dbd440d8SJani Nikula 	i915->vbt.lvds_use_ssc = 1;
2073df0566a6SJani Nikula 	/*
2074df0566a6SJani Nikula 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
2075df0566a6SJani Nikula 	 * clock for LVDS.
2076df0566a6SJani Nikula 	 */
2077dbd440d8SJani Nikula 	i915->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(i915,
2078dbd440d8SJani Nikula 							   !HAS_PCH_SPLIT(i915));
2079dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "Set default to SSC at %d kHz\n",
2080dbd440d8SJani Nikula 		    i915->vbt.lvds_ssc_freq);
2081df0566a6SJani Nikula }
2082df0566a6SJani Nikula 
2083df0566a6SJani Nikula /* Defaults to initialize only if there is no VBT. */
2084df0566a6SJani Nikula static void
2085dbd440d8SJani Nikula init_vbt_missing_defaults(struct drm_i915_private *i915)
2086df0566a6SJani Nikula {
2087df0566a6SJani Nikula 	enum port port;
2088df0566a6SJani Nikula 
2089c4a774c4SJani Nikula 	for_each_port(port) {
2090df0566a6SJani Nikula 		struct ddi_vbt_port_info *info =
2091dbd440d8SJani Nikula 			&i915->vbt.ddi_port_info[port];
2092dbd440d8SJani Nikula 		enum phy phy = intel_port_to_phy(i915, port);
2093df0566a6SJani Nikula 
2094df0566a6SJani Nikula 		/*
2095df0566a6SJani Nikula 		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
2096df0566a6SJani Nikula 		 * to detect it.
2097df0566a6SJani Nikula 		 */
2098dbd440d8SJani Nikula 		if (intel_phy_is_tc(i915, phy))
2099df0566a6SJani Nikula 			continue;
2100df0566a6SJani Nikula 
2101df0566a6SJani Nikula 		info->supports_dvi = (port != PORT_A && port != PORT_E);
2102df0566a6SJani Nikula 		info->supports_hdmi = info->supports_dvi;
2103df0566a6SJani Nikula 		info->supports_dp = (port != PORT_E);
2104df0566a6SJani Nikula 		info->supports_edp = (port == PORT_A);
2105df0566a6SJani Nikula 	}
2106df0566a6SJani Nikula }
2107df0566a6SJani Nikula 
2108df0566a6SJani Nikula static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
2109df0566a6SJani Nikula {
2110df0566a6SJani Nikula 	const void *_vbt = vbt;
2111df0566a6SJani Nikula 
2112df0566a6SJani Nikula 	return _vbt + vbt->bdb_offset;
2113df0566a6SJani Nikula }
2114df0566a6SJani Nikula 
2115df0566a6SJani Nikula /**
2116df0566a6SJani Nikula  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
2117df0566a6SJani Nikula  * @buf:	pointer to a buffer to validate
2118df0566a6SJani Nikula  * @size:	size of the buffer
2119df0566a6SJani Nikula  *
2120df0566a6SJani Nikula  * Returns true on valid VBT.
2121df0566a6SJani Nikula  */
2122df0566a6SJani Nikula bool intel_bios_is_valid_vbt(const void *buf, size_t size)
2123df0566a6SJani Nikula {
2124df0566a6SJani Nikula 	const struct vbt_header *vbt = buf;
2125df0566a6SJani Nikula 	const struct bdb_header *bdb;
2126df0566a6SJani Nikula 
2127df0566a6SJani Nikula 	if (!vbt)
2128df0566a6SJani Nikula 		return false;
2129df0566a6SJani Nikula 
2130df0566a6SJani Nikula 	if (sizeof(struct vbt_header) > size) {
2131df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
2132df0566a6SJani Nikula 		return false;
2133df0566a6SJani Nikula 	}
2134df0566a6SJani Nikula 
2135df0566a6SJani Nikula 	if (memcmp(vbt->signature, "$VBT", 4)) {
2136df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("VBT invalid signature\n");
2137df0566a6SJani Nikula 		return false;
2138df0566a6SJani Nikula 	}
2139df0566a6SJani Nikula 
2140ff00ff96SLucas De Marchi 	if (vbt->vbt_size > size) {
2141ff00ff96SLucas De Marchi 		DRM_DEBUG_DRIVER("VBT incomplete (vbt_size overflows)\n");
2142ff00ff96SLucas De Marchi 		return false;
2143ff00ff96SLucas De Marchi 	}
2144ff00ff96SLucas De Marchi 
2145ff00ff96SLucas De Marchi 	size = vbt->vbt_size;
2146ff00ff96SLucas De Marchi 
2147df0566a6SJani Nikula 	if (range_overflows_t(size_t,
2148df0566a6SJani Nikula 			      vbt->bdb_offset,
2149df0566a6SJani Nikula 			      sizeof(struct bdb_header),
2150df0566a6SJani Nikula 			      size)) {
2151df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("BDB header incomplete\n");
2152df0566a6SJani Nikula 		return false;
2153df0566a6SJani Nikula 	}
2154df0566a6SJani Nikula 
2155df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
2156df0566a6SJani Nikula 	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
2157df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("BDB incomplete\n");
2158df0566a6SJani Nikula 		return false;
2159df0566a6SJani Nikula 	}
2160df0566a6SJani Nikula 
2161df0566a6SJani Nikula 	return vbt;
2162df0566a6SJani Nikula }
2163df0566a6SJani Nikula 
2164dbd440d8SJani Nikula static struct vbt_header *oprom_get_vbt(struct drm_i915_private *i915)
2165df0566a6SJani Nikula {
2166dbd440d8SJani Nikula 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
21672cded152SLucas De Marchi 	void __iomem *p = NULL, *oprom;
2168fd0186ceSLucas De Marchi 	struct vbt_header *vbt;
2169fd0186ceSLucas De Marchi 	u16 vbt_size;
21702cded152SLucas De Marchi 	size_t i, size;
21712cded152SLucas De Marchi 
21722cded152SLucas De Marchi 	oprom = pci_map_rom(pdev, &size);
21732cded152SLucas De Marchi 	if (!oprom)
21742cded152SLucas De Marchi 		return NULL;
2175df0566a6SJani Nikula 
2176df0566a6SJani Nikula 	/* Scour memory looking for the VBT signature. */
217798cf5c9aSLucas De Marchi 	for (i = 0; i + 4 < size; i += 4) {
2178496f50a6SLucas De Marchi 		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
2179df0566a6SJani Nikula 			continue;
2180df0566a6SJani Nikula 
2181fd0186ceSLucas De Marchi 		p = oprom + i;
2182fd0186ceSLucas De Marchi 		size -= i;
2183df0566a6SJani Nikula 		break;
2184df0566a6SJani Nikula 	}
2185df0566a6SJani Nikula 
2186fd0186ceSLucas De Marchi 	if (!p)
21872cded152SLucas De Marchi 		goto err_unmap_oprom;
2188fd0186ceSLucas De Marchi 
2189fd0186ceSLucas De Marchi 	if (sizeof(struct vbt_header) > size) {
2190dbd440d8SJani Nikula 		drm_dbg(&i915->drm, "VBT header incomplete\n");
21912cded152SLucas De Marchi 		goto err_unmap_oprom;
2192fd0186ceSLucas De Marchi 	}
2193fd0186ceSLucas De Marchi 
2194fd0186ceSLucas De Marchi 	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
2195fd0186ceSLucas De Marchi 	if (vbt_size > size) {
2196dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
2197e92cbf38SWambui Karuga 			"VBT incomplete (vbt_size overflows)\n");
21982cded152SLucas De Marchi 		goto err_unmap_oprom;
2199fd0186ceSLucas De Marchi 	}
2200fd0186ceSLucas De Marchi 
2201fd0186ceSLucas De Marchi 	/* The rest will be validated by intel_bios_is_valid_vbt() */
2202fd0186ceSLucas De Marchi 	vbt = kmalloc(vbt_size, GFP_KERNEL);
2203fd0186ceSLucas De Marchi 	if (!vbt)
22042cded152SLucas De Marchi 		goto err_unmap_oprom;
2205fd0186ceSLucas De Marchi 
2206fd0186ceSLucas De Marchi 	memcpy_fromio(vbt, p, vbt_size);
2207fd0186ceSLucas De Marchi 
2208fd0186ceSLucas De Marchi 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
2209fd0186ceSLucas De Marchi 		goto err_free_vbt;
2210fd0186ceSLucas De Marchi 
22112cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
22122cded152SLucas De Marchi 
2213fd0186ceSLucas De Marchi 	return vbt;
2214fd0186ceSLucas De Marchi 
2215fd0186ceSLucas De Marchi err_free_vbt:
2216fd0186ceSLucas De Marchi 	kfree(vbt);
22172cded152SLucas De Marchi err_unmap_oprom:
22182cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
2219fd0186ceSLucas De Marchi 
2220df0566a6SJani Nikula 	return NULL;
2221df0566a6SJani Nikula }
2222df0566a6SJani Nikula 
2223df0566a6SJani Nikula /**
2224df0566a6SJani Nikula  * intel_bios_init - find VBT and initialize settings from the BIOS
2225dbd440d8SJani Nikula  * @i915: i915 device instance
2226df0566a6SJani Nikula  *
2227df0566a6SJani Nikula  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
2228df0566a6SJani Nikula  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
2229df0566a6SJani Nikula  * initialize some defaults if the VBT is not present at all.
2230df0566a6SJani Nikula  */
2231dbd440d8SJani Nikula void intel_bios_init(struct drm_i915_private *i915)
2232df0566a6SJani Nikula {
2233dbd440d8SJani Nikula 	const struct vbt_header *vbt = i915->opregion.vbt;
22342cded152SLucas De Marchi 	struct vbt_header *oprom_vbt = NULL;
2235df0566a6SJani Nikula 	const struct bdb_header *bdb;
2236df0566a6SJani Nikula 
2237dbd440d8SJani Nikula 	INIT_LIST_HEAD(&i915->vbt.display_devices);
22380d9ef19bSJani Nikula 
2239dbd440d8SJani Nikula 	if (!HAS_DISPLAY(i915)) {
2240dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2241e92cbf38SWambui Karuga 			    "Skipping VBT init due to disabled display.\n");
2242df0566a6SJani Nikula 		return;
2243df0566a6SJani Nikula 	}
2244df0566a6SJani Nikula 
2245dbd440d8SJani Nikula 	init_vbt_defaults(i915);
2246df0566a6SJani Nikula 
2247df0566a6SJani Nikula 	/* If the OpRegion does not have VBT, look in PCI ROM. */
2248df0566a6SJani Nikula 	if (!vbt) {
2249dbd440d8SJani Nikula 		oprom_vbt = oprom_get_vbt(i915);
22502cded152SLucas De Marchi 		if (!oprom_vbt)
2251df0566a6SJani Nikula 			goto out;
2252df0566a6SJani Nikula 
22532cded152SLucas De Marchi 		vbt = oprom_vbt;
2254df0566a6SJani Nikula 
2255dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Found valid VBT in PCI ROM\n");
2256df0566a6SJani Nikula 	}
2257df0566a6SJani Nikula 
2258df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
2259*ef0096e4SJani Nikula 	i915->vbt.version = bdb->version;
2260df0566a6SJani Nikula 
2261dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
2262e92cbf38SWambui Karuga 		    "VBT signature \"%.*s\", BDB version %d\n",
2263df0566a6SJani Nikula 		    (int)sizeof(vbt->signature), vbt->signature, bdb->version);
2264df0566a6SJani Nikula 
2265df0566a6SJani Nikula 	/* Grab useful general definitions */
2266dbd440d8SJani Nikula 	parse_general_features(i915, bdb);
2267dbd440d8SJani Nikula 	parse_general_definitions(i915, bdb);
2268dbd440d8SJani Nikula 	parse_panel_options(i915, bdb);
2269dbd440d8SJani Nikula 	parse_panel_dtd(i915, bdb);
2270dbd440d8SJani Nikula 	parse_lfp_backlight(i915, bdb);
2271dbd440d8SJani Nikula 	parse_sdvo_panel_data(i915, bdb);
2272dbd440d8SJani Nikula 	parse_driver_features(i915, bdb);
2273dbd440d8SJani Nikula 	parse_power_conservation_features(i915, bdb);
2274dbd440d8SJani Nikula 	parse_edp(i915, bdb);
2275dbd440d8SJani Nikula 	parse_psr(i915, bdb);
2276dbd440d8SJani Nikula 	parse_mipi_config(i915, bdb);
2277dbd440d8SJani Nikula 	parse_mipi_sequence(i915, bdb);
2278df0566a6SJani Nikula 
22796e0d46e9SJani Nikula 	/* Depends on child device list */
2280dbd440d8SJani Nikula 	parse_compression_parameters(i915, bdb);
22816e0d46e9SJani Nikula 
2282df0566a6SJani Nikula 	/* Further processing on pre-parsed data */
2283*ef0096e4SJani Nikula 	parse_sdvo_device_mapping(i915);
2284*ef0096e4SJani Nikula 	parse_ddi_ports(i915);
2285df0566a6SJani Nikula 
2286df0566a6SJani Nikula out:
2287df0566a6SJani Nikula 	if (!vbt) {
2288dbd440d8SJani Nikula 		drm_info(&i915->drm,
2289e92cbf38SWambui Karuga 			 "Failed to find VBIOS tables (VBT)\n");
2290dbd440d8SJani Nikula 		init_vbt_missing_defaults(i915);
2291df0566a6SJani Nikula 	}
2292df0566a6SJani Nikula 
22932cded152SLucas De Marchi 	kfree(oprom_vbt);
2294df0566a6SJani Nikula }
2295df0566a6SJani Nikula 
2296df0566a6SJani Nikula /**
229778dae1acSJanusz Krzysztofik  * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
2298dbd440d8SJani Nikula  * @i915: i915 device instance
2299df0566a6SJani Nikula  */
2300dbd440d8SJani Nikula void intel_bios_driver_remove(struct drm_i915_private *i915)
2301df0566a6SJani Nikula {
23020d9ef19bSJani Nikula 	struct display_device_data *devdata, *n;
23030d9ef19bSJani Nikula 
2304dbd440d8SJani Nikula 	list_for_each_entry_safe(devdata, n, &i915->vbt.display_devices, node) {
23050d9ef19bSJani Nikula 		list_del(&devdata->node);
23066e0d46e9SJani Nikula 		kfree(devdata->dsc);
23070d9ef19bSJani Nikula 		kfree(devdata);
23080d9ef19bSJani Nikula 	}
23090d9ef19bSJani Nikula 
2310dbd440d8SJani Nikula 	kfree(i915->vbt.sdvo_lvds_vbt_mode);
2311dbd440d8SJani Nikula 	i915->vbt.sdvo_lvds_vbt_mode = NULL;
2312dbd440d8SJani Nikula 	kfree(i915->vbt.lfp_lvds_vbt_mode);
2313dbd440d8SJani Nikula 	i915->vbt.lfp_lvds_vbt_mode = NULL;
2314dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.data);
2315dbd440d8SJani Nikula 	i915->vbt.dsi.data = NULL;
2316dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.pps);
2317dbd440d8SJani Nikula 	i915->vbt.dsi.pps = NULL;
2318dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.config);
2319dbd440d8SJani Nikula 	i915->vbt.dsi.config = NULL;
2320dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.deassert_seq);
2321dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq = NULL;
2322df0566a6SJani Nikula }
2323df0566a6SJani Nikula 
2324df0566a6SJani Nikula /**
2325df0566a6SJani Nikula  * intel_bios_is_tv_present - is integrated TV present in VBT
2326dbd440d8SJani Nikula  * @i915: i915 device instance
2327df0566a6SJani Nikula  *
2328df0566a6SJani Nikula  * Return true if TV is present. If no child devices were parsed from VBT,
2329df0566a6SJani Nikula  * assume TV is present.
2330df0566a6SJani Nikula  */
2331dbd440d8SJani Nikula bool intel_bios_is_tv_present(struct drm_i915_private *i915)
2332df0566a6SJani Nikula {
23330d9ef19bSJani Nikula 	const struct display_device_data *devdata;
2334df0566a6SJani Nikula 	const struct child_device_config *child;
2335df0566a6SJani Nikula 
2336dbd440d8SJani Nikula 	if (!i915->vbt.int_tv_support)
2337df0566a6SJani Nikula 		return false;
2338df0566a6SJani Nikula 
2339dbd440d8SJani Nikula 	if (list_empty(&i915->vbt.display_devices))
2340df0566a6SJani Nikula 		return true;
2341df0566a6SJani Nikula 
2342dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
23430d9ef19bSJani Nikula 		child = &devdata->child;
23440d9ef19bSJani Nikula 
2345df0566a6SJani Nikula 		/*
2346df0566a6SJani Nikula 		 * If the device type is not TV, continue.
2347df0566a6SJani Nikula 		 */
2348df0566a6SJani Nikula 		switch (child->device_type) {
2349df0566a6SJani Nikula 		case DEVICE_TYPE_INT_TV:
2350df0566a6SJani Nikula 		case DEVICE_TYPE_TV:
2351df0566a6SJani Nikula 		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
2352df0566a6SJani Nikula 			break;
2353df0566a6SJani Nikula 		default:
2354df0566a6SJani Nikula 			continue;
2355df0566a6SJani Nikula 		}
2356df0566a6SJani Nikula 		/* Only when the addin_offset is non-zero, it is regarded
2357df0566a6SJani Nikula 		 * as present.
2358df0566a6SJani Nikula 		 */
2359df0566a6SJani Nikula 		if (child->addin_offset)
2360df0566a6SJani Nikula 			return true;
2361df0566a6SJani Nikula 	}
2362df0566a6SJani Nikula 
2363df0566a6SJani Nikula 	return false;
2364df0566a6SJani Nikula }
2365df0566a6SJani Nikula 
2366df0566a6SJani Nikula /**
2367df0566a6SJani Nikula  * intel_bios_is_lvds_present - is LVDS present in VBT
2368dbd440d8SJani Nikula  * @i915:	i915 device instance
2369df0566a6SJani Nikula  * @i2c_pin:	i2c pin for LVDS if present
2370df0566a6SJani Nikula  *
2371df0566a6SJani Nikula  * Return true if LVDS is present. If no child devices were parsed from VBT,
2372df0566a6SJani Nikula  * assume LVDS is present.
2373df0566a6SJani Nikula  */
2374dbd440d8SJani Nikula bool intel_bios_is_lvds_present(struct drm_i915_private *i915, u8 *i2c_pin)
2375df0566a6SJani Nikula {
23760d9ef19bSJani Nikula 	const struct display_device_data *devdata;
2377df0566a6SJani Nikula 	const struct child_device_config *child;
2378df0566a6SJani Nikula 
2379dbd440d8SJani Nikula 	if (list_empty(&i915->vbt.display_devices))
2380df0566a6SJani Nikula 		return true;
2381df0566a6SJani Nikula 
2382dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
23830d9ef19bSJani Nikula 		child = &devdata->child;
2384df0566a6SJani Nikula 
2385df0566a6SJani Nikula 		/* If the device type is not LFP, continue.
2386df0566a6SJani Nikula 		 * We have to check both the new identifiers as well as the
2387df0566a6SJani Nikula 		 * old for compatibility with some BIOSes.
2388df0566a6SJani Nikula 		 */
2389df0566a6SJani Nikula 		if (child->device_type != DEVICE_TYPE_INT_LFP &&
2390df0566a6SJani Nikula 		    child->device_type != DEVICE_TYPE_LFP)
2391df0566a6SJani Nikula 			continue;
2392df0566a6SJani Nikula 
2393dbd440d8SJani Nikula 		if (intel_gmbus_is_valid_pin(i915, child->i2c_pin))
2394df0566a6SJani Nikula 			*i2c_pin = child->i2c_pin;
2395df0566a6SJani Nikula 
2396df0566a6SJani Nikula 		/* However, we cannot trust the BIOS writers to populate
2397df0566a6SJani Nikula 		 * the VBT correctly.  Since LVDS requires additional
2398df0566a6SJani Nikula 		 * information from AIM blocks, a non-zero addin offset is
2399df0566a6SJani Nikula 		 * a good indicator that the LVDS is actually present.
2400df0566a6SJani Nikula 		 */
2401df0566a6SJani Nikula 		if (child->addin_offset)
2402df0566a6SJani Nikula 			return true;
2403df0566a6SJani Nikula 
2404df0566a6SJani Nikula 		/* But even then some BIOS writers perform some black magic
2405df0566a6SJani Nikula 		 * and instantiate the device without reference to any
2406df0566a6SJani Nikula 		 * additional data.  Trust that if the VBT was written into
2407df0566a6SJani Nikula 		 * the OpRegion then they have validated the LVDS's existence.
2408df0566a6SJani Nikula 		 */
2409dbd440d8SJani Nikula 		if (i915->opregion.vbt)
2410df0566a6SJani Nikula 			return true;
2411df0566a6SJani Nikula 	}
2412df0566a6SJani Nikula 
2413df0566a6SJani Nikula 	return false;
2414df0566a6SJani Nikula }
2415df0566a6SJani Nikula 
2416df0566a6SJani Nikula /**
2417df0566a6SJani Nikula  * intel_bios_is_port_present - is the specified digital port present
2418dbd440d8SJani Nikula  * @i915:	i915 device instance
2419df0566a6SJani Nikula  * @port:	port to check
2420df0566a6SJani Nikula  *
2421df0566a6SJani Nikula  * Return true if the device in %port is present.
2422df0566a6SJani Nikula  */
2423dbd440d8SJani Nikula bool intel_bios_is_port_present(struct drm_i915_private *i915, enum port port)
2424df0566a6SJani Nikula {
24250d9ef19bSJani Nikula 	const struct display_device_data *devdata;
2426df0566a6SJani Nikula 	const struct child_device_config *child;
2427df0566a6SJani Nikula 	static const struct {
2428df0566a6SJani Nikula 		u16 dp, hdmi;
2429df0566a6SJani Nikula 	} port_mapping[] = {
2430df0566a6SJani Nikula 		[PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
2431df0566a6SJani Nikula 		[PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
2432df0566a6SJani Nikula 		[PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
2433df0566a6SJani Nikula 		[PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
2434df0566a6SJani Nikula 		[PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
2435df0566a6SJani Nikula 	};
2436df0566a6SJani Nikula 
2437dbd440d8SJani Nikula 	if (HAS_DDI(i915)) {
2438df0566a6SJani Nikula 		const struct ddi_vbt_port_info *port_info =
2439dbd440d8SJani Nikula 			&i915->vbt.ddi_port_info[port];
2440df0566a6SJani Nikula 
244185d8ec20SJani Nikula 		return port_info->child;
2442df0566a6SJani Nikula 	}
2443df0566a6SJani Nikula 
2444df0566a6SJani Nikula 	/* FIXME maybe deal with port A as well? */
2445dbd440d8SJani Nikula 	if (drm_WARN_ON(&i915->drm,
2446f4224a4cSPankaj Bharadiya 			port == PORT_A) || port >= ARRAY_SIZE(port_mapping))
2447df0566a6SJani Nikula 		return false;
2448df0566a6SJani Nikula 
2449dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
24500d9ef19bSJani Nikula 		child = &devdata->child;
2451df0566a6SJani Nikula 
2452df0566a6SJani Nikula 		if ((child->dvo_port == port_mapping[port].dp ||
2453df0566a6SJani Nikula 		     child->dvo_port == port_mapping[port].hdmi) &&
2454df0566a6SJani Nikula 		    (child->device_type & (DEVICE_TYPE_TMDS_DVI_SIGNALING |
2455df0566a6SJani Nikula 					   DEVICE_TYPE_DISPLAYPORT_OUTPUT)))
2456df0566a6SJani Nikula 			return true;
2457df0566a6SJani Nikula 	}
2458df0566a6SJani Nikula 
2459df0566a6SJani Nikula 	return false;
2460df0566a6SJani Nikula }
2461df0566a6SJani Nikula 
2462df0566a6SJani Nikula /**
2463df0566a6SJani Nikula  * intel_bios_is_port_edp - is the device in given port eDP
2464dbd440d8SJani Nikula  * @i915:	i915 device instance
2465df0566a6SJani Nikula  * @port:	port to check
2466df0566a6SJani Nikula  *
2467df0566a6SJani Nikula  * Return true if the device in %port is eDP.
2468df0566a6SJani Nikula  */
2469dbd440d8SJani Nikula bool intel_bios_is_port_edp(struct drm_i915_private *i915, enum port port)
2470df0566a6SJani Nikula {
24710d9ef19bSJani Nikula 	const struct display_device_data *devdata;
2472df0566a6SJani Nikula 	const struct child_device_config *child;
2473df0566a6SJani Nikula 	static const short port_mapping[] = {
2474df0566a6SJani Nikula 		[PORT_B] = DVO_PORT_DPB,
2475df0566a6SJani Nikula 		[PORT_C] = DVO_PORT_DPC,
2476df0566a6SJani Nikula 		[PORT_D] = DVO_PORT_DPD,
2477df0566a6SJani Nikula 		[PORT_E] = DVO_PORT_DPE,
2478df0566a6SJani Nikula 		[PORT_F] = DVO_PORT_DPF,
2479df0566a6SJani Nikula 	};
2480df0566a6SJani Nikula 
2481dbd440d8SJani Nikula 	if (HAS_DDI(i915))
2482dbd440d8SJani Nikula 		return i915->vbt.ddi_port_info[port].supports_edp;
2483df0566a6SJani Nikula 
2484dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
24850d9ef19bSJani Nikula 		child = &devdata->child;
2486df0566a6SJani Nikula 
2487df0566a6SJani Nikula 		if (child->dvo_port == port_mapping[port] &&
2488df0566a6SJani Nikula 		    (child->device_type & DEVICE_TYPE_eDP_BITS) ==
2489df0566a6SJani Nikula 		    (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
2490df0566a6SJani Nikula 			return true;
2491df0566a6SJani Nikula 	}
2492df0566a6SJani Nikula 
2493df0566a6SJani Nikula 	return false;
2494df0566a6SJani Nikula }
2495df0566a6SJani Nikula 
2496df0566a6SJani Nikula static bool child_dev_is_dp_dual_mode(const struct child_device_config *child,
2497df0566a6SJani Nikula 				      enum port port)
2498df0566a6SJani Nikula {
2499df0566a6SJani Nikula 	static const struct {
2500df0566a6SJani Nikula 		u16 dp, hdmi;
2501df0566a6SJani Nikula 	} port_mapping[] = {
2502df0566a6SJani Nikula 		/*
2503df0566a6SJani Nikula 		 * Buggy VBTs may declare DP ports as having
2504df0566a6SJani Nikula 		 * HDMI type dvo_port :( So let's check both.
2505df0566a6SJani Nikula 		 */
2506df0566a6SJani Nikula 		[PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
2507df0566a6SJani Nikula 		[PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
2508df0566a6SJani Nikula 		[PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
2509df0566a6SJani Nikula 		[PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
2510df0566a6SJani Nikula 		[PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
2511df0566a6SJani Nikula 	};
2512df0566a6SJani Nikula 
2513df0566a6SJani Nikula 	if (port == PORT_A || port >= ARRAY_SIZE(port_mapping))
2514df0566a6SJani Nikula 		return false;
2515df0566a6SJani Nikula 
2516df0566a6SJani Nikula 	if ((child->device_type & DEVICE_TYPE_DP_DUAL_MODE_BITS) !=
2517df0566a6SJani Nikula 	    (DEVICE_TYPE_DP_DUAL_MODE & DEVICE_TYPE_DP_DUAL_MODE_BITS))
2518df0566a6SJani Nikula 		return false;
2519df0566a6SJani Nikula 
2520df0566a6SJani Nikula 	if (child->dvo_port == port_mapping[port].dp)
2521df0566a6SJani Nikula 		return true;
2522df0566a6SJani Nikula 
2523df0566a6SJani Nikula 	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
2524df0566a6SJani Nikula 	if (child->dvo_port == port_mapping[port].hdmi &&
2525df0566a6SJani Nikula 	    child->aux_channel != 0)
2526df0566a6SJani Nikula 		return true;
2527df0566a6SJani Nikula 
2528df0566a6SJani Nikula 	return false;
2529df0566a6SJani Nikula }
2530df0566a6SJani Nikula 
2531dbd440d8SJani Nikula bool intel_bios_is_port_dp_dual_mode(struct drm_i915_private *i915,
2532df0566a6SJani Nikula 				     enum port port)
2533df0566a6SJani Nikula {
25340d9ef19bSJani Nikula 	const struct display_device_data *devdata;
2535df0566a6SJani Nikula 
2536dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
25370d9ef19bSJani Nikula 		if (child_dev_is_dp_dual_mode(&devdata->child, port))
2538df0566a6SJani Nikula 			return true;
2539df0566a6SJani Nikula 	}
2540df0566a6SJani Nikula 
2541df0566a6SJani Nikula 	return false;
2542df0566a6SJani Nikula }
2543df0566a6SJani Nikula 
2544df0566a6SJani Nikula /**
2545df0566a6SJani Nikula  * intel_bios_is_dsi_present - is DSI present in VBT
2546dbd440d8SJani Nikula  * @i915:	i915 device instance
2547df0566a6SJani Nikula  * @port:	port for DSI if present
2548df0566a6SJani Nikula  *
2549df0566a6SJani Nikula  * Return true if DSI is present, and return the port in %port.
2550df0566a6SJani Nikula  */
2551dbd440d8SJani Nikula bool intel_bios_is_dsi_present(struct drm_i915_private *i915,
2552df0566a6SJani Nikula 			       enum port *port)
2553df0566a6SJani Nikula {
25540d9ef19bSJani Nikula 	const struct display_device_data *devdata;
2555df0566a6SJani Nikula 	const struct child_device_config *child;
2556df0566a6SJani Nikula 	u8 dvo_port;
2557df0566a6SJani Nikula 
2558dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
25590d9ef19bSJani Nikula 		child = &devdata->child;
2560df0566a6SJani Nikula 
2561df0566a6SJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
2562df0566a6SJani Nikula 			continue;
2563df0566a6SJani Nikula 
2564df0566a6SJani Nikula 		dvo_port = child->dvo_port;
2565df0566a6SJani Nikula 
2566df0566a6SJani Nikula 		if (dvo_port == DVO_PORT_MIPIA ||
2567dbd440d8SJani Nikula 		    (dvo_port == DVO_PORT_MIPIB && INTEL_GEN(i915) >= 11) ||
2568dbd440d8SJani Nikula 		    (dvo_port == DVO_PORT_MIPIC && INTEL_GEN(i915) < 11)) {
2569df0566a6SJani Nikula 			if (port)
2570df0566a6SJani Nikula 				*port = dvo_port - DVO_PORT_MIPIA;
2571df0566a6SJani Nikula 			return true;
2572df0566a6SJani Nikula 		} else if (dvo_port == DVO_PORT_MIPIB ||
2573df0566a6SJani Nikula 			   dvo_port == DVO_PORT_MIPIC ||
2574df0566a6SJani Nikula 			   dvo_port == DVO_PORT_MIPID) {
2575dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
2576e92cbf38SWambui Karuga 				    "VBT has unsupported DSI port %c\n",
2577df0566a6SJani Nikula 				    port_name(dvo_port - DVO_PORT_MIPIA));
2578df0566a6SJani Nikula 		}
2579df0566a6SJani Nikula 	}
2580df0566a6SJani Nikula 
2581df0566a6SJani Nikula 	return false;
2582df0566a6SJani Nikula }
2583df0566a6SJani Nikula 
25841bf2f3bfSJani Nikula static void fill_dsc(struct intel_crtc_state *crtc_state,
25851bf2f3bfSJani Nikula 		     struct dsc_compression_parameters_entry *dsc,
25861bf2f3bfSJani Nikula 		     int dsc_max_bpc)
25871bf2f3bfSJani Nikula {
25881bf2f3bfSJani Nikula 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
25891bf2f3bfSJani Nikula 	int bpc = 8;
25901bf2f3bfSJani Nikula 
25911bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_major = dsc->version_major;
25921bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_minor = dsc->version_minor;
25931bf2f3bfSJani Nikula 
25941bf2f3bfSJani Nikula 	if (dsc->support_12bpc && dsc_max_bpc >= 12)
25951bf2f3bfSJani Nikula 		bpc = 12;
25961bf2f3bfSJani Nikula 	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
25971bf2f3bfSJani Nikula 		bpc = 10;
25981bf2f3bfSJani Nikula 	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
25991bf2f3bfSJani Nikula 		bpc = 8;
26001bf2f3bfSJani Nikula 	else
26011bf2f3bfSJani Nikula 		DRM_DEBUG_KMS("VBT: Unsupported BPC %d for DCS\n",
26021bf2f3bfSJani Nikula 			      dsc_max_bpc);
26031bf2f3bfSJani Nikula 
26041bf2f3bfSJani Nikula 	crtc_state->pipe_bpp = bpc * 3;
26051bf2f3bfSJani Nikula 
26061bf2f3bfSJani Nikula 	crtc_state->dsc.compressed_bpp = min(crtc_state->pipe_bpp,
26071bf2f3bfSJani Nikula 					     VBT_DSC_MAX_BPP(dsc->max_bpp));
26081bf2f3bfSJani Nikula 
26091bf2f3bfSJani Nikula 	/*
26101bf2f3bfSJani Nikula 	 * FIXME: This is ugly, and slice count should take DSC engine
26111bf2f3bfSJani Nikula 	 * throughput etc. into account.
26121bf2f3bfSJani Nikula 	 *
26131bf2f3bfSJani Nikula 	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
26141bf2f3bfSJani Nikula 	 */
26151bf2f3bfSJani Nikula 	if (dsc->slices_per_line & BIT(2)) {
26161bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 4;
26171bf2f3bfSJani Nikula 	} else if (dsc->slices_per_line & BIT(1)) {
26181bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 2;
26191bf2f3bfSJani Nikula 	} else {
26201bf2f3bfSJani Nikula 		/* FIXME */
26211bf2f3bfSJani Nikula 		if (!(dsc->slices_per_line & BIT(0)))
26221bf2f3bfSJani Nikula 			DRM_DEBUG_KMS("VBT: Unsupported DSC slice count for DSI\n");
26231bf2f3bfSJani Nikula 
26241bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 1;
26251bf2f3bfSJani Nikula 	}
26261bf2f3bfSJani Nikula 
26271bf2f3bfSJani Nikula 	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
26281bf2f3bfSJani Nikula 	    crtc_state->dsc.slice_count != 0)
26291bf2f3bfSJani Nikula 		DRM_DEBUG_KMS("VBT: DSC hdisplay %d not divisible by slice count %d\n",
26301bf2f3bfSJani Nikula 			      crtc_state->hw.adjusted_mode.crtc_hdisplay,
26311bf2f3bfSJani Nikula 			      crtc_state->dsc.slice_count);
26321bf2f3bfSJani Nikula 
26331bf2f3bfSJani Nikula 	/*
26341bf2f3bfSJani Nikula 	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
2635fd8a5b27SJani Nikula 	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63.
26361bf2f3bfSJani Nikula 	 */
2637fd8a5b27SJani Nikula 	vdsc_cfg->rc_model_size = drm_dsc_dp_rc_buffer_size(dsc->rc_buffer_block_size,
2638fd8a5b27SJani Nikula 							    dsc->rc_buffer_size);
26391bf2f3bfSJani Nikula 
26401bf2f3bfSJani Nikula 	/* FIXME: DSI spec says bpc + 1 for this one */
26411bf2f3bfSJani Nikula 	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
26421bf2f3bfSJani Nikula 
26431bf2f3bfSJani Nikula 	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
26441bf2f3bfSJani Nikula 
26451bf2f3bfSJani Nikula 	vdsc_cfg->slice_height = dsc->slice_height;
26461bf2f3bfSJani Nikula }
26471bf2f3bfSJani Nikula 
26481bf2f3bfSJani Nikula /* FIXME: initially DSI specific */
26491bf2f3bfSJani Nikula bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
26501bf2f3bfSJani Nikula 			       struct intel_crtc_state *crtc_state,
26511bf2f3bfSJani Nikula 			       int dsc_max_bpc)
26521bf2f3bfSJani Nikula {
26531bf2f3bfSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
26541bf2f3bfSJani Nikula 	const struct display_device_data *devdata;
26551bf2f3bfSJani Nikula 	const struct child_device_config *child;
26561bf2f3bfSJani Nikula 
26571bf2f3bfSJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
26581bf2f3bfSJani Nikula 		child = &devdata->child;
26591bf2f3bfSJani Nikula 
26601bf2f3bfSJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
26611bf2f3bfSJani Nikula 			continue;
26621bf2f3bfSJani Nikula 
26631bf2f3bfSJani Nikula 		if (child->dvo_port - DVO_PORT_MIPIA == encoder->port) {
26641bf2f3bfSJani Nikula 			if (!devdata->dsc)
26651bf2f3bfSJani Nikula 				return false;
26661bf2f3bfSJani Nikula 
26671bf2f3bfSJani Nikula 			if (crtc_state)
26681bf2f3bfSJani Nikula 				fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
26691bf2f3bfSJani Nikula 
26701bf2f3bfSJani Nikula 			return true;
26711bf2f3bfSJani Nikula 		}
26721bf2f3bfSJani Nikula 	}
26731bf2f3bfSJani Nikula 
26741bf2f3bfSJani Nikula 	return false;
26751bf2f3bfSJani Nikula }
26761bf2f3bfSJani Nikula 
2677df0566a6SJani Nikula /**
2678df0566a6SJani Nikula  * intel_bios_is_port_hpd_inverted - is HPD inverted for %port
2679df0566a6SJani Nikula  * @i915:	i915 device instance
2680df0566a6SJani Nikula  * @port:	port to check
2681df0566a6SJani Nikula  *
2682df0566a6SJani Nikula  * Return true if HPD should be inverted for %port.
2683df0566a6SJani Nikula  */
2684df0566a6SJani Nikula bool
2685df0566a6SJani Nikula intel_bios_is_port_hpd_inverted(const struct drm_i915_private *i915,
2686df0566a6SJani Nikula 				enum port port)
2687df0566a6SJani Nikula {
2688df0566a6SJani Nikula 	const struct child_device_config *child =
2689df0566a6SJani Nikula 		i915->vbt.ddi_port_info[port].child;
2690df0566a6SJani Nikula 
2691f4224a4cSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&i915->drm, !IS_GEN9_LP(i915)))
2692df0566a6SJani Nikula 		return false;
2693df0566a6SJani Nikula 
2694df0566a6SJani Nikula 	return child && child->hpd_invert;
2695df0566a6SJani Nikula }
2696df0566a6SJani Nikula 
2697df0566a6SJani Nikula /**
2698df0566a6SJani Nikula  * intel_bios_is_lspcon_present - if LSPCON is attached on %port
2699df0566a6SJani Nikula  * @i915:	i915 device instance
2700df0566a6SJani Nikula  * @port:	port to check
2701df0566a6SJani Nikula  *
2702df0566a6SJani Nikula  * Return true if LSPCON is present on this port
2703df0566a6SJani Nikula  */
2704df0566a6SJani Nikula bool
2705df0566a6SJani Nikula intel_bios_is_lspcon_present(const struct drm_i915_private *i915,
2706df0566a6SJani Nikula 			     enum port port)
2707df0566a6SJani Nikula {
2708df0566a6SJani Nikula 	const struct child_device_config *child =
2709df0566a6SJani Nikula 		i915->vbt.ddi_port_info[port].child;
2710df0566a6SJani Nikula 
2711df0566a6SJani Nikula 	return HAS_LSPCON(i915) && child && child->lspcon;
2712df0566a6SJani Nikula }
2713df0566a6SJani Nikula 
2714aaab24bbSUma Shankar /**
2715aaab24bbSUma Shankar  * intel_bios_is_lane_reversal_needed - if lane reversal needed on port
2716aaab24bbSUma Shankar  * @i915:       i915 device instance
2717aaab24bbSUma Shankar  * @port:       port to check
2718aaab24bbSUma Shankar  *
2719aaab24bbSUma Shankar  * Return true if port requires lane reversal
2720aaab24bbSUma Shankar  */
2721aaab24bbSUma Shankar bool
2722aaab24bbSUma Shankar intel_bios_is_lane_reversal_needed(const struct drm_i915_private *i915,
2723aaab24bbSUma Shankar 				   enum port port)
2724aaab24bbSUma Shankar {
2725aaab24bbSUma Shankar 	const struct child_device_config *child =
2726aaab24bbSUma Shankar 		i915->vbt.ddi_port_info[port].child;
2727aaab24bbSUma Shankar 
2728aaab24bbSUma Shankar 	return child && child->lane_reversal;
2729aaab24bbSUma Shankar }
2730aaab24bbSUma Shankar 
2731dbd440d8SJani Nikula enum aux_ch intel_bios_port_aux_ch(struct drm_i915_private *i915,
2732df0566a6SJani Nikula 				   enum port port)
2733df0566a6SJani Nikula {
2734df0566a6SJani Nikula 	const struct ddi_vbt_port_info *info =
2735dbd440d8SJani Nikula 		&i915->vbt.ddi_port_info[port];
2736df0566a6SJani Nikula 	enum aux_ch aux_ch;
2737df0566a6SJani Nikula 
2738df0566a6SJani Nikula 	if (!info->alternate_aux_channel) {
2739df0566a6SJani Nikula 		aux_ch = (enum aux_ch)port;
2740df0566a6SJani Nikula 
2741dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2742e92cbf38SWambui Karuga 			    "using AUX %c for port %c (platform default)\n",
2743df0566a6SJani Nikula 			    aux_ch_name(aux_ch), port_name(port));
2744df0566a6SJani Nikula 		return aux_ch;
2745df0566a6SJani Nikula 	}
2746df0566a6SJani Nikula 
274718c283dfSAditya Swarup 	/*
274818c283dfSAditya Swarup 	 * RKL/DG1 VBT uses PHY based mapping. Combo PHYs A,B,C,D
274918c283dfSAditya Swarup 	 * map to DDI A,B,TC1,TC2 respectively.
275018c283dfSAditya Swarup 	 *
275118c283dfSAditya Swarup 	 * ADL-S VBT uses PHY based mapping. Combo PHYs A,B,C,D,E
275218c283dfSAditya Swarup 	 * map to DDI A,TC1,TC2,TC3,TC4 respectively.
275318c283dfSAditya Swarup 	 */
2754df0566a6SJani Nikula 	switch (info->alternate_aux_channel) {
2755df0566a6SJani Nikula 	case DP_AUX_A:
2756df0566a6SJani Nikula 		aux_ch = AUX_CH_A;
2757df0566a6SJani Nikula 		break;
2758df0566a6SJani Nikula 	case DP_AUX_B:
2759dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
276018c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC1;
276118c283dfSAditya Swarup 		else
2762df0566a6SJani Nikula 			aux_ch = AUX_CH_B;
2763df0566a6SJani Nikula 		break;
2764df0566a6SJani Nikula 	case DP_AUX_C:
2765dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
276618c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC2;
2767dbd440d8SJani Nikula 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
276818c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC1;
276918c283dfSAditya Swarup 		else
277018c283dfSAditya Swarup 			aux_ch = AUX_CH_C;
2771df0566a6SJani Nikula 		break;
2772df0566a6SJani Nikula 	case DP_AUX_D:
2773dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
277418c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC3;
2775dbd440d8SJani Nikula 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
277618c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC2;
277718c283dfSAditya Swarup 		else
277818c283dfSAditya Swarup 			aux_ch = AUX_CH_D;
2779df0566a6SJani Nikula 		break;
2780df0566a6SJani Nikula 	case DP_AUX_E:
2781dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
278218c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC4;
278318c283dfSAditya Swarup 		else
2784df0566a6SJani Nikula 			aux_ch = AUX_CH_E;
2785df0566a6SJani Nikula 		break;
2786df0566a6SJani Nikula 	case DP_AUX_F:
2787df0566a6SJani Nikula 		aux_ch = AUX_CH_F;
2788df0566a6SJani Nikula 		break;
2789eb8de23cSKhaled Almahallawy 	case DP_AUX_G:
2790eb8de23cSKhaled Almahallawy 		aux_ch = AUX_CH_G;
2791eb8de23cSKhaled Almahallawy 		break;
27925bf22ee4SVille Syrjälä 	case DP_AUX_H:
27935bf22ee4SVille Syrjälä 		aux_ch = AUX_CH_H;
27945bf22ee4SVille Syrjälä 		break;
27955bf22ee4SVille Syrjälä 	case DP_AUX_I:
27965bf22ee4SVille Syrjälä 		aux_ch = AUX_CH_I;
27975bf22ee4SVille Syrjälä 		break;
2798df0566a6SJani Nikula 	default:
2799df0566a6SJani Nikula 		MISSING_CASE(info->alternate_aux_channel);
2800df0566a6SJani Nikula 		aux_ch = AUX_CH_A;
2801df0566a6SJani Nikula 		break;
2802df0566a6SJani Nikula 	}
2803df0566a6SJani Nikula 
2804dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "using AUX %c for port %c (VBT)\n",
2805df0566a6SJani Nikula 		    aux_ch_name(aux_ch), port_name(port));
2806df0566a6SJani Nikula 
2807df0566a6SJani Nikula 	return aux_ch;
2808df0566a6SJani Nikula }
2809d9ee2111SJani Nikula 
2810d9ee2111SJani Nikula int intel_bios_max_tmds_clock(struct intel_encoder *encoder)
2811d9ee2111SJani Nikula {
2812d9ee2111SJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2813d9ee2111SJani Nikula 
2814d9ee2111SJani Nikula 	return i915->vbt.ddi_port_info[encoder->port].max_tmds_clock;
2815d9ee2111SJani Nikula }
28160aed3bdeSJani Nikula 
28170aed3bdeSJani Nikula int intel_bios_hdmi_level_shift(struct intel_encoder *encoder)
28180aed3bdeSJani Nikula {
28190aed3bdeSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
28200aed3bdeSJani Nikula 	const struct ddi_vbt_port_info *info =
28210aed3bdeSJani Nikula 		&i915->vbt.ddi_port_info[encoder->port];
28220aed3bdeSJani Nikula 
28230aed3bdeSJani Nikula 	return info->hdmi_level_shift_set ? info->hdmi_level_shift : -1;
28240aed3bdeSJani Nikula }
2825605a1872SJani Nikula 
2826605a1872SJani Nikula int intel_bios_dp_boost_level(struct intel_encoder *encoder)
2827605a1872SJani Nikula {
2828605a1872SJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2829605a1872SJani Nikula 
2830605a1872SJani Nikula 	return i915->vbt.ddi_port_info[encoder->port].dp_boost_level;
2831605a1872SJani Nikula }
283201a60883SJani Nikula 
283301a60883SJani Nikula int intel_bios_hdmi_boost_level(struct intel_encoder *encoder)
283401a60883SJani Nikula {
283501a60883SJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
283601a60883SJani Nikula 
283701a60883SJani Nikula 	return i915->vbt.ddi_port_info[encoder->port].hdmi_boost_level;
283801a60883SJani Nikula }
2839f83acdabSJani Nikula 
2840f83acdabSJani Nikula int intel_bios_dp_max_link_rate(struct intel_encoder *encoder)
2841f83acdabSJani Nikula {
2842f83acdabSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
2843f83acdabSJani Nikula 
2844f83acdabSJani Nikula 	return i915->vbt.ddi_port_info[encoder->port].dp_max_link_rate;
2845f83acdabSJani Nikula }
284617004bfbSJani Nikula 
284717004bfbSJani Nikula int intel_bios_alternate_ddc_pin(struct intel_encoder *encoder)
284817004bfbSJani Nikula {
284917004bfbSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
285017004bfbSJani Nikula 
285117004bfbSJani Nikula 	return i915->vbt.ddi_port_info[encoder->port].alternate_ddc_pin;
285217004bfbSJani Nikula }
2853c5faae5aSJani Nikula 
2854c5faae5aSJani Nikula bool intel_bios_port_supports_dvi(struct drm_i915_private *i915, enum port port)
2855c5faae5aSJani Nikula {
2856c5faae5aSJani Nikula 	return i915->vbt.ddi_port_info[port].supports_dvi;
2857c5faae5aSJani Nikula }
2858c5faae5aSJani Nikula 
2859c5faae5aSJani Nikula bool intel_bios_port_supports_hdmi(struct drm_i915_private *i915, enum port port)
2860c5faae5aSJani Nikula {
2861c5faae5aSJani Nikula 	return i915->vbt.ddi_port_info[port].supports_hdmi;
2862c5faae5aSJani Nikula }
2863c5faae5aSJani Nikula 
2864c5faae5aSJani Nikula bool intel_bios_port_supports_dp(struct drm_i915_private *i915, enum port port)
2865c5faae5aSJani Nikula {
2866c5faae5aSJani Nikula 	return i915->vbt.ddi_port_info[port].supports_dp;
2867c5faae5aSJani Nikula }
2868c5faae5aSJani Nikula 
2869c5faae5aSJani Nikula bool intel_bios_port_supports_typec_usb(struct drm_i915_private *i915,
2870c5faae5aSJani Nikula 					enum port port)
2871c5faae5aSJani Nikula {
2872c5faae5aSJani Nikula 	return i915->vbt.ddi_port_info[port].supports_typec_usb;
2873c5faae5aSJani Nikula }
2874c5faae5aSJani Nikula 
2875c5faae5aSJani Nikula bool intel_bios_port_supports_tbt(struct drm_i915_private *i915, enum port port)
2876c5faae5aSJani Nikula {
2877c5faae5aSJani Nikula 	return i915->vbt.ddi_port_info[port].supports_tbt;
2878c5faae5aSJani Nikula }
2879