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 /* TODO:
44  * Implement HDR, right now we just implement the bare minimum to bring us back into SDR mode so we
45  * can make people's backlights work in the mean time
46  */
47 
48 /*
49  * DP AUX registers for Intel's proprietary HDR backlight interface. We define
50  * them here since we'll likely be the only driver to ever use these.
51  */
52 #define INTEL_EDP_HDR_TCON_CAP0                                        0x340
53 
54 #define INTEL_EDP_HDR_TCON_CAP1                                        0x341
55 # define INTEL_EDP_HDR_TCON_2084_DECODE_CAP                           BIT(0)
56 # define INTEL_EDP_HDR_TCON_2020_GAMUT_CAP                            BIT(1)
57 # define INTEL_EDP_HDR_TCON_TONE_MAPPING_CAP                          BIT(2)
58 # define INTEL_EDP_HDR_TCON_SEGMENTED_BACKLIGHT_CAP                   BIT(3)
59 # define INTEL_EDP_HDR_TCON_BRIGHTNESS_NITS_CAP                       BIT(4)
60 # define INTEL_EDP_HDR_TCON_OPTIMIZATION_CAP                          BIT(5)
61 # define INTEL_EDP_HDR_TCON_SDP_COLORIMETRY_CAP                       BIT(6)
62 # define INTEL_EDP_HDR_TCON_SRGB_TO_PANEL_GAMUT_CONVERSION_CAP        BIT(7)
63 
64 #define INTEL_EDP_HDR_TCON_CAP2                                        0x342
65 # define INTEL_EDP_SDR_TCON_BRIGHTNESS_AUX_CAP                        BIT(0)
66 
67 #define INTEL_EDP_HDR_TCON_CAP3                                        0x343
68 
69 #define INTEL_EDP_HDR_GETSET_CTRL_PARAMS                               0x344
70 # define INTEL_EDP_HDR_TCON_2084_DECODE_ENABLE                        BIT(0)
71 # define INTEL_EDP_HDR_TCON_2020_GAMUT_ENABLE                         BIT(1)
72 # define INTEL_EDP_HDR_TCON_TONE_MAPPING_ENABLE                       BIT(2) /* Pre-TGL+ */
73 # define INTEL_EDP_HDR_TCON_SEGMENTED_BACKLIGHT_ENABLE                BIT(3)
74 # define INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE                     BIT(4)
75 # define INTEL_EDP_HDR_TCON_SRGB_TO_PANEL_GAMUT_ENABLE                BIT(5)
76 /* Bit 6 is reserved */
77 # define INTEL_EDP_HDR_TCON_SDP_COLORIMETRY_ENABLE                    BIT(7)
78 
79 #define INTEL_EDP_HDR_CONTENT_LUMINANCE                                0x346 /* Pre-TGL+ */
80 #define INTEL_EDP_HDR_PANEL_LUMINANCE_OVERRIDE                         0x34A
81 #define INTEL_EDP_SDR_LUMINANCE_LEVEL                                  0x352
82 #define INTEL_EDP_BRIGHTNESS_NITS_LSB                                  0x354
83 #define INTEL_EDP_BRIGHTNESS_NITS_MSB                                  0x355
84 #define INTEL_EDP_BRIGHTNESS_DELAY_FRAMES                              0x356
85 #define INTEL_EDP_BRIGHTNESS_PER_FRAME_STEPS                           0x357
86 
87 #define INTEL_EDP_BRIGHTNESS_OPTIMIZATION_0                            0x358
88 # define INTEL_EDP_TCON_USAGE_MASK                             GENMASK(0, 3)
89 # define INTEL_EDP_TCON_USAGE_UNKNOWN                                    0x0
90 # define INTEL_EDP_TCON_USAGE_DESKTOP                                    0x1
91 # define INTEL_EDP_TCON_USAGE_FULL_SCREEN_MEDIA                          0x2
92 # define INTEL_EDP_TCON_USAGE_FULL_SCREEN_GAMING                         0x3
93 # define INTEL_EDP_TCON_POWER_MASK                                    BIT(4)
94 # define INTEL_EDP_TCON_POWER_DC                                    (0 << 4)
95 # define INTEL_EDP_TCON_POWER_AC                                    (1 << 4)
96 # define INTEL_EDP_TCON_OPTIMIZATION_STRENGTH_MASK             GENMASK(5, 7)
97 
98 #define INTEL_EDP_BRIGHTNESS_OPTIMIZATION_1                            0x359
99 
100 /* Intel EDP backlight callbacks */
101 static bool
102 intel_dp_aux_supports_hdr_backlight(struct intel_connector *connector)
103 {
104 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
105 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
106 	struct drm_dp_aux *aux = &intel_dp->aux;
107 	struct intel_panel *panel = &connector->panel;
108 	int ret;
109 	u8 tcon_cap[4];
110 
111 	intel_dp_wait_source_oui(intel_dp);
112 
113 	ret = drm_dp_dpcd_read(aux, INTEL_EDP_HDR_TCON_CAP0, tcon_cap, sizeof(tcon_cap));
114 	if (ret != sizeof(tcon_cap))
115 		return false;
116 
117 	if (!(tcon_cap[1] & INTEL_EDP_HDR_TCON_BRIGHTNESS_NITS_CAP))
118 		return false;
119 
120 	if (tcon_cap[0] >= 1) {
121 		drm_dbg_kms(&i915->drm, "Detected Intel HDR backlight interface version %d\n",
122 			    tcon_cap[0]);
123 	} else {
124 		drm_dbg_kms(&i915->drm, "Detected unsupported HDR backlight interface version %d\n",
125 			    tcon_cap[0]);
126 		return false;
127 	}
128 
129 	panel->backlight.edp.intel.sdr_uses_aux =
130 		tcon_cap[2] & INTEL_EDP_SDR_TCON_BRIGHTNESS_AUX_CAP;
131 
132 	return true;
133 }
134 
135 static u32
136 intel_dp_aux_hdr_get_backlight(struct intel_connector *connector, enum pipe pipe)
137 {
138 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
139 	struct intel_panel *panel = &connector->panel;
140 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
141 	u8 tmp;
142 	u8 buf[2] = { 0 };
143 
144 	if (drm_dp_dpcd_readb(&intel_dp->aux, INTEL_EDP_HDR_GETSET_CTRL_PARAMS, &tmp) != 1) {
145 		drm_err(&i915->drm, "Failed to read current backlight mode from DPCD\n");
146 		return 0;
147 	}
148 
149 	if (!(tmp & INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE)) {
150 		if (!panel->backlight.edp.intel.sdr_uses_aux) {
151 			u32 pwm_level = panel->backlight.pwm_funcs->get(connector, pipe);
152 
153 			return intel_backlight_level_from_pwm(connector, pwm_level);
154 		}
155 
156 		/* Assume 100% brightness if backlight controls aren't enabled yet */
157 		return panel->backlight.max;
158 	}
159 
160 	if (drm_dp_dpcd_read(&intel_dp->aux, INTEL_EDP_BRIGHTNESS_NITS_LSB, buf,
161 			     sizeof(buf)) != sizeof(buf)) {
162 		drm_err(&i915->drm, "Failed to read brightness from DPCD\n");
163 		return 0;
164 	}
165 
166 	return (buf[1] << 8 | buf[0]);
167 }
168 
169 static void
170 intel_dp_aux_hdr_set_aux_backlight(const struct drm_connector_state *conn_state, u32 level)
171 {
172 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
173 	struct drm_device *dev = connector->base.dev;
174 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
175 	u8 buf[4] = { 0 };
176 
177 	buf[0] = level & 0xFF;
178 	buf[1] = (level & 0xFF00) >> 8;
179 
180 	if (drm_dp_dpcd_write(&intel_dp->aux, INTEL_EDP_BRIGHTNESS_NITS_LSB, buf,
181 			      sizeof(buf)) != sizeof(buf))
182 		drm_err(dev, "Failed to write brightness level to DPCD\n");
183 }
184 
185 static void
186 intel_dp_aux_hdr_set_backlight(const struct drm_connector_state *conn_state, u32 level)
187 {
188 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
189 	struct intel_panel *panel = &connector->panel;
190 
191 	if (panel->backlight.edp.intel.sdr_uses_aux) {
192 		intel_dp_aux_hdr_set_aux_backlight(conn_state, level);
193 	} else {
194 		const u32 pwm_level = intel_backlight_level_to_pwm(connector, level);
195 
196 		intel_backlight_set_pwm_level(conn_state, pwm_level);
197 	}
198 }
199 
200 static void
201 intel_dp_aux_hdr_enable_backlight(const struct intel_crtc_state *crtc_state,
202 				  const struct drm_connector_state *conn_state, u32 level)
203 {
204 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
205 	struct intel_panel *panel = &connector->panel;
206 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
207 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
208 	int ret;
209 	u8 old_ctrl, ctrl;
210 
211 	intel_dp_wait_source_oui(intel_dp);
212 
213 	ret = drm_dp_dpcd_readb(&intel_dp->aux, INTEL_EDP_HDR_GETSET_CTRL_PARAMS, &old_ctrl);
214 	if (ret != 1) {
215 		drm_err(&i915->drm, "Failed to read current backlight control mode: %d\n", ret);
216 		return;
217 	}
218 
219 	ctrl = old_ctrl;
220 	if (panel->backlight.edp.intel.sdr_uses_aux) {
221 		ctrl |= INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE;
222 		intel_dp_aux_hdr_set_aux_backlight(conn_state, level);
223 	} else {
224 		u32 pwm_level = intel_backlight_level_to_pwm(connector, level);
225 
226 		panel->backlight.pwm_funcs->enable(crtc_state, conn_state, pwm_level);
227 
228 		ctrl &= ~INTEL_EDP_HDR_TCON_BRIGHTNESS_AUX_ENABLE;
229 	}
230 
231 	if (ctrl != old_ctrl)
232 		if (drm_dp_dpcd_writeb(&intel_dp->aux, INTEL_EDP_HDR_GETSET_CTRL_PARAMS, ctrl) != 1)
233 			drm_err(&i915->drm, "Failed to configure DPCD brightness controls\n");
234 }
235 
236 static void
237 intel_dp_aux_hdr_disable_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 	/* Nothing to do for AUX based backlight controls */
243 	if (panel->backlight.edp.intel.sdr_uses_aux)
244 		return;
245 
246 	/* Note we want the actual pwm_level to be 0, regardless of pwm_min */
247 	panel->backlight.pwm_funcs->disable(conn_state, intel_backlight_invert_pwm_level(connector, 0));
248 }
249 
250 static int
251 intel_dp_aux_hdr_setup_backlight(struct intel_connector *connector, enum pipe pipe)
252 {
253 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
254 	struct intel_panel *panel = &connector->panel;
255 	int ret;
256 
257 	if (panel->backlight.edp.intel.sdr_uses_aux) {
258 		drm_dbg_kms(&i915->drm, "SDR backlight is controlled through DPCD\n");
259 	} else {
260 		drm_dbg_kms(&i915->drm, "SDR backlight is controlled through PWM\n");
261 
262 		ret = panel->backlight.pwm_funcs->setup(connector, pipe);
263 		if (ret < 0) {
264 			drm_err(&i915->drm,
265 				"Failed to setup SDR backlight controls through PWM: %d\n", ret);
266 			return ret;
267 		}
268 	}
269 
270 	panel->backlight.max = 512;
271 	panel->backlight.min = 0;
272 	panel->backlight.level = intel_dp_aux_hdr_get_backlight(connector, pipe);
273 	panel->backlight.enabled = panel->backlight.level != 0;
274 
275 	return 0;
276 }
277 
278 /* VESA backlight callbacks */
279 static u32 intel_dp_aux_vesa_get_backlight(struct intel_connector *connector, enum pipe unused)
280 {
281 	return connector->panel.backlight.level;
282 }
283 
284 static void
285 intel_dp_aux_vesa_set_backlight(const struct drm_connector_state *conn_state, u32 level)
286 {
287 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
288 	struct intel_panel *panel = &connector->panel;
289 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
290 
291 	if (!panel->backlight.edp.vesa.info.aux_set) {
292 		const u32 pwm_level = intel_backlight_level_to_pwm(connector, level);
293 
294 		intel_backlight_set_pwm_level(conn_state, pwm_level);
295 	}
296 
297 	drm_edp_backlight_set_level(&intel_dp->aux, &panel->backlight.edp.vesa.info, level);
298 }
299 
300 static void
301 intel_dp_aux_vesa_enable_backlight(const struct intel_crtc_state *crtc_state,
302 				   const struct drm_connector_state *conn_state, u32 level)
303 {
304 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
305 	struct intel_panel *panel = &connector->panel;
306 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
307 
308 	if (!panel->backlight.edp.vesa.info.aux_enable) {
309 		u32 pwm_level;
310 
311 		if (!panel->backlight.edp.vesa.info.aux_set)
312 			pwm_level = intel_backlight_level_to_pwm(connector, level);
313 		else
314 			pwm_level = intel_backlight_invert_pwm_level(connector,
315 								     panel->backlight.pwm_level_max);
316 
317 		panel->backlight.pwm_funcs->enable(crtc_state, conn_state, pwm_level);
318 	}
319 
320 	drm_edp_backlight_enable(&intel_dp->aux, &panel->backlight.edp.vesa.info, level);
321 }
322 
323 static void intel_dp_aux_vesa_disable_backlight(const struct drm_connector_state *old_conn_state,
324 						u32 level)
325 {
326 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
327 	struct intel_panel *panel = &connector->panel;
328 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
329 
330 	drm_edp_backlight_disable(&intel_dp->aux, &panel->backlight.edp.vesa.info);
331 
332 	if (!panel->backlight.edp.vesa.info.aux_enable)
333 		panel->backlight.pwm_funcs->disable(old_conn_state,
334 						    intel_backlight_invert_pwm_level(connector, 0));
335 }
336 
337 static int intel_dp_aux_vesa_setup_backlight(struct intel_connector *connector, enum pipe pipe)
338 {
339 	struct intel_dp *intel_dp = intel_attached_dp(connector);
340 	struct intel_panel *panel = &connector->panel;
341 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
342 	u16 current_level;
343 	u8 current_mode;
344 	int ret;
345 
346 	ret = drm_edp_backlight_init(&intel_dp->aux, &panel->backlight.edp.vesa.info,
347 				     i915->vbt.backlight.pwm_freq_hz, intel_dp->edp_dpcd,
348 				     &current_level, &current_mode);
349 	if (ret < 0)
350 		return ret;
351 
352 	if (!panel->backlight.edp.vesa.info.aux_set || !panel->backlight.edp.vesa.info.aux_enable) {
353 		ret = panel->backlight.pwm_funcs->setup(connector, pipe);
354 		if (ret < 0) {
355 			drm_err(&i915->drm,
356 				"Failed to setup PWM backlight controls for eDP backlight: %d\n",
357 				ret);
358 			return ret;
359 		}
360 	}
361 
362 	if (panel->backlight.edp.vesa.info.aux_set) {
363 		panel->backlight.max = panel->backlight.edp.vesa.info.max;
364 		panel->backlight.min = 0;
365 		if (current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
366 			panel->backlight.level = current_level;
367 			panel->backlight.enabled = panel->backlight.level != 0;
368 		} else {
369 			panel->backlight.level = panel->backlight.max;
370 			panel->backlight.enabled = false;
371 		}
372 	} else {
373 		panel->backlight.max = panel->backlight.pwm_level_max;
374 		panel->backlight.min = panel->backlight.pwm_level_min;
375 		if (current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_PWM) {
376 			panel->backlight.level = panel->backlight.pwm_funcs->get(connector, pipe);
377 			panel->backlight.enabled = panel->backlight.pwm_enabled;
378 		} else {
379 			panel->backlight.level = panel->backlight.max;
380 			panel->backlight.enabled = false;
381 		}
382 	}
383 
384 	return 0;
385 }
386 
387 static bool
388 intel_dp_aux_supports_vesa_backlight(struct intel_connector *connector)
389 {
390 	struct intel_dp *intel_dp = intel_attached_dp(connector);
391 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
392 
393 	if (drm_edp_backlight_supported(intel_dp->edp_dpcd)) {
394 		drm_dbg_kms(&i915->drm, "AUX Backlight Control Supported!\n");
395 		return true;
396 	}
397 	return false;
398 }
399 
400 static const struct intel_panel_bl_funcs intel_dp_hdr_bl_funcs = {
401 	.setup = intel_dp_aux_hdr_setup_backlight,
402 	.enable = intel_dp_aux_hdr_enable_backlight,
403 	.disable = intel_dp_aux_hdr_disable_backlight,
404 	.set = intel_dp_aux_hdr_set_backlight,
405 	.get = intel_dp_aux_hdr_get_backlight,
406 };
407 
408 static const struct intel_panel_bl_funcs intel_dp_vesa_bl_funcs = {
409 	.setup = intel_dp_aux_vesa_setup_backlight,
410 	.enable = intel_dp_aux_vesa_enable_backlight,
411 	.disable = intel_dp_aux_vesa_disable_backlight,
412 	.set = intel_dp_aux_vesa_set_backlight,
413 	.get = intel_dp_aux_vesa_get_backlight,
414 };
415 
416 enum intel_dp_aux_backlight_modparam {
417 	INTEL_DP_AUX_BACKLIGHT_AUTO = -1,
418 	INTEL_DP_AUX_BACKLIGHT_OFF = 0,
419 	INTEL_DP_AUX_BACKLIGHT_ON = 1,
420 	INTEL_DP_AUX_BACKLIGHT_FORCE_VESA = 2,
421 	INTEL_DP_AUX_BACKLIGHT_FORCE_INTEL = 3,
422 };
423 
424 int intel_dp_aux_init_backlight_funcs(struct intel_connector *connector)
425 {
426 	struct drm_device *dev = connector->base.dev;
427 	struct intel_panel *panel = &connector->panel;
428 	struct intel_dp *intel_dp = enc_to_intel_dp(connector->encoder);
429 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
430 	bool try_intel_interface = false, try_vesa_interface = false;
431 
432 	/* Check the VBT and user's module parameters to figure out which
433 	 * interfaces to probe
434 	 */
435 	switch (i915->params.enable_dpcd_backlight) {
436 	case INTEL_DP_AUX_BACKLIGHT_OFF:
437 		return -ENODEV;
438 	case INTEL_DP_AUX_BACKLIGHT_AUTO:
439 		switch (i915->vbt.backlight.type) {
440 		case INTEL_BACKLIGHT_VESA_EDP_AUX_INTERFACE:
441 			try_vesa_interface = true;
442 			break;
443 		case INTEL_BACKLIGHT_DISPLAY_DDI:
444 			try_intel_interface = true;
445 			break;
446 		default:
447 			return -ENODEV;
448 		}
449 		break;
450 	case INTEL_DP_AUX_BACKLIGHT_ON:
451 		if (i915->vbt.backlight.type != INTEL_BACKLIGHT_VESA_EDP_AUX_INTERFACE)
452 			try_intel_interface = true;
453 
454 		try_vesa_interface = true;
455 		break;
456 	case INTEL_DP_AUX_BACKLIGHT_FORCE_VESA:
457 		try_vesa_interface = true;
458 		break;
459 	case INTEL_DP_AUX_BACKLIGHT_FORCE_INTEL:
460 		try_intel_interface = true;
461 		break;
462 	}
463 
464 	/*
465 	 * Since Intel has their own backlight control interface, the majority of machines out there
466 	 * using DPCD backlight controls with Intel GPUs will be using this interface as opposed to
467 	 * the VESA interface. However, other GPUs (such as Nvidia's) will always use the VESA
468 	 * interface. This means that there's quite a number of panels out there that will advertise
469 	 * support for both interfaces, primarily systems with Intel/Nvidia hybrid GPU setups.
470 	 *
471 	 * There's a catch to this though: on many panels that advertise support for both
472 	 * interfaces, the VESA backlight interface will stop working once we've programmed the
473 	 * panel with Intel's OUI - which is also required for us to be able to detect Intel's
474 	 * backlight interface at all. This means that the only sensible way for us to detect both
475 	 * interfaces is to probe for Intel's first, and VESA's second.
476 	 */
477 	if (try_intel_interface && intel_dp_aux_supports_hdr_backlight(connector)) {
478 		drm_dbg_kms(dev, "Using Intel proprietary eDP backlight controls\n");
479 		panel->backlight.funcs = &intel_dp_hdr_bl_funcs;
480 		return 0;
481 	}
482 
483 	if (try_vesa_interface && intel_dp_aux_supports_vesa_backlight(connector)) {
484 		drm_dbg_kms(dev, "Using VESA eDP backlight controls\n");
485 		panel->backlight.funcs = &intel_dp_vesa_bl_funcs;
486 		return 0;
487 	}
488 
489 	return -ENODEV;
490 }
491