xref: /linux/drivers/gpu/drm/i915/display/intel_dp_aux_backlight.c (revision 66e72a01b60ae6950ddbb3585fdc1424d303e14b)
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 /*
26  * Laptops with Intel GPUs which have panels that support controlling the
27  * backlight through DP AUX can actually use two different interfaces: Intel's
28  * proprietary DP AUX backlight interface, and the standard VESA backlight
29  * interface. Unfortunately, at the time of writing this a lot of laptops will
30  * advertise support for the standard VESA backlight interface when they
31  * don't properly support it. However, on these systems the Intel backlight
32  * interface generally does work properly. Additionally, these systems will
33  * usually just indicate that they use PWM backlight controls in their VBIOS
34  * for some reason.
35  */
36 
37 #include "i915_drv.h"
38 #include "intel_backlight.h"
39 #include "intel_display_types.h"
40 #include "intel_dp.h"
41 #include "intel_dp_aux_backlight.h"
42 
43 /*
44  * DP AUX registers for Intel's proprietary HDR backlight interface. We define
45  * them here since we'll likely be the only driver to ever use these.
46  */
47 #define INTEL_EDP_HDR_TCON_CAP0                                        0x340
48 
49 #define INTEL_EDP_HDR_TCON_CAP1                                        0x341
50 # define INTEL_EDP_HDR_TCON_2084_DECODE_CAP                           BIT(0)
51 # define INTEL_EDP_HDR_TCON_2020_GAMUT_CAP                            BIT(1)
52 # define INTEL_EDP_HDR_TCON_TONE_MAPPING_CAP                          BIT(2)
53 # define INTEL_EDP_HDR_TCON_SEGMENTED_BACKLIGHT_CAP                   BIT(3)
54 # define INTEL_EDP_HDR_TCON_BRIGHTNESS_NITS_CAP                       BIT(4)
55 # define INTEL_EDP_HDR_TCON_OPTIMIZATION_CAP                          BIT(5)
56 # define INTEL_EDP_HDR_TCON_SDP_COLORIMETRY_CAP                       BIT(6)
57 # define INTEL_EDP_HDR_TCON_SRGB_TO_PANEL_GAMUT_CONVERSION_CAP        BIT(7)
58 
59 #define INTEL_EDP_HDR_TCON_CAP2                                        0x342
60 # define INTEL_EDP_SDR_TCON_BRIGHTNESS_AUX_CAP                        BIT(0)
61 
62 #define INTEL_EDP_HDR_TCON_CAP3                                        0x343
63 
64 #define INTEL_EDP_HDR_GETSET_CTRL_PARAMS                               0x344
65 # define INTEL_EDP_HDR_TCON_2084_DECODE_ENABLE                        BIT(0)
66 # define INTEL_EDP_HDR_TCON_2020_GAMUT_ENABLE                         BIT(1)
67 # define INTEL_EDP_HDR_TCON_TONE_MAPPING_ENABLE                       BIT(2)
68 # define INTEL_EDP_HDR_TCON_SEGMENTED_BACKLIGHT_ENABLE                BIT(3)
69 # define INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE                     BIT(4)
70 # define INTEL_EDP_HDR_TCON_SRGB_TO_PANEL_GAMUT_ENABLE                BIT(5)
71 /* Bit 6 is reserved */
72 # define INTEL_EDP_HDR_TCON_SDP_OVERRIDE_AUX			      BIT(7)
73 
74 #define INTEL_EDP_HDR_CONTENT_LUMINANCE                                0x346
75 #define INTEL_EDP_HDR_PANEL_LUMINANCE_OVERRIDE                         0x34A
76 #define INTEL_EDP_SDR_LUMINANCE_LEVEL                                  0x352
77 #define INTEL_EDP_BRIGHTNESS_NITS_LSB                                  0x354
78 #define INTEL_EDP_BRIGHTNESS_NITS_MSB                                  0x355
79 #define INTEL_EDP_BRIGHTNESS_DELAY_FRAMES                              0x356
80 #define INTEL_EDP_BRIGHTNESS_PER_FRAME_STEPS                           0x357
81 
82 #define INTEL_EDP_BRIGHTNESS_OPTIMIZATION_0                            0x358
83 # define INTEL_EDP_TCON_USAGE_MASK                             GENMASK(0, 3)
84 # define INTEL_EDP_TCON_USAGE_UNKNOWN                                    0x0
85 # define INTEL_EDP_TCON_USAGE_DESKTOP                                    0x1
86 # define INTEL_EDP_TCON_USAGE_FULL_SCREEN_MEDIA                          0x2
87 # define INTEL_EDP_TCON_USAGE_FULL_SCREEN_GAMING                         0x3
88 # define INTEL_EDP_TCON_POWER_MASK                                    BIT(4)
89 # define INTEL_EDP_TCON_POWER_DC                                    (0 << 4)
90 # define INTEL_EDP_TCON_POWER_AC                                    (1 << 4)
91 # define INTEL_EDP_TCON_OPTIMIZATION_STRENGTH_MASK             GENMASK(5, 7)
92 
93 #define INTEL_EDP_BRIGHTNESS_OPTIMIZATION_1                            0x359
94 
95 enum intel_dp_aux_backlight_modparam {
96 	INTEL_DP_AUX_BACKLIGHT_AUTO = -1,
97 	INTEL_DP_AUX_BACKLIGHT_OFF = 0,
98 	INTEL_DP_AUX_BACKLIGHT_ON = 1,
99 	INTEL_DP_AUX_BACKLIGHT_FORCE_VESA = 2,
100 	INTEL_DP_AUX_BACKLIGHT_FORCE_INTEL = 3,
101 };
102 
103 static bool is_intel_tcon_cap(const u8 tcon_cap[4])
104 {
105 	return tcon_cap[0] >= 1;
106 }
107 
108 /* Intel EDP backlight callbacks */
109 static bool
110 intel_dp_aux_supports_hdr_backlight(struct intel_connector *connector)
111 {
112 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
113 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
114 	struct drm_dp_aux *aux = &intel_dp->aux;
115 	struct intel_panel *panel = &connector->panel;
116 	int ret;
117 	u8 tcon_cap[4];
118 
119 	intel_dp_wait_source_oui(intel_dp);
120 
121 	ret = drm_dp_dpcd_read(aux, INTEL_EDP_HDR_TCON_CAP0, tcon_cap, sizeof(tcon_cap));
122 	if (ret != sizeof(tcon_cap))
123 		return false;
124 
125 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] Detected %s HDR backlight interface version %d\n",
126 		    connector->base.base.id, connector->base.name,
127 		    is_intel_tcon_cap(tcon_cap) ? "Intel" : "unsupported", tcon_cap[0]);
128 
129 	if (!is_intel_tcon_cap(tcon_cap))
130 		return false;
131 
132 	if (!(tcon_cap[1] & INTEL_EDP_HDR_TCON_BRIGHTNESS_NITS_CAP))
133 		return false;
134 
135 	/*
136 	 * If we don't have HDR static metadata there is no way to
137 	 * runtime detect used range for nits based control. For now
138 	 * do not use Intel proprietary eDP backlight control if we
139 	 * don't have this data in panel EDID. In case we find panel
140 	 * which supports only nits based control, but doesn't provide
141 	 * HDR static metadata we need to start maintaining table of
142 	 * ranges for such panels.
143 	 */
144 	if (i915->display.params.enable_dpcd_backlight != INTEL_DP_AUX_BACKLIGHT_FORCE_INTEL &&
145 	    !(connector->base.hdr_sink_metadata.hdmi_type1.metadata_type &
146 	      BIT(HDMI_STATIC_METADATA_TYPE1))) {
147 		drm_info(&i915->drm,
148 			 "[CONNECTOR:%d:%s] Panel is missing HDR static metadata. Possible support for Intel HDR backlight interface is not used. If your backlight controls don't work try booting with i915.enable_dpcd_backlight=%d. needs this, please file a _new_ bug report on drm/i915, see " FDO_BUG_URL " for details.\n",
149 			 connector->base.base.id, connector->base.name,
150 			 INTEL_DP_AUX_BACKLIGHT_FORCE_INTEL);
151 		return false;
152 	}
153 
154 	panel->backlight.edp.intel_cap.sdr_uses_aux =
155 		tcon_cap[2] & INTEL_EDP_SDR_TCON_BRIGHTNESS_AUX_CAP;
156 	panel->backlight.edp.intel_cap.supports_2084_decode =
157 		tcon_cap[1] & INTEL_EDP_HDR_TCON_2084_DECODE_CAP;
158 	panel->backlight.edp.intel_cap.supports_2020_gamut =
159 		tcon_cap[1] & INTEL_EDP_HDR_TCON_2020_GAMUT_CAP;
160 	panel->backlight.edp.intel_cap.supports_segmented_backlight =
161 		tcon_cap[1] & INTEL_EDP_HDR_TCON_SEGMENTED_BACKLIGHT_CAP;
162 	panel->backlight.edp.intel_cap.supports_sdp_colorimetry =
163 		tcon_cap[1] & INTEL_EDP_HDR_TCON_SDP_COLORIMETRY_CAP;
164 	panel->backlight.edp.intel_cap.supports_tone_mapping =
165 		tcon_cap[1] & INTEL_EDP_HDR_TCON_TONE_MAPPING_CAP;
166 
167 	return true;
168 }
169 
170 static u32
171 intel_dp_aux_hdr_get_backlight(struct intel_connector *connector, enum pipe pipe)
172 {
173 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
174 	struct intel_panel *panel = &connector->panel;
175 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
176 	u8 tmp;
177 	u8 buf[2] = {};
178 
179 	if (drm_dp_dpcd_readb(&intel_dp->aux, INTEL_EDP_HDR_GETSET_CTRL_PARAMS, &tmp) != 1) {
180 		drm_err(&i915->drm, "[CONNECTOR:%d:%s] Failed to read current backlight mode from DPCD\n",
181 			connector->base.base.id, connector->base.name);
182 		return 0;
183 	}
184 
185 	if (!(tmp & INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE)) {
186 		if (!panel->backlight.edp.intel_cap.sdr_uses_aux) {
187 			u32 pwm_level = panel->backlight.pwm_funcs->get(connector, pipe);
188 
189 			return intel_backlight_level_from_pwm(connector, pwm_level);
190 		}
191 
192 		/* Assume 100% brightness if backlight controls aren't enabled yet */
193 		return panel->backlight.max;
194 	}
195 
196 	if (drm_dp_dpcd_read(&intel_dp->aux, INTEL_EDP_BRIGHTNESS_NITS_LSB, buf,
197 			     sizeof(buf)) != sizeof(buf)) {
198 		drm_err(&i915->drm, "[CONNECTOR:%d:%s] Failed to read brightness from DPCD\n",
199 			connector->base.base.id, connector->base.name);
200 		return 0;
201 	}
202 
203 	return (buf[1] << 8 | buf[0]);
204 }
205 
206 static void
207 intel_dp_aux_hdr_set_aux_backlight(const struct drm_connector_state *conn_state, u32 level)
208 {
209 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
210 	struct drm_device *dev = connector->base.dev;
211 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
212 	u8 buf[4] = {};
213 
214 	buf[0] = level & 0xFF;
215 	buf[1] = (level & 0xFF00) >> 8;
216 
217 	if (drm_dp_dpcd_write(&intel_dp->aux, INTEL_EDP_BRIGHTNESS_NITS_LSB, buf,
218 			      sizeof(buf)) != sizeof(buf))
219 		drm_err(dev, "[CONNECTOR:%d:%s] Failed to write brightness level to DPCD\n",
220 			connector->base.base.id, connector->base.name);
221 }
222 
223 static bool
224 intel_dp_in_hdr_mode(const struct drm_connector_state *conn_state)
225 {
226 	struct hdr_output_metadata *hdr_metadata;
227 
228 	if (!conn_state->hdr_output_metadata)
229 		return false;
230 
231 	hdr_metadata = conn_state->hdr_output_metadata->data;
232 
233 	return hdr_metadata->hdmi_metadata_type1.eotf == HDMI_EOTF_SMPTE_ST2084;
234 }
235 
236 static void
237 intel_dp_aux_hdr_set_backlight(const struct drm_connector_state *conn_state, u32 level)
238 {
239 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
240 	struct intel_panel *panel = &connector->panel;
241 
242 	if (intel_dp_in_hdr_mode(conn_state) ||
243 	    panel->backlight.edp.intel_cap.sdr_uses_aux) {
244 		intel_dp_aux_hdr_set_aux_backlight(conn_state, level);
245 	} else {
246 		const u32 pwm_level = intel_backlight_level_to_pwm(connector, level);
247 
248 		intel_backlight_set_pwm_level(conn_state, pwm_level);
249 	}
250 }
251 
252 static void
253 intel_dp_aux_write_content_luminance(struct intel_connector *connector,
254 				     struct hdr_output_metadata *hdr_metadata)
255 {
256 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
257 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
258 	int ret;
259 	u8 buf[4];
260 
261 	if (!intel_dp_has_gamut_metadata_dip(connector->encoder))
262 		return;
263 
264 	buf[0] = hdr_metadata->hdmi_metadata_type1.max_cll & 0xFF;
265 	buf[1] = (hdr_metadata->hdmi_metadata_type1.max_cll & 0xFF00) >> 8;
266 	buf[2] = hdr_metadata->hdmi_metadata_type1.max_fall & 0xFF;
267 	buf[3] = (hdr_metadata->hdmi_metadata_type1.max_fall & 0xFF00) >> 8;
268 
269 	ret = drm_dp_dpcd_write(&intel_dp->aux,
270 				INTEL_EDP_HDR_CONTENT_LUMINANCE,
271 				buf, sizeof(buf));
272 	if (ret < 0)
273 		drm_dbg_kms(&i915->drm,
274 			    "Content Luminance DPCD reg write failed, err:-%d\n",
275 			    ret);
276 }
277 
278 static void
279 intel_dp_aux_fill_hdr_tcon_params(const struct drm_connector_state *conn_state, u8 *ctrl)
280 {
281 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
282 	struct intel_panel *panel = &connector->panel;
283 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
284 
285 	/*
286 	 * According to spec segmented backlight needs to be set whenever panel is in
287 	 * HDR mode.
288 	 */
289 	if (intel_dp_in_hdr_mode(conn_state)) {
290 		*ctrl |= INTEL_EDP_HDR_TCON_SEGMENTED_BACKLIGHT_ENABLE;
291 		*ctrl |= INTEL_EDP_HDR_TCON_2084_DECODE_ENABLE;
292 	}
293 
294 	if (DISPLAY_VER(i915) < 11)
295 		*ctrl &= ~INTEL_EDP_HDR_TCON_TONE_MAPPING_ENABLE;
296 
297 	if (panel->backlight.edp.intel_cap.supports_2020_gamut &&
298 	    (conn_state->colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB ||
299 	     conn_state->colorspace == DRM_MODE_COLORIMETRY_BT2020_YCC ||
300 	     conn_state->colorspace == DRM_MODE_COLORIMETRY_BT2020_CYCC))
301 		*ctrl |= INTEL_EDP_HDR_TCON_2020_GAMUT_ENABLE;
302 
303 	if (panel->backlight.edp.intel_cap.supports_sdp_colorimetry &&
304 	    intel_dp_has_gamut_metadata_dip(connector->encoder))
305 		*ctrl |= INTEL_EDP_HDR_TCON_SDP_OVERRIDE_AUX;
306 	else
307 		*ctrl &= ~INTEL_EDP_HDR_TCON_SDP_OVERRIDE_AUX;
308 }
309 
310 static void
311 intel_dp_aux_hdr_enable_backlight(const struct intel_crtc_state *crtc_state,
312 				  const struct drm_connector_state *conn_state, u32 level)
313 {
314 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
315 	struct intel_panel *panel = &connector->panel;
316 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
317 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
318 	struct hdr_output_metadata *hdr_metadata;
319 	int ret;
320 	u8 old_ctrl, ctrl;
321 
322 	intel_dp_wait_source_oui(intel_dp);
323 
324 	ret = drm_dp_dpcd_readb(&intel_dp->aux, INTEL_EDP_HDR_GETSET_CTRL_PARAMS, &old_ctrl);
325 	if (ret != 1) {
326 		drm_err(&i915->drm, "[CONNECTOR:%d:%s] Failed to read current backlight control mode: %d\n",
327 			connector->base.base.id, connector->base.name, ret);
328 		return;
329 	}
330 
331 	ctrl = old_ctrl;
332 	if (intel_dp_in_hdr_mode(conn_state) ||
333 	    panel->backlight.edp.intel_cap.sdr_uses_aux) {
334 		ctrl |= INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE;
335 
336 		intel_dp_aux_hdr_set_aux_backlight(conn_state, level);
337 	} else {
338 		u32 pwm_level = intel_backlight_level_to_pwm(connector, level);
339 
340 		panel->backlight.pwm_funcs->enable(crtc_state, conn_state, pwm_level);
341 
342 		ctrl &= ~INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE;
343 	}
344 
345 	intel_dp_aux_fill_hdr_tcon_params(conn_state, &ctrl);
346 
347 	if (ctrl != old_ctrl &&
348 	    drm_dp_dpcd_writeb(&intel_dp->aux, INTEL_EDP_HDR_GETSET_CTRL_PARAMS, ctrl) != 1)
349 		drm_err(&i915->drm, "[CONNECTOR:%d:%s] Failed to configure DPCD brightness controls\n",
350 			connector->base.base.id, connector->base.name);
351 
352 	if (intel_dp_in_hdr_mode(conn_state)) {
353 		hdr_metadata = conn_state->hdr_output_metadata->data;
354 		intel_dp_aux_write_content_luminance(connector, hdr_metadata);
355 	}
356 }
357 
358 static void
359 intel_dp_aux_hdr_disable_backlight(const struct drm_connector_state *conn_state, u32 level)
360 {
361 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
362 	struct intel_panel *panel = &connector->panel;
363 
364 	/* Nothing to do for AUX based backlight controls */
365 	if (panel->backlight.edp.intel_cap.sdr_uses_aux)
366 		return;
367 
368 	/* Note we want the actual pwm_level to be 0, regardless of pwm_min */
369 	panel->backlight.pwm_funcs->disable(conn_state, intel_backlight_invert_pwm_level(connector, 0));
370 }
371 
372 static const char *dpcd_vs_pwm_str(bool aux)
373 {
374 	return aux ? "DPCD" : "PWM";
375 }
376 
377 static void
378 intel_dp_aux_write_panel_luminance_override(struct intel_connector *connector)
379 {
380 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
381 	struct intel_panel *panel = &connector->panel;
382 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
383 	int ret;
384 	u8 buf[4] = {};
385 
386 	buf[0] = panel->backlight.min & 0xFF;
387 	buf[1] = (panel->backlight.min & 0xFF00) >> 8;
388 	buf[2] = panel->backlight.max & 0xFF;
389 	buf[3] = (panel->backlight.max & 0xFF00) >> 8;
390 
391 	ret = drm_dp_dpcd_write(&intel_dp->aux,
392 				INTEL_EDP_HDR_PANEL_LUMINANCE_OVERRIDE,
393 				buf, sizeof(buf));
394 	if (ret < 0)
395 		drm_dbg_kms(&i915->drm,
396 			    "Panel Luminance DPCD reg write failed, err:-%d\n",
397 			    ret);
398 }
399 
400 static int
401 intel_dp_aux_hdr_setup_backlight(struct intel_connector *connector, enum pipe pipe)
402 {
403 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
404 	struct intel_panel *panel = &connector->panel;
405 	struct drm_luminance_range_info *luminance_range =
406 		&connector->base.display_info.luminance_range;
407 	int ret;
408 
409 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] SDR backlight is controlled through %s\n",
410 		    connector->base.base.id, connector->base.name,
411 		    dpcd_vs_pwm_str(panel->backlight.edp.intel_cap.sdr_uses_aux));
412 
413 	if (!panel->backlight.edp.intel_cap.sdr_uses_aux) {
414 		ret = panel->backlight.pwm_funcs->setup(connector, pipe);
415 		if (ret < 0) {
416 			drm_err(&i915->drm,
417 				"[CONNECTOR:%d:%s] Failed to setup SDR backlight controls through PWM: %d\n",
418 				connector->base.base.id, connector->base.name, ret);
419 			return ret;
420 		}
421 	}
422 
423 	if (luminance_range->max_luminance) {
424 		panel->backlight.max = luminance_range->max_luminance;
425 		panel->backlight.min = luminance_range->min_luminance;
426 	} else {
427 		panel->backlight.max = 512;
428 		panel->backlight.min = 0;
429 	}
430 
431 	intel_dp_aux_write_panel_luminance_override(connector);
432 
433 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] Using AUX HDR interface for backlight control (range %d..%d)\n",
434 		    connector->base.base.id, connector->base.name,
435 		    panel->backlight.min, panel->backlight.max);
436 
437 	panel->backlight.level = intel_dp_aux_hdr_get_backlight(connector, pipe);
438 	panel->backlight.enabled = panel->backlight.level != 0;
439 
440 	return 0;
441 }
442 
443 /* VESA backlight callbacks */
444 static u32 intel_dp_aux_vesa_get_backlight(struct intel_connector *connector, enum pipe unused)
445 {
446 	return connector->panel.backlight.level;
447 }
448 
449 static void
450 intel_dp_aux_vesa_set_backlight(const struct drm_connector_state *conn_state, u32 level)
451 {
452 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
453 	struct intel_panel *panel = &connector->panel;
454 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
455 
456 	if (!panel->backlight.edp.vesa.info.aux_set) {
457 		const u32 pwm_level = intel_backlight_level_to_pwm(connector, level);
458 
459 		intel_backlight_set_pwm_level(conn_state, pwm_level);
460 	}
461 
462 	drm_edp_backlight_set_level(&intel_dp->aux, &panel->backlight.edp.vesa.info, level);
463 }
464 
465 static void
466 intel_dp_aux_vesa_enable_backlight(const struct intel_crtc_state *crtc_state,
467 				   const struct drm_connector_state *conn_state, u32 level)
468 {
469 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
470 	struct intel_panel *panel = &connector->panel;
471 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
472 
473 	if (!panel->backlight.edp.vesa.info.aux_enable) {
474 		u32 pwm_level;
475 
476 		if (!panel->backlight.edp.vesa.info.aux_set)
477 			pwm_level = intel_backlight_level_to_pwm(connector, level);
478 		else
479 			pwm_level = intel_backlight_invert_pwm_level(connector,
480 								     panel->backlight.pwm_level_max);
481 
482 		panel->backlight.pwm_funcs->enable(crtc_state, conn_state, pwm_level);
483 	}
484 
485 	drm_edp_backlight_enable(&intel_dp->aux, &panel->backlight.edp.vesa.info, level);
486 }
487 
488 static void intel_dp_aux_vesa_disable_backlight(const struct drm_connector_state *old_conn_state,
489 						u32 level)
490 {
491 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
492 	struct intel_panel *panel = &connector->panel;
493 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
494 
495 	drm_edp_backlight_disable(&intel_dp->aux, &panel->backlight.edp.vesa.info);
496 
497 	if (!panel->backlight.edp.vesa.info.aux_enable)
498 		panel->backlight.pwm_funcs->disable(old_conn_state,
499 						    intel_backlight_invert_pwm_level(connector, 0));
500 }
501 
502 static int intel_dp_aux_vesa_setup_backlight(struct intel_connector *connector, enum pipe pipe)
503 {
504 	struct intel_dp *intel_dp = intel_attached_dp(connector);
505 	struct intel_panel *panel = &connector->panel;
506 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
507 	u16 current_level;
508 	u8 current_mode;
509 	int ret;
510 
511 	ret = drm_edp_backlight_init(&intel_dp->aux, &panel->backlight.edp.vesa.info,
512 				     panel->vbt.backlight.pwm_freq_hz, intel_dp->edp_dpcd,
513 				     &current_level, &current_mode);
514 	if (ret < 0)
515 		return ret;
516 
517 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] AUX VESA backlight enable is controlled through %s\n",
518 		    connector->base.base.id, connector->base.name,
519 		    dpcd_vs_pwm_str(panel->backlight.edp.vesa.info.aux_enable));
520 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] AUX VESA backlight level is controlled through %s\n",
521 		    connector->base.base.id, connector->base.name,
522 		    dpcd_vs_pwm_str(panel->backlight.edp.vesa.info.aux_set));
523 
524 	if (!panel->backlight.edp.vesa.info.aux_set || !panel->backlight.edp.vesa.info.aux_enable) {
525 		ret = panel->backlight.pwm_funcs->setup(connector, pipe);
526 		if (ret < 0) {
527 			drm_err(&i915->drm,
528 				"[CONNECTOR:%d:%s] Failed to setup PWM backlight controls for eDP backlight: %d\n",
529 				connector->base.base.id, connector->base.name, ret);
530 			return ret;
531 		}
532 	}
533 
534 	if (panel->backlight.edp.vesa.info.aux_set) {
535 		panel->backlight.max = panel->backlight.edp.vesa.info.max;
536 		panel->backlight.min = 0;
537 		if (current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
538 			panel->backlight.level = current_level;
539 			panel->backlight.enabled = panel->backlight.level != 0;
540 		} else {
541 			panel->backlight.level = panel->backlight.max;
542 			panel->backlight.enabled = false;
543 		}
544 	} else {
545 		panel->backlight.max = panel->backlight.pwm_level_max;
546 		panel->backlight.min = panel->backlight.pwm_level_min;
547 		if (current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_PWM) {
548 			panel->backlight.level = panel->backlight.pwm_funcs->get(connector, pipe);
549 			panel->backlight.enabled = panel->backlight.pwm_enabled;
550 		} else {
551 			panel->backlight.level = panel->backlight.max;
552 			panel->backlight.enabled = false;
553 		}
554 	}
555 
556 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] Using AUX VESA interface for backlight control\n",
557 		    connector->base.base.id, connector->base.name);
558 
559 	return 0;
560 }
561 
562 static bool
563 intel_dp_aux_supports_vesa_backlight(struct intel_connector *connector)
564 {
565 	struct intel_dp *intel_dp = intel_attached_dp(connector);
566 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
567 
568 	if (drm_edp_backlight_supported(intel_dp->edp_dpcd)) {
569 		drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] AUX Backlight Control Supported!\n",
570 			    connector->base.base.id, connector->base.name);
571 		return true;
572 	}
573 	return false;
574 }
575 
576 static const struct intel_panel_bl_funcs intel_dp_hdr_bl_funcs = {
577 	.setup = intel_dp_aux_hdr_setup_backlight,
578 	.enable = intel_dp_aux_hdr_enable_backlight,
579 	.disable = intel_dp_aux_hdr_disable_backlight,
580 	.set = intel_dp_aux_hdr_set_backlight,
581 	.get = intel_dp_aux_hdr_get_backlight,
582 };
583 
584 static const struct intel_panel_bl_funcs intel_dp_vesa_bl_funcs = {
585 	.setup = intel_dp_aux_vesa_setup_backlight,
586 	.enable = intel_dp_aux_vesa_enable_backlight,
587 	.disable = intel_dp_aux_vesa_disable_backlight,
588 	.set = intel_dp_aux_vesa_set_backlight,
589 	.get = intel_dp_aux_vesa_get_backlight,
590 };
591 
592 int intel_dp_aux_init_backlight_funcs(struct intel_connector *connector)
593 {
594 	struct drm_device *dev = connector->base.dev;
595 	struct intel_panel *panel = &connector->panel;
596 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
597 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
598 	bool try_intel_interface = false, try_vesa_interface = false;
599 
600 	/* Check the VBT and user's module parameters to figure out which
601 	 * interfaces to probe
602 	 */
603 	switch (i915->display.params.enable_dpcd_backlight) {
604 	case INTEL_DP_AUX_BACKLIGHT_OFF:
605 		return -ENODEV;
606 	case INTEL_DP_AUX_BACKLIGHT_AUTO:
607 		switch (panel->vbt.backlight.type) {
608 		case INTEL_BACKLIGHT_VESA_EDP_AUX_INTERFACE:
609 			try_vesa_interface = true;
610 			break;
611 		case INTEL_BACKLIGHT_DISPLAY_DDI:
612 			try_intel_interface = true;
613 			break;
614 		default:
615 			return -ENODEV;
616 		}
617 		break;
618 	case INTEL_DP_AUX_BACKLIGHT_ON:
619 		if (panel->vbt.backlight.type != INTEL_BACKLIGHT_VESA_EDP_AUX_INTERFACE)
620 			try_intel_interface = true;
621 
622 		try_vesa_interface = true;
623 		break;
624 	case INTEL_DP_AUX_BACKLIGHT_FORCE_VESA:
625 		try_vesa_interface = true;
626 		break;
627 	case INTEL_DP_AUX_BACKLIGHT_FORCE_INTEL:
628 		try_intel_interface = true;
629 		break;
630 	}
631 
632 	/*
633 	 * Since Intel has their own backlight control interface, the majority of machines out there
634 	 * using DPCD backlight controls with Intel GPUs will be using this interface as opposed to
635 	 * the VESA interface. However, other GPUs (such as Nvidia's) will always use the VESA
636 	 * interface. This means that there's quite a number of panels out there that will advertise
637 	 * support for both interfaces, primarily systems with Intel/Nvidia hybrid GPU setups.
638 	 *
639 	 * There's a catch to this though: on many panels that advertise support for both
640 	 * interfaces, the VESA backlight interface will stop working once we've programmed the
641 	 * panel with Intel's OUI - which is also required for us to be able to detect Intel's
642 	 * backlight interface at all. This means that the only sensible way for us to detect both
643 	 * interfaces is to probe for Intel's first, and VESA's second.
644 	 */
645 	if (try_intel_interface && intel_dp_aux_supports_hdr_backlight(connector)) {
646 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Using Intel proprietary eDP backlight controls\n",
647 			    connector->base.base.id, connector->base.name);
648 		panel->backlight.funcs = &intel_dp_hdr_bl_funcs;
649 		return 0;
650 	}
651 
652 	if (try_vesa_interface && intel_dp_aux_supports_vesa_backlight(connector)) {
653 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Using VESA eDP backlight controls\n",
654 			    connector->base.base.id, connector->base.name);
655 		panel->backlight.funcs = &intel_dp_vesa_bl_funcs;
656 		return 0;
657 	}
658 
659 	return -ENODEV;
660 }
661