xref: /linux/drivers/gpu/drm/i915/display/intel_pps.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 
6 #include "g4x_dp.h"
7 #include "i915_drv.h"
8 #include "i915_reg.h"
9 #include "intel_de.h"
10 #include "intel_display_power_well.h"
11 #include "intel_display_types.h"
12 #include "intel_dp.h"
13 #include "intel_dpio_phy.h"
14 #include "intel_dpll.h"
15 #include "intel_lvds.h"
16 #include "intel_lvds_regs.h"
17 #include "intel_pps.h"
18 #include "intel_pps_regs.h"
19 #include "intel_quirks.h"
20 
21 static void vlv_steal_power_sequencer(struct drm_i915_private *dev_priv,
22 				      enum pipe pipe);
23 
24 static void pps_init_delays(struct intel_dp *intel_dp);
25 static void pps_init_registers(struct intel_dp *intel_dp, bool force_disable_vdd);
26 
27 static const char *pps_name(struct drm_i915_private *i915,
28 			    struct intel_pps *pps)
29 {
30 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
31 		switch (pps->pps_pipe) {
32 		case INVALID_PIPE:
33 			/*
34 			 * FIXME would be nice if we can guarantee
35 			 * to always have a valid PPS when calling this.
36 			 */
37 			return "PPS <none>";
38 		case PIPE_A:
39 			return "PPS A";
40 		case PIPE_B:
41 			return "PPS B";
42 		default:
43 			MISSING_CASE(pps->pps_pipe);
44 			break;
45 		}
46 	} else {
47 		switch (pps->pps_idx) {
48 		case 0:
49 			return "PPS 0";
50 		case 1:
51 			return "PPS 1";
52 		default:
53 			MISSING_CASE(pps->pps_idx);
54 			break;
55 		}
56 	}
57 
58 	return "PPS <invalid>";
59 }
60 
61 intel_wakeref_t intel_pps_lock(struct intel_dp *intel_dp)
62 {
63 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
64 	intel_wakeref_t wakeref;
65 
66 	/*
67 	 * See intel_pps_reset_all() why we need a power domain reference here.
68 	 */
69 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
70 	mutex_lock(&dev_priv->display.pps.mutex);
71 
72 	return wakeref;
73 }
74 
75 intel_wakeref_t intel_pps_unlock(struct intel_dp *intel_dp,
76 				 intel_wakeref_t wakeref)
77 {
78 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
79 
80 	mutex_unlock(&dev_priv->display.pps.mutex);
81 	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
82 
83 	return 0;
84 }
85 
86 static void
87 vlv_power_sequencer_kick(struct intel_dp *intel_dp)
88 {
89 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
90 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
91 	enum pipe pipe = intel_dp->pps.pps_pipe;
92 	bool pll_enabled, release_cl_override = false;
93 	enum dpio_phy phy = vlv_pipe_to_phy(pipe);
94 	enum dpio_channel ch = vlv_pipe_to_channel(pipe);
95 	u32 DP;
96 
97 	if (drm_WARN(&dev_priv->drm,
98 		     intel_de_read(dev_priv, intel_dp->output_reg) & DP_PORT_EN,
99 		     "skipping %s kick due to [ENCODER:%d:%s] being active\n",
100 		     pps_name(dev_priv, &intel_dp->pps),
101 		     dig_port->base.base.base.id, dig_port->base.base.name))
102 		return;
103 
104 	drm_dbg_kms(&dev_priv->drm,
105 		    "kicking %s for [ENCODER:%d:%s]\n",
106 		    pps_name(dev_priv, &intel_dp->pps),
107 		    dig_port->base.base.base.id, dig_port->base.base.name);
108 
109 	/* Preserve the BIOS-computed detected bit. This is
110 	 * supposed to be read-only.
111 	 */
112 	DP = intel_de_read(dev_priv, intel_dp->output_reg) & DP_DETECTED;
113 	DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
114 	DP |= DP_PORT_WIDTH(1);
115 	DP |= DP_LINK_TRAIN_PAT_1;
116 
117 	if (IS_CHERRYVIEW(dev_priv))
118 		DP |= DP_PIPE_SEL_CHV(pipe);
119 	else
120 		DP |= DP_PIPE_SEL(pipe);
121 
122 	pll_enabled = intel_de_read(dev_priv, DPLL(dev_priv, pipe)) & DPLL_VCO_ENABLE;
123 
124 	/*
125 	 * The DPLL for the pipe must be enabled for this to work.
126 	 * So enable temporarily it if it's not already enabled.
127 	 */
128 	if (!pll_enabled) {
129 		release_cl_override = IS_CHERRYVIEW(dev_priv) &&
130 			!chv_phy_powergate_ch(dev_priv, phy, ch, true);
131 
132 		if (vlv_force_pll_on(dev_priv, pipe, vlv_get_dpll(dev_priv))) {
133 			drm_err(&dev_priv->drm,
134 				"Failed to force on PLL for pipe %c!\n",
135 				pipe_name(pipe));
136 			return;
137 		}
138 	}
139 
140 	/*
141 	 * Similar magic as in intel_dp_enable_port().
142 	 * We _must_ do this port enable + disable trick
143 	 * to make this power sequencer lock onto the port.
144 	 * Otherwise even VDD force bit won't work.
145 	 */
146 	intel_de_write(dev_priv, intel_dp->output_reg, DP);
147 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
148 
149 	intel_de_write(dev_priv, intel_dp->output_reg, DP | DP_PORT_EN);
150 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
151 
152 	intel_de_write(dev_priv, intel_dp->output_reg, DP & ~DP_PORT_EN);
153 	intel_de_posting_read(dev_priv, intel_dp->output_reg);
154 
155 	if (!pll_enabled) {
156 		vlv_force_pll_off(dev_priv, pipe);
157 
158 		if (release_cl_override)
159 			chv_phy_powergate_ch(dev_priv, phy, ch, false);
160 	}
161 }
162 
163 static enum pipe vlv_find_free_pps(struct drm_i915_private *dev_priv)
164 {
165 	struct intel_encoder *encoder;
166 	unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
167 
168 	/*
169 	 * We don't have power sequencer currently.
170 	 * Pick one that's not used by other ports.
171 	 */
172 	for_each_intel_dp(&dev_priv->drm, encoder) {
173 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
174 
175 		if (encoder->type == INTEL_OUTPUT_EDP) {
176 			drm_WARN_ON(&dev_priv->drm,
177 				    intel_dp->pps.active_pipe != INVALID_PIPE &&
178 				    intel_dp->pps.active_pipe !=
179 				    intel_dp->pps.pps_pipe);
180 
181 			if (intel_dp->pps.pps_pipe != INVALID_PIPE)
182 				pipes &= ~(1 << intel_dp->pps.pps_pipe);
183 		} else {
184 			drm_WARN_ON(&dev_priv->drm,
185 				    intel_dp->pps.pps_pipe != INVALID_PIPE);
186 
187 			if (intel_dp->pps.active_pipe != INVALID_PIPE)
188 				pipes &= ~(1 << intel_dp->pps.active_pipe);
189 		}
190 	}
191 
192 	if (pipes == 0)
193 		return INVALID_PIPE;
194 
195 	return ffs(pipes) - 1;
196 }
197 
198 static enum pipe
199 vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
200 {
201 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
202 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
203 	enum pipe pipe;
204 
205 	lockdep_assert_held(&dev_priv->display.pps.mutex);
206 
207 	/* We should never land here with regular DP ports */
208 	drm_WARN_ON(&dev_priv->drm, !intel_dp_is_edp(intel_dp));
209 
210 	drm_WARN_ON(&dev_priv->drm, intel_dp->pps.active_pipe != INVALID_PIPE &&
211 		    intel_dp->pps.active_pipe != intel_dp->pps.pps_pipe);
212 
213 	if (intel_dp->pps.pps_pipe != INVALID_PIPE)
214 		return intel_dp->pps.pps_pipe;
215 
216 	pipe = vlv_find_free_pps(dev_priv);
217 
218 	/*
219 	 * Didn't find one. This should not happen since there
220 	 * are two power sequencers and up to two eDP ports.
221 	 */
222 	if (drm_WARN_ON(&dev_priv->drm, pipe == INVALID_PIPE))
223 		pipe = PIPE_A;
224 
225 	vlv_steal_power_sequencer(dev_priv, pipe);
226 	intel_dp->pps.pps_pipe = pipe;
227 
228 	drm_dbg_kms(&dev_priv->drm,
229 		    "picked %s for [ENCODER:%d:%s]\n",
230 		    pps_name(dev_priv, &intel_dp->pps),
231 		    dig_port->base.base.base.id, dig_port->base.base.name);
232 
233 	/* init power sequencer on this pipe and port */
234 	pps_init_delays(intel_dp);
235 	pps_init_registers(intel_dp, true);
236 
237 	/*
238 	 * Even vdd force doesn't work until we've made
239 	 * the power sequencer lock in on the port.
240 	 */
241 	vlv_power_sequencer_kick(intel_dp);
242 
243 	return intel_dp->pps.pps_pipe;
244 }
245 
246 static int
247 bxt_power_sequencer_idx(struct intel_dp *intel_dp)
248 {
249 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
250 	int pps_idx = intel_dp->pps.pps_idx;
251 
252 	lockdep_assert_held(&dev_priv->display.pps.mutex);
253 
254 	/* We should never land here with regular DP ports */
255 	drm_WARN_ON(&dev_priv->drm, !intel_dp_is_edp(intel_dp));
256 
257 	if (!intel_dp->pps.pps_reset)
258 		return pps_idx;
259 
260 	intel_dp->pps.pps_reset = false;
261 
262 	/*
263 	 * Only the HW needs to be reprogrammed, the SW state is fixed and
264 	 * has been setup during connector init.
265 	 */
266 	pps_init_registers(intel_dp, false);
267 
268 	return pps_idx;
269 }
270 
271 typedef bool (*pps_check)(struct drm_i915_private *dev_priv, int pps_idx);
272 
273 static bool pps_has_pp_on(struct drm_i915_private *dev_priv, int pps_idx)
274 {
275 	return intel_de_read(dev_priv, PP_STATUS(dev_priv, pps_idx)) & PP_ON;
276 }
277 
278 static bool pps_has_vdd_on(struct drm_i915_private *dev_priv, int pps_idx)
279 {
280 	return intel_de_read(dev_priv, PP_CONTROL(dev_priv, pps_idx)) & EDP_FORCE_VDD;
281 }
282 
283 static bool pps_any(struct drm_i915_private *dev_priv, int pps_idx)
284 {
285 	return true;
286 }
287 
288 static enum pipe
289 vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
290 		     enum port port, pps_check check)
291 {
292 	enum pipe pipe;
293 
294 	for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
295 		u32 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(dev_priv, pipe)) &
296 			PANEL_PORT_SELECT_MASK;
297 
298 		if (port_sel != PANEL_PORT_SELECT_VLV(port))
299 			continue;
300 
301 		if (!check(dev_priv, pipe))
302 			continue;
303 
304 		return pipe;
305 	}
306 
307 	return INVALID_PIPE;
308 }
309 
310 static void
311 vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
312 {
313 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
314 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
315 	enum port port = dig_port->base.port;
316 
317 	lockdep_assert_held(&dev_priv->display.pps.mutex);
318 
319 	/* try to find a pipe with this port selected */
320 	/* first pick one where the panel is on */
321 	intel_dp->pps.pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
322 						      pps_has_pp_on);
323 	/* didn't find one? pick one where vdd is on */
324 	if (intel_dp->pps.pps_pipe == INVALID_PIPE)
325 		intel_dp->pps.pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
326 							      pps_has_vdd_on);
327 	/* didn't find one? pick one with just the correct port */
328 	if (intel_dp->pps.pps_pipe == INVALID_PIPE)
329 		intel_dp->pps.pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
330 							      pps_any);
331 
332 	/* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
333 	if (intel_dp->pps.pps_pipe == INVALID_PIPE) {
334 		drm_dbg_kms(&dev_priv->drm,
335 			    "[ENCODER:%d:%s] no initial power sequencer\n",
336 			    dig_port->base.base.base.id, dig_port->base.base.name);
337 		return;
338 	}
339 
340 	drm_dbg_kms(&dev_priv->drm,
341 		    "[ENCODER:%d:%s] initial power sequencer: %s\n",
342 		    dig_port->base.base.base.id, dig_port->base.base.name,
343 		    pps_name(dev_priv, &intel_dp->pps));
344 }
345 
346 static int intel_num_pps(struct drm_i915_private *i915)
347 {
348 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
349 		return 2;
350 
351 	if (IS_GEMINILAKE(i915) || IS_BROXTON(i915))
352 		return 2;
353 
354 	if (INTEL_PCH_TYPE(i915) >= PCH_MTL)
355 		return 2;
356 
357 	if (INTEL_PCH_TYPE(i915) >= PCH_DG1)
358 		return 1;
359 
360 	if (INTEL_PCH_TYPE(i915) >= PCH_ICP)
361 		return 2;
362 
363 	return 1;
364 }
365 
366 static bool intel_pps_is_valid(struct intel_dp *intel_dp)
367 {
368 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
369 
370 	if (intel_dp->pps.pps_idx == 1 &&
371 	    INTEL_PCH_TYPE(i915) >= PCH_ICP &&
372 	    INTEL_PCH_TYPE(i915) <= PCH_ADP)
373 		return intel_de_read(i915, SOUTH_CHICKEN1) & ICP_SECOND_PPS_IO_SELECT;
374 
375 	return true;
376 }
377 
378 static int
379 bxt_initial_pps_idx(struct drm_i915_private *i915, pps_check check)
380 {
381 	int pps_idx, pps_num = intel_num_pps(i915);
382 
383 	for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
384 		if (check(i915, pps_idx))
385 			return pps_idx;
386 	}
387 
388 	return -1;
389 }
390 
391 static bool
392 pps_initial_setup(struct intel_dp *intel_dp)
393 {
394 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
395 	struct intel_connector *connector = intel_dp->attached_connector;
396 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
397 
398 	lockdep_assert_held(&i915->display.pps.mutex);
399 
400 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
401 		vlv_initial_power_sequencer_setup(intel_dp);
402 		return true;
403 	}
404 
405 	/* first ask the VBT */
406 	if (intel_num_pps(i915) > 1)
407 		intel_dp->pps.pps_idx = connector->panel.vbt.backlight.controller;
408 	else
409 		intel_dp->pps.pps_idx = 0;
410 
411 	if (drm_WARN_ON(&i915->drm, intel_dp->pps.pps_idx >= intel_num_pps(i915)))
412 		intel_dp->pps.pps_idx = -1;
413 
414 	/* VBT wasn't parsed yet? pick one where the panel is on */
415 	if (intel_dp->pps.pps_idx < 0)
416 		intel_dp->pps.pps_idx = bxt_initial_pps_idx(i915, pps_has_pp_on);
417 	/* didn't find one? pick one where vdd is on */
418 	if (intel_dp->pps.pps_idx < 0)
419 		intel_dp->pps.pps_idx = bxt_initial_pps_idx(i915, pps_has_vdd_on);
420 	/* didn't find one? pick any */
421 	if (intel_dp->pps.pps_idx < 0) {
422 		intel_dp->pps.pps_idx = bxt_initial_pps_idx(i915, pps_any);
423 
424 		drm_dbg_kms(&i915->drm,
425 			    "[ENCODER:%d:%s] no initial power sequencer, assuming %s\n",
426 			    encoder->base.base.id, encoder->base.name,
427 			    pps_name(i915, &intel_dp->pps));
428 	} else {
429 		drm_dbg_kms(&i915->drm,
430 			    "[ENCODER:%d:%s] initial power sequencer: %s\n",
431 			    encoder->base.base.id, encoder->base.name,
432 			    pps_name(i915, &intel_dp->pps));
433 	}
434 
435 	return intel_pps_is_valid(intel_dp);
436 }
437 
438 void intel_pps_reset_all(struct drm_i915_private *dev_priv)
439 {
440 	struct intel_encoder *encoder;
441 
442 	if (drm_WARN_ON(&dev_priv->drm, !IS_LP(dev_priv)))
443 		return;
444 
445 	if (!HAS_DISPLAY(dev_priv))
446 		return;
447 
448 	/*
449 	 * We can't grab pps_mutex here due to deadlock with power_domain
450 	 * mutex when power_domain functions are called while holding pps_mutex.
451 	 * That also means that in order to use pps_pipe the code needs to
452 	 * hold both a power domain reference and pps_mutex, and the power domain
453 	 * reference get/put must be done while _not_ holding pps_mutex.
454 	 * pps_{lock,unlock}() do these steps in the correct order, so one
455 	 * should use them always.
456 	 */
457 
458 	for_each_intel_dp(&dev_priv->drm, encoder) {
459 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
460 
461 		drm_WARN_ON(&dev_priv->drm,
462 			    intel_dp->pps.active_pipe != INVALID_PIPE);
463 
464 		if (encoder->type != INTEL_OUTPUT_EDP)
465 			continue;
466 
467 		if (DISPLAY_VER(dev_priv) >= 9)
468 			intel_dp->pps.pps_reset = true;
469 		else
470 			intel_dp->pps.pps_pipe = INVALID_PIPE;
471 	}
472 }
473 
474 struct pps_registers {
475 	i915_reg_t pp_ctrl;
476 	i915_reg_t pp_stat;
477 	i915_reg_t pp_on;
478 	i915_reg_t pp_off;
479 	i915_reg_t pp_div;
480 };
481 
482 static void intel_pps_get_registers(struct intel_dp *intel_dp,
483 				    struct pps_registers *regs)
484 {
485 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
486 	int pps_idx;
487 
488 	memset(regs, 0, sizeof(*regs));
489 
490 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
491 		pps_idx = vlv_power_sequencer_pipe(intel_dp);
492 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
493 		pps_idx = bxt_power_sequencer_idx(intel_dp);
494 	else
495 		pps_idx = intel_dp->pps.pps_idx;
496 
497 	regs->pp_ctrl = PP_CONTROL(dev_priv, pps_idx);
498 	regs->pp_stat = PP_STATUS(dev_priv, pps_idx);
499 	regs->pp_on = PP_ON_DELAYS(dev_priv, pps_idx);
500 	regs->pp_off = PP_OFF_DELAYS(dev_priv, pps_idx);
501 
502 	/* Cycle delay moved from PP_DIVISOR to PP_CONTROL */
503 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv) ||
504 	    INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
505 		regs->pp_div = INVALID_MMIO_REG;
506 	else
507 		regs->pp_div = PP_DIVISOR(dev_priv, pps_idx);
508 }
509 
510 static i915_reg_t
511 _pp_ctrl_reg(struct intel_dp *intel_dp)
512 {
513 	struct pps_registers regs;
514 
515 	intel_pps_get_registers(intel_dp, &regs);
516 
517 	return regs.pp_ctrl;
518 }
519 
520 static i915_reg_t
521 _pp_stat_reg(struct intel_dp *intel_dp)
522 {
523 	struct pps_registers regs;
524 
525 	intel_pps_get_registers(intel_dp, &regs);
526 
527 	return regs.pp_stat;
528 }
529 
530 static bool edp_have_panel_power(struct intel_dp *intel_dp)
531 {
532 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
533 
534 	lockdep_assert_held(&dev_priv->display.pps.mutex);
535 
536 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
537 	    intel_dp->pps.pps_pipe == INVALID_PIPE)
538 		return false;
539 
540 	return (intel_de_read(dev_priv, _pp_stat_reg(intel_dp)) & PP_ON) != 0;
541 }
542 
543 static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
544 {
545 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
546 
547 	lockdep_assert_held(&dev_priv->display.pps.mutex);
548 
549 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
550 	    intel_dp->pps.pps_pipe == INVALID_PIPE)
551 		return false;
552 
553 	return intel_de_read(dev_priv, _pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
554 }
555 
556 void intel_pps_check_power_unlocked(struct intel_dp *intel_dp)
557 {
558 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
559 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
560 
561 	if (!intel_dp_is_edp(intel_dp))
562 		return;
563 
564 	if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
565 		drm_WARN(&dev_priv->drm, 1,
566 			 "[ENCODER:%d:%s] %s powered off while attempting AUX CH communication.\n",
567 			 dig_port->base.base.base.id, dig_port->base.base.name,
568 			 pps_name(dev_priv, &intel_dp->pps));
569 		drm_dbg_kms(&dev_priv->drm,
570 			    "[ENCODER:%d:%s] %s PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
571 			    dig_port->base.base.base.id, dig_port->base.base.name,
572 			    pps_name(dev_priv, &intel_dp->pps),
573 			    intel_de_read(dev_priv, _pp_stat_reg(intel_dp)),
574 			    intel_de_read(dev_priv, _pp_ctrl_reg(intel_dp)));
575 	}
576 }
577 
578 #define IDLE_ON_MASK		(PP_ON | PP_SEQUENCE_MASK | 0                     | PP_SEQUENCE_STATE_MASK)
579 #define IDLE_ON_VALUE		(PP_ON | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_ON_IDLE)
580 
581 #define IDLE_OFF_MASK		(PP_ON | PP_SEQUENCE_MASK | 0                     | 0)
582 #define IDLE_OFF_VALUE		(0     | PP_SEQUENCE_NONE | 0                     | 0)
583 
584 #define IDLE_CYCLE_MASK		(PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
585 #define IDLE_CYCLE_VALUE	(0     | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_OFF_IDLE)
586 
587 static void intel_pps_verify_state(struct intel_dp *intel_dp);
588 
589 static void wait_panel_status(struct intel_dp *intel_dp,
590 			      u32 mask, u32 value)
591 {
592 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
593 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
594 	i915_reg_t pp_stat_reg, pp_ctrl_reg;
595 
596 	lockdep_assert_held(&dev_priv->display.pps.mutex);
597 
598 	intel_pps_verify_state(intel_dp);
599 
600 	pp_stat_reg = _pp_stat_reg(intel_dp);
601 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
602 
603 	drm_dbg_kms(&dev_priv->drm,
604 		    "[ENCODER:%d:%s] %s mask: 0x%08x value: 0x%08x PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
605 		    dig_port->base.base.base.id, dig_port->base.base.name,
606 		    pps_name(dev_priv, &intel_dp->pps),
607 		    mask, value,
608 		    intel_de_read(dev_priv, pp_stat_reg),
609 		    intel_de_read(dev_priv, pp_ctrl_reg));
610 
611 	if (intel_de_wait(dev_priv, pp_stat_reg, mask, value, 5000))
612 		drm_err(&dev_priv->drm,
613 			"[ENCODER:%d:%s] %s panel status timeout: PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
614 			dig_port->base.base.base.id, dig_port->base.base.name,
615 			pps_name(dev_priv, &intel_dp->pps),
616 			intel_de_read(dev_priv, pp_stat_reg),
617 			intel_de_read(dev_priv, pp_ctrl_reg));
618 
619 	drm_dbg_kms(&dev_priv->drm, "Wait complete\n");
620 }
621 
622 static void wait_panel_on(struct intel_dp *intel_dp)
623 {
624 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
625 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
626 
627 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] %s wait for panel power on\n",
628 		    dig_port->base.base.base.id, dig_port->base.base.name,
629 		    pps_name(i915, &intel_dp->pps));
630 	wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
631 }
632 
633 static void wait_panel_off(struct intel_dp *intel_dp)
634 {
635 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
636 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
637 
638 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] %s wait for panel power off time\n",
639 		    dig_port->base.base.base.id, dig_port->base.base.name,
640 		    pps_name(i915, &intel_dp->pps));
641 	wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
642 }
643 
644 static void wait_panel_power_cycle(struct intel_dp *intel_dp)
645 {
646 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
647 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
648 	ktime_t panel_power_on_time;
649 	s64 panel_power_off_duration;
650 
651 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] %s wait for panel power cycle\n",
652 		    dig_port->base.base.base.id, dig_port->base.base.name,
653 		    pps_name(i915, &intel_dp->pps));
654 
655 	/* take the difference of current time and panel power off time
656 	 * and then make panel wait for t11_t12 if needed. */
657 	panel_power_on_time = ktime_get_boottime();
658 	panel_power_off_duration = ktime_ms_delta(panel_power_on_time, intel_dp->pps.panel_power_off_time);
659 
660 	/* When we disable the VDD override bit last we have to do the manual
661 	 * wait. */
662 	if (panel_power_off_duration < (s64)intel_dp->pps.panel_power_cycle_delay)
663 		wait_remaining_ms_from_jiffies(jiffies,
664 				       intel_dp->pps.panel_power_cycle_delay - panel_power_off_duration);
665 
666 	wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
667 }
668 
669 void intel_pps_wait_power_cycle(struct intel_dp *intel_dp)
670 {
671 	intel_wakeref_t wakeref;
672 
673 	if (!intel_dp_is_edp(intel_dp))
674 		return;
675 
676 	with_intel_pps_lock(intel_dp, wakeref)
677 		wait_panel_power_cycle(intel_dp);
678 }
679 
680 static void wait_backlight_on(struct intel_dp *intel_dp)
681 {
682 	wait_remaining_ms_from_jiffies(intel_dp->pps.last_power_on,
683 				       intel_dp->pps.backlight_on_delay);
684 }
685 
686 static void edp_wait_backlight_off(struct intel_dp *intel_dp)
687 {
688 	wait_remaining_ms_from_jiffies(intel_dp->pps.last_backlight_off,
689 				       intel_dp->pps.backlight_off_delay);
690 }
691 
692 /* Read the current pp_control value, unlocking the register if it
693  * is locked
694  */
695 
696 static  u32 ilk_get_pp_control(struct intel_dp *intel_dp)
697 {
698 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
699 	u32 control;
700 
701 	lockdep_assert_held(&dev_priv->display.pps.mutex);
702 
703 	control = intel_de_read(dev_priv, _pp_ctrl_reg(intel_dp));
704 	if (drm_WARN_ON(&dev_priv->drm, !HAS_DDI(dev_priv) &&
705 			(control & PANEL_UNLOCK_MASK) != PANEL_UNLOCK_REGS)) {
706 		control &= ~PANEL_UNLOCK_MASK;
707 		control |= PANEL_UNLOCK_REGS;
708 	}
709 	return control;
710 }
711 
712 /*
713  * Must be paired with intel_pps_vdd_off_unlocked().
714  * Must hold pps_mutex around the whole on/off sequence.
715  * Can be nested with intel_pps_vdd_{on,off}() calls.
716  */
717 bool intel_pps_vdd_on_unlocked(struct intel_dp *intel_dp)
718 {
719 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
720 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
721 	u32 pp;
722 	i915_reg_t pp_stat_reg, pp_ctrl_reg;
723 	bool need_to_disable = !intel_dp->pps.want_panel_vdd;
724 
725 	lockdep_assert_held(&dev_priv->display.pps.mutex);
726 
727 	if (!intel_dp_is_edp(intel_dp))
728 		return false;
729 
730 	cancel_delayed_work(&intel_dp->pps.panel_vdd_work);
731 	intel_dp->pps.want_panel_vdd = true;
732 
733 	if (edp_have_panel_vdd(intel_dp))
734 		return need_to_disable;
735 
736 	drm_WARN_ON(&dev_priv->drm, intel_dp->pps.vdd_wakeref);
737 	intel_dp->pps.vdd_wakeref = intel_display_power_get(dev_priv,
738 							    intel_aux_power_domain(dig_port));
739 
740 	pp_stat_reg = _pp_stat_reg(intel_dp);
741 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
742 
743 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] %s turning VDD on\n",
744 		    dig_port->base.base.base.id, dig_port->base.base.name,
745 		    pps_name(dev_priv, &intel_dp->pps));
746 
747 	if (!edp_have_panel_power(intel_dp))
748 		wait_panel_power_cycle(intel_dp);
749 
750 	pp = ilk_get_pp_control(intel_dp);
751 	pp |= EDP_FORCE_VDD;
752 
753 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
754 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
755 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] %s PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
756 		    dig_port->base.base.base.id, dig_port->base.base.name,
757 		    pps_name(dev_priv, &intel_dp->pps),
758 		    intel_de_read(dev_priv, pp_stat_reg),
759 		    intel_de_read(dev_priv, pp_ctrl_reg));
760 	/*
761 	 * If the panel wasn't on, delay before accessing aux channel
762 	 */
763 	if (!edp_have_panel_power(intel_dp)) {
764 		drm_dbg_kms(&dev_priv->drm,
765 			    "[ENCODER:%d:%s] %s panel power wasn't enabled\n",
766 			    dig_port->base.base.base.id, dig_port->base.base.name,
767 			    pps_name(dev_priv, &intel_dp->pps));
768 		msleep(intel_dp->pps.panel_power_up_delay);
769 	}
770 
771 	return need_to_disable;
772 }
773 
774 /*
775  * Must be paired with intel_pps_off().
776  * Nested calls to these functions are not allowed since
777  * we drop the lock. Caller must use some higher level
778  * locking to prevent nested calls from other threads.
779  */
780 void intel_pps_vdd_on(struct intel_dp *intel_dp)
781 {
782 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
783 	intel_wakeref_t wakeref;
784 	bool vdd;
785 
786 	if (!intel_dp_is_edp(intel_dp))
787 		return;
788 
789 	vdd = false;
790 	with_intel_pps_lock(intel_dp, wakeref)
791 		vdd = intel_pps_vdd_on_unlocked(intel_dp);
792 	I915_STATE_WARN(i915, !vdd, "[ENCODER:%d:%s] %s VDD already requested on\n",
793 			dp_to_dig_port(intel_dp)->base.base.base.id,
794 			dp_to_dig_port(intel_dp)->base.base.name,
795 			pps_name(i915, &intel_dp->pps));
796 }
797 
798 static void intel_pps_vdd_off_sync_unlocked(struct intel_dp *intel_dp)
799 {
800 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
801 	struct intel_digital_port *dig_port =
802 		dp_to_dig_port(intel_dp);
803 	u32 pp;
804 	i915_reg_t pp_stat_reg, pp_ctrl_reg;
805 
806 	lockdep_assert_held(&dev_priv->display.pps.mutex);
807 
808 	drm_WARN_ON(&dev_priv->drm, intel_dp->pps.want_panel_vdd);
809 
810 	if (!edp_have_panel_vdd(intel_dp))
811 		return;
812 
813 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] %s turning VDD off\n",
814 		    dig_port->base.base.base.id, dig_port->base.base.name,
815 		    pps_name(dev_priv, &intel_dp->pps));
816 
817 	pp = ilk_get_pp_control(intel_dp);
818 	pp &= ~EDP_FORCE_VDD;
819 
820 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
821 	pp_stat_reg = _pp_stat_reg(intel_dp);
822 
823 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
824 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
825 
826 	/* Make sure sequencer is idle before allowing subsequent activity */
827 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] %s PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
828 		    dig_port->base.base.base.id, dig_port->base.base.name,
829 		    pps_name(dev_priv, &intel_dp->pps),
830 		    intel_de_read(dev_priv, pp_stat_reg),
831 		    intel_de_read(dev_priv, pp_ctrl_reg));
832 
833 	if ((pp & PANEL_POWER_ON) == 0)
834 		intel_dp->pps.panel_power_off_time = ktime_get_boottime();
835 
836 	intel_display_power_put(dev_priv,
837 				intel_aux_power_domain(dig_port),
838 				fetch_and_zero(&intel_dp->pps.vdd_wakeref));
839 }
840 
841 void intel_pps_vdd_off_sync(struct intel_dp *intel_dp)
842 {
843 	intel_wakeref_t wakeref;
844 
845 	if (!intel_dp_is_edp(intel_dp))
846 		return;
847 
848 	cancel_delayed_work_sync(&intel_dp->pps.panel_vdd_work);
849 	/*
850 	 * vdd might still be enabled due to the delayed vdd off.
851 	 * Make sure vdd is actually turned off here.
852 	 */
853 	with_intel_pps_lock(intel_dp, wakeref)
854 		intel_pps_vdd_off_sync_unlocked(intel_dp);
855 }
856 
857 static void edp_panel_vdd_work(struct work_struct *__work)
858 {
859 	struct intel_pps *pps = container_of(to_delayed_work(__work),
860 					     struct intel_pps, panel_vdd_work);
861 	struct intel_dp *intel_dp = container_of(pps, struct intel_dp, pps);
862 	intel_wakeref_t wakeref;
863 
864 	with_intel_pps_lock(intel_dp, wakeref) {
865 		if (!intel_dp->pps.want_panel_vdd)
866 			intel_pps_vdd_off_sync_unlocked(intel_dp);
867 	}
868 }
869 
870 static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
871 {
872 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
873 	unsigned long delay;
874 
875 	/*
876 	 * We may not yet know the real power sequencing delays,
877 	 * so keep VDD enabled until we're done with init.
878 	 */
879 	if (intel_dp->pps.initializing)
880 		return;
881 
882 	/*
883 	 * Queue the timer to fire a long time from now (relative to the power
884 	 * down delay) to keep the panel power up across a sequence of
885 	 * operations.
886 	 */
887 	delay = msecs_to_jiffies(intel_dp->pps.panel_power_cycle_delay * 5);
888 	queue_delayed_work(i915->unordered_wq,
889 			   &intel_dp->pps.panel_vdd_work, delay);
890 }
891 
892 /*
893  * Must be paired with edp_panel_vdd_on().
894  * Must hold pps_mutex around the whole on/off sequence.
895  * Can be nested with intel_pps_vdd_{on,off}() calls.
896  */
897 void intel_pps_vdd_off_unlocked(struct intel_dp *intel_dp, bool sync)
898 {
899 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
900 
901 	lockdep_assert_held(&dev_priv->display.pps.mutex);
902 
903 	if (!intel_dp_is_edp(intel_dp))
904 		return;
905 
906 	I915_STATE_WARN(dev_priv, !intel_dp->pps.want_panel_vdd,
907 			"[ENCODER:%d:%s] %s VDD not forced on",
908 			dp_to_dig_port(intel_dp)->base.base.base.id,
909 			dp_to_dig_port(intel_dp)->base.base.name,
910 			pps_name(dev_priv, &intel_dp->pps));
911 
912 	intel_dp->pps.want_panel_vdd = false;
913 
914 	if (sync)
915 		intel_pps_vdd_off_sync_unlocked(intel_dp);
916 	else
917 		edp_panel_vdd_schedule_off(intel_dp);
918 }
919 
920 void intel_pps_on_unlocked(struct intel_dp *intel_dp)
921 {
922 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
923 	u32 pp;
924 	i915_reg_t pp_ctrl_reg;
925 
926 	lockdep_assert_held(&dev_priv->display.pps.mutex);
927 
928 	if (!intel_dp_is_edp(intel_dp))
929 		return;
930 
931 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] %s turn panel power on\n",
932 		    dp_to_dig_port(intel_dp)->base.base.base.id,
933 		    dp_to_dig_port(intel_dp)->base.base.name,
934 		    pps_name(dev_priv, &intel_dp->pps));
935 
936 	if (drm_WARN(&dev_priv->drm, edp_have_panel_power(intel_dp),
937 		     "[ENCODER:%d:%s] %s panel power already on\n",
938 		     dp_to_dig_port(intel_dp)->base.base.base.id,
939 		     dp_to_dig_port(intel_dp)->base.base.name,
940 		     pps_name(dev_priv, &intel_dp->pps)))
941 		return;
942 
943 	wait_panel_power_cycle(intel_dp);
944 
945 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
946 	pp = ilk_get_pp_control(intel_dp);
947 	if (IS_IRONLAKE(dev_priv)) {
948 		/* ILK workaround: disable reset around power sequence */
949 		pp &= ~PANEL_POWER_RESET;
950 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
951 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
952 	}
953 
954 	pp |= PANEL_POWER_ON;
955 	if (!IS_IRONLAKE(dev_priv))
956 		pp |= PANEL_POWER_RESET;
957 
958 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
959 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
960 
961 	wait_panel_on(intel_dp);
962 	intel_dp->pps.last_power_on = jiffies;
963 
964 	if (IS_IRONLAKE(dev_priv)) {
965 		pp |= PANEL_POWER_RESET; /* restore panel reset bit */
966 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
967 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
968 	}
969 }
970 
971 void intel_pps_on(struct intel_dp *intel_dp)
972 {
973 	intel_wakeref_t wakeref;
974 
975 	if (!intel_dp_is_edp(intel_dp))
976 		return;
977 
978 	with_intel_pps_lock(intel_dp, wakeref)
979 		intel_pps_on_unlocked(intel_dp);
980 }
981 
982 void intel_pps_off_unlocked(struct intel_dp *intel_dp)
983 {
984 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
985 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
986 	u32 pp;
987 	i915_reg_t pp_ctrl_reg;
988 
989 	lockdep_assert_held(&dev_priv->display.pps.mutex);
990 
991 	if (!intel_dp_is_edp(intel_dp))
992 		return;
993 
994 	drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s] %s turn panel power off\n",
995 		    dig_port->base.base.base.id, dig_port->base.base.name,
996 		    pps_name(dev_priv, &intel_dp->pps));
997 
998 	drm_WARN(&dev_priv->drm, !intel_dp->pps.want_panel_vdd,
999 		 "[ENCODER:%d:%s] %s need VDD to turn off panel\n",
1000 		 dig_port->base.base.base.id, dig_port->base.base.name,
1001 		 pps_name(dev_priv, &intel_dp->pps));
1002 
1003 	pp = ilk_get_pp_control(intel_dp);
1004 	/* We need to switch off panel power _and_ force vdd, for otherwise some
1005 	 * panels get very unhappy and cease to work. */
1006 	pp &= ~(PANEL_POWER_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
1007 		EDP_BLC_ENABLE);
1008 
1009 	pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
1010 
1011 	intel_dp->pps.want_panel_vdd = false;
1012 
1013 	intel_de_write(dev_priv, pp_ctrl_reg, pp);
1014 	intel_de_posting_read(dev_priv, pp_ctrl_reg);
1015 
1016 	wait_panel_off(intel_dp);
1017 	intel_dp->pps.panel_power_off_time = ktime_get_boottime();
1018 
1019 	/* We got a reference when we enabled the VDD. */
1020 	intel_display_power_put(dev_priv,
1021 				intel_aux_power_domain(dig_port),
1022 				fetch_and_zero(&intel_dp->pps.vdd_wakeref));
1023 }
1024 
1025 void intel_pps_off(struct intel_dp *intel_dp)
1026 {
1027 	intel_wakeref_t wakeref;
1028 
1029 	if (!intel_dp_is_edp(intel_dp))
1030 		return;
1031 
1032 	with_intel_pps_lock(intel_dp, wakeref)
1033 		intel_pps_off_unlocked(intel_dp);
1034 }
1035 
1036 /* Enable backlight in the panel power control. */
1037 void intel_pps_backlight_on(struct intel_dp *intel_dp)
1038 {
1039 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1040 	intel_wakeref_t wakeref;
1041 
1042 	/*
1043 	 * If we enable the backlight right away following a panel power
1044 	 * on, we may see slight flicker as the panel syncs with the eDP
1045 	 * link.  So delay a bit to make sure the image is solid before
1046 	 * allowing it to appear.
1047 	 */
1048 	wait_backlight_on(intel_dp);
1049 
1050 	with_intel_pps_lock(intel_dp, wakeref) {
1051 		i915_reg_t pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
1052 		u32 pp;
1053 
1054 		pp = ilk_get_pp_control(intel_dp);
1055 		pp |= EDP_BLC_ENABLE;
1056 
1057 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
1058 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
1059 	}
1060 }
1061 
1062 /* Disable backlight in the panel power control. */
1063 void intel_pps_backlight_off(struct intel_dp *intel_dp)
1064 {
1065 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1066 	intel_wakeref_t wakeref;
1067 
1068 	if (!intel_dp_is_edp(intel_dp))
1069 		return;
1070 
1071 	with_intel_pps_lock(intel_dp, wakeref) {
1072 		i915_reg_t pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
1073 		u32 pp;
1074 
1075 		pp = ilk_get_pp_control(intel_dp);
1076 		pp &= ~EDP_BLC_ENABLE;
1077 
1078 		intel_de_write(dev_priv, pp_ctrl_reg, pp);
1079 		intel_de_posting_read(dev_priv, pp_ctrl_reg);
1080 	}
1081 
1082 	intel_dp->pps.last_backlight_off = jiffies;
1083 	edp_wait_backlight_off(intel_dp);
1084 }
1085 
1086 /*
1087  * Hook for controlling the panel power control backlight through the bl_power
1088  * sysfs attribute. Take care to handle multiple calls.
1089  */
1090 void intel_pps_backlight_power(struct intel_connector *connector, bool enable)
1091 {
1092 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
1093 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1094 	intel_wakeref_t wakeref;
1095 	bool is_enabled;
1096 
1097 	is_enabled = false;
1098 	with_intel_pps_lock(intel_dp, wakeref)
1099 		is_enabled = ilk_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
1100 	if (is_enabled == enable)
1101 		return;
1102 
1103 	drm_dbg_kms(&i915->drm, "panel power control backlight %s\n",
1104 		    enable ? "enable" : "disable");
1105 
1106 	if (enable)
1107 		intel_pps_backlight_on(intel_dp);
1108 	else
1109 		intel_pps_backlight_off(intel_dp);
1110 }
1111 
1112 static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
1113 {
1114 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1115 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
1116 	enum pipe pipe = intel_dp->pps.pps_pipe;
1117 	i915_reg_t pp_on_reg = PP_ON_DELAYS(dev_priv, pipe);
1118 
1119 	drm_WARN_ON(&dev_priv->drm, intel_dp->pps.active_pipe != INVALID_PIPE);
1120 
1121 	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1122 		return;
1123 
1124 	intel_pps_vdd_off_sync_unlocked(intel_dp);
1125 
1126 	/*
1127 	 * VLV seems to get confused when multiple power sequencers
1128 	 * have the same port selected (even if only one has power/vdd
1129 	 * enabled). The failure manifests as vlv_wait_port_ready() failing
1130 	 * CHV on the other hand doesn't seem to mind having the same port
1131 	 * selected in multiple power sequencers, but let's clear the
1132 	 * port select always when logically disconnecting a power sequencer
1133 	 * from a port.
1134 	 */
1135 	drm_dbg_kms(&dev_priv->drm,
1136 		    "detaching %s from [ENCODER:%d:%s]\n",
1137 		    pps_name(dev_priv, &intel_dp->pps),
1138 		    dig_port->base.base.base.id, dig_port->base.base.name);
1139 	intel_de_write(dev_priv, pp_on_reg, 0);
1140 	intel_de_posting_read(dev_priv, pp_on_reg);
1141 
1142 	intel_dp->pps.pps_pipe = INVALID_PIPE;
1143 }
1144 
1145 static void vlv_steal_power_sequencer(struct drm_i915_private *dev_priv,
1146 				      enum pipe pipe)
1147 {
1148 	struct intel_encoder *encoder;
1149 
1150 	lockdep_assert_held(&dev_priv->display.pps.mutex);
1151 
1152 	for_each_intel_dp(&dev_priv->drm, encoder) {
1153 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1154 
1155 		drm_WARN(&dev_priv->drm, intel_dp->pps.active_pipe == pipe,
1156 			 "stealing PPS %c from active [ENCODER:%d:%s]\n",
1157 			 pipe_name(pipe), encoder->base.base.id,
1158 			 encoder->base.name);
1159 
1160 		if (intel_dp->pps.pps_pipe != pipe)
1161 			continue;
1162 
1163 		drm_dbg_kms(&dev_priv->drm,
1164 			    "stealing PPS %c from [ENCODER:%d:%s]\n",
1165 			    pipe_name(pipe), encoder->base.base.id,
1166 			    encoder->base.name);
1167 
1168 		/* make sure vdd is off before we steal it */
1169 		vlv_detach_power_sequencer(intel_dp);
1170 	}
1171 }
1172 
1173 void vlv_pps_init(struct intel_encoder *encoder,
1174 		  const struct intel_crtc_state *crtc_state)
1175 {
1176 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1177 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1178 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1179 
1180 	lockdep_assert_held(&dev_priv->display.pps.mutex);
1181 
1182 	drm_WARN_ON(&dev_priv->drm, intel_dp->pps.active_pipe != INVALID_PIPE);
1183 
1184 	if (intel_dp->pps.pps_pipe != INVALID_PIPE &&
1185 	    intel_dp->pps.pps_pipe != crtc->pipe) {
1186 		/*
1187 		 * If another power sequencer was being used on this
1188 		 * port previously make sure to turn off vdd there while
1189 		 * we still have control of it.
1190 		 */
1191 		vlv_detach_power_sequencer(intel_dp);
1192 	}
1193 
1194 	/*
1195 	 * We may be stealing the power
1196 	 * sequencer from another port.
1197 	 */
1198 	vlv_steal_power_sequencer(dev_priv, crtc->pipe);
1199 
1200 	intel_dp->pps.active_pipe = crtc->pipe;
1201 
1202 	if (!intel_dp_is_edp(intel_dp))
1203 		return;
1204 
1205 	/* now it's all ours */
1206 	intel_dp->pps.pps_pipe = crtc->pipe;
1207 
1208 	drm_dbg_kms(&dev_priv->drm,
1209 		    "initializing %s for [ENCODER:%d:%s]\n",
1210 		    pps_name(dev_priv, &intel_dp->pps),
1211 		    encoder->base.base.id, encoder->base.name);
1212 
1213 	/* init power sequencer on this pipe and port */
1214 	pps_init_delays(intel_dp);
1215 	pps_init_registers(intel_dp, true);
1216 }
1217 
1218 static void pps_vdd_init(struct intel_dp *intel_dp)
1219 {
1220 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1221 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1222 
1223 	lockdep_assert_held(&dev_priv->display.pps.mutex);
1224 
1225 	if (!edp_have_panel_vdd(intel_dp))
1226 		return;
1227 
1228 	/*
1229 	 * The VDD bit needs a power domain reference, so if the bit is
1230 	 * already enabled when we boot or resume, grab this reference and
1231 	 * schedule a vdd off, so we don't hold on to the reference
1232 	 * indefinitely.
1233 	 */
1234 	drm_dbg_kms(&dev_priv->drm,
1235 		    "[ENCODER:%d:%s] %s VDD left on by BIOS, adjusting state tracking\n",
1236 		    dig_port->base.base.base.id, dig_port->base.base.name,
1237 		    pps_name(dev_priv, &intel_dp->pps));
1238 	drm_WARN_ON(&dev_priv->drm, intel_dp->pps.vdd_wakeref);
1239 	intel_dp->pps.vdd_wakeref = intel_display_power_get(dev_priv,
1240 							    intel_aux_power_domain(dig_port));
1241 }
1242 
1243 bool intel_pps_have_panel_power_or_vdd(struct intel_dp *intel_dp)
1244 {
1245 	intel_wakeref_t wakeref;
1246 	bool have_power = false;
1247 
1248 	with_intel_pps_lock(intel_dp, wakeref) {
1249 		have_power = edp_have_panel_power(intel_dp) ||
1250 			     edp_have_panel_vdd(intel_dp);
1251 	}
1252 
1253 	return have_power;
1254 }
1255 
1256 static void pps_init_timestamps(struct intel_dp *intel_dp)
1257 {
1258 	/*
1259 	 * Initialize panel power off time to 0, assuming panel power could have
1260 	 * been toggled between kernel boot and now only by a previously loaded
1261 	 * and removed i915, which has already ensured sufficient power off
1262 	 * delay at module remove.
1263 	 */
1264 	intel_dp->pps.panel_power_off_time = 0;
1265 	intel_dp->pps.last_power_on = jiffies;
1266 	intel_dp->pps.last_backlight_off = jiffies;
1267 }
1268 
1269 static void
1270 intel_pps_readout_hw_state(struct intel_dp *intel_dp, struct edp_power_seq *seq)
1271 {
1272 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1273 	u32 pp_on, pp_off, pp_ctl;
1274 	struct pps_registers regs;
1275 
1276 	intel_pps_get_registers(intel_dp, &regs);
1277 
1278 	pp_ctl = ilk_get_pp_control(intel_dp);
1279 
1280 	/* Ensure PPS is unlocked */
1281 	if (!HAS_DDI(dev_priv))
1282 		intel_de_write(dev_priv, regs.pp_ctrl, pp_ctl);
1283 
1284 	pp_on = intel_de_read(dev_priv, regs.pp_on);
1285 	pp_off = intel_de_read(dev_priv, regs.pp_off);
1286 
1287 	/* Pull timing values out of registers */
1288 	seq->t1_t3 = REG_FIELD_GET(PANEL_POWER_UP_DELAY_MASK, pp_on);
1289 	seq->t8 = REG_FIELD_GET(PANEL_LIGHT_ON_DELAY_MASK, pp_on);
1290 	seq->t9 = REG_FIELD_GET(PANEL_LIGHT_OFF_DELAY_MASK, pp_off);
1291 	seq->t10 = REG_FIELD_GET(PANEL_POWER_DOWN_DELAY_MASK, pp_off);
1292 
1293 	if (i915_mmio_reg_valid(regs.pp_div)) {
1294 		u32 pp_div;
1295 
1296 		pp_div = intel_de_read(dev_priv, regs.pp_div);
1297 
1298 		seq->t11_t12 = REG_FIELD_GET(PANEL_POWER_CYCLE_DELAY_MASK, pp_div) * 1000;
1299 	} else {
1300 		seq->t11_t12 = REG_FIELD_GET(BXT_POWER_CYCLE_DELAY_MASK, pp_ctl) * 1000;
1301 	}
1302 }
1303 
1304 static void
1305 intel_pps_dump_state(struct intel_dp *intel_dp, const char *state_name,
1306 		     const struct edp_power_seq *seq)
1307 {
1308 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1309 
1310 	drm_dbg_kms(&i915->drm, "%s t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
1311 		    state_name,
1312 		    seq->t1_t3, seq->t8, seq->t9, seq->t10, seq->t11_t12);
1313 }
1314 
1315 static void
1316 intel_pps_verify_state(struct intel_dp *intel_dp)
1317 {
1318 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1319 	struct edp_power_seq hw;
1320 	struct edp_power_seq *sw = &intel_dp->pps.pps_delays;
1321 
1322 	intel_pps_readout_hw_state(intel_dp, &hw);
1323 
1324 	if (hw.t1_t3 != sw->t1_t3 || hw.t8 != sw->t8 || hw.t9 != sw->t9 ||
1325 	    hw.t10 != sw->t10 || hw.t11_t12 != sw->t11_t12) {
1326 		drm_err(&i915->drm, "PPS state mismatch\n");
1327 		intel_pps_dump_state(intel_dp, "sw", sw);
1328 		intel_pps_dump_state(intel_dp, "hw", &hw);
1329 	}
1330 }
1331 
1332 static bool pps_delays_valid(struct edp_power_seq *delays)
1333 {
1334 	return delays->t1_t3 || delays->t8 || delays->t9 ||
1335 		delays->t10 || delays->t11_t12;
1336 }
1337 
1338 static void pps_init_delays_bios(struct intel_dp *intel_dp,
1339 				 struct edp_power_seq *bios)
1340 {
1341 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1342 
1343 	lockdep_assert_held(&dev_priv->display.pps.mutex);
1344 
1345 	if (!pps_delays_valid(&intel_dp->pps.bios_pps_delays))
1346 		intel_pps_readout_hw_state(intel_dp, &intel_dp->pps.bios_pps_delays);
1347 
1348 	*bios = intel_dp->pps.bios_pps_delays;
1349 
1350 	intel_pps_dump_state(intel_dp, "bios", bios);
1351 }
1352 
1353 static void pps_init_delays_vbt(struct intel_dp *intel_dp,
1354 				struct edp_power_seq *vbt)
1355 {
1356 	struct intel_display *display = to_intel_display(intel_dp);
1357 	struct intel_connector *connector = intel_dp->attached_connector;
1358 
1359 	*vbt = connector->panel.vbt.edp.pps;
1360 
1361 	if (!pps_delays_valid(vbt))
1362 		return;
1363 
1364 	/* On Toshiba Satellite P50-C-18C system the VBT T12 delay
1365 	 * of 500ms appears to be too short. Ocassionally the panel
1366 	 * just fails to power back on. Increasing the delay to 800ms
1367 	 * seems sufficient to avoid this problem.
1368 	 */
1369 	if (intel_has_quirk(display, QUIRK_INCREASE_T12_DELAY)) {
1370 		vbt->t11_t12 = max_t(u16, vbt->t11_t12, 1300 * 10);
1371 		drm_dbg_kms(display->drm,
1372 			    "Increasing T12 panel delay as per the quirk to %d\n",
1373 			    vbt->t11_t12);
1374 	}
1375 
1376 	/* T11_T12 delay is special and actually in units of 100ms, but zero
1377 	 * based in the hw (so we need to add 100 ms). But the sw vbt
1378 	 * table multiplies it with 1000 to make it in units of 100usec,
1379 	 * too. */
1380 	vbt->t11_t12 += 100 * 10;
1381 
1382 	intel_pps_dump_state(intel_dp, "vbt", vbt);
1383 }
1384 
1385 static void pps_init_delays_spec(struct intel_dp *intel_dp,
1386 				 struct edp_power_seq *spec)
1387 {
1388 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1389 
1390 	lockdep_assert_held(&dev_priv->display.pps.mutex);
1391 
1392 	/* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
1393 	 * our hw here, which are all in 100usec. */
1394 	spec->t1_t3 = 210 * 10;
1395 	spec->t8 = 50 * 10; /* no limit for t8, use t7 instead */
1396 	spec->t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
1397 	spec->t10 = 500 * 10;
1398 	/* This one is special and actually in units of 100ms, but zero
1399 	 * based in the hw (so we need to add 100 ms). But the sw vbt
1400 	 * table multiplies it with 1000 to make it in units of 100usec,
1401 	 * too. */
1402 	spec->t11_t12 = (510 + 100) * 10;
1403 
1404 	intel_pps_dump_state(intel_dp, "spec", spec);
1405 }
1406 
1407 static void pps_init_delays(struct intel_dp *intel_dp)
1408 {
1409 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1410 	struct edp_power_seq cur, vbt, spec,
1411 		*final = &intel_dp->pps.pps_delays;
1412 
1413 	lockdep_assert_held(&dev_priv->display.pps.mutex);
1414 
1415 	/* already initialized? */
1416 	if (pps_delays_valid(final))
1417 		return;
1418 
1419 	pps_init_delays_bios(intel_dp, &cur);
1420 	pps_init_delays_vbt(intel_dp, &vbt);
1421 	pps_init_delays_spec(intel_dp, &spec);
1422 
1423 	/* Use the max of the register settings and vbt. If both are
1424 	 * unset, fall back to the spec limits. */
1425 #define assign_final(field)	final->field = (max(cur.field, vbt.field) == 0 ? \
1426 				       spec.field : \
1427 				       max(cur.field, vbt.field))
1428 	assign_final(t1_t3);
1429 	assign_final(t8);
1430 	assign_final(t9);
1431 	assign_final(t10);
1432 	assign_final(t11_t12);
1433 #undef assign_final
1434 
1435 #define get_delay(field)	(DIV_ROUND_UP(final->field, 10))
1436 	intel_dp->pps.panel_power_up_delay = get_delay(t1_t3);
1437 	intel_dp->pps.backlight_on_delay = get_delay(t8);
1438 	intel_dp->pps.backlight_off_delay = get_delay(t9);
1439 	intel_dp->pps.panel_power_down_delay = get_delay(t10);
1440 	intel_dp->pps.panel_power_cycle_delay = get_delay(t11_t12);
1441 #undef get_delay
1442 
1443 	drm_dbg_kms(&dev_priv->drm,
1444 		    "panel power up delay %d, power down delay %d, power cycle delay %d\n",
1445 		    intel_dp->pps.panel_power_up_delay,
1446 		    intel_dp->pps.panel_power_down_delay,
1447 		    intel_dp->pps.panel_power_cycle_delay);
1448 
1449 	drm_dbg_kms(&dev_priv->drm, "backlight on delay %d, off delay %d\n",
1450 		    intel_dp->pps.backlight_on_delay,
1451 		    intel_dp->pps.backlight_off_delay);
1452 
1453 	/*
1454 	 * We override the HW backlight delays to 1 because we do manual waits
1455 	 * on them. For T8, even BSpec recommends doing it. For T9, if we
1456 	 * don't do this, we'll end up waiting for the backlight off delay
1457 	 * twice: once when we do the manual sleep, and once when we disable
1458 	 * the panel and wait for the PP_STATUS bit to become zero.
1459 	 */
1460 	final->t8 = 1;
1461 	final->t9 = 1;
1462 
1463 	/*
1464 	 * HW has only a 100msec granularity for t11_t12 so round it up
1465 	 * accordingly.
1466 	 */
1467 	final->t11_t12 = roundup(final->t11_t12, 100 * 10);
1468 }
1469 
1470 static void pps_init_registers(struct intel_dp *intel_dp, bool force_disable_vdd)
1471 {
1472 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1473 	u32 pp_on, pp_off, port_sel = 0;
1474 	int div = RUNTIME_INFO(dev_priv)->rawclk_freq / 1000;
1475 	struct pps_registers regs;
1476 	enum port port = dp_to_dig_port(intel_dp)->base.port;
1477 	const struct edp_power_seq *seq = &intel_dp->pps.pps_delays;
1478 
1479 	lockdep_assert_held(&dev_priv->display.pps.mutex);
1480 
1481 	intel_pps_get_registers(intel_dp, &regs);
1482 
1483 	/*
1484 	 * On some VLV machines the BIOS can leave the VDD
1485 	 * enabled even on power sequencers which aren't
1486 	 * hooked up to any port. This would mess up the
1487 	 * power domain tracking the first time we pick
1488 	 * one of these power sequencers for use since
1489 	 * intel_pps_vdd_on_unlocked() would notice that the VDD was
1490 	 * already on and therefore wouldn't grab the power
1491 	 * domain reference. Disable VDD first to avoid this.
1492 	 * This also avoids spuriously turning the VDD on as
1493 	 * soon as the new power sequencer gets initialized.
1494 	 */
1495 	if (force_disable_vdd) {
1496 		u32 pp = ilk_get_pp_control(intel_dp);
1497 
1498 		drm_WARN(&dev_priv->drm, pp & PANEL_POWER_ON,
1499 			 "Panel power already on\n");
1500 
1501 		if (pp & EDP_FORCE_VDD)
1502 			drm_dbg_kms(&dev_priv->drm,
1503 				    "VDD already on, disabling first\n");
1504 
1505 		pp &= ~EDP_FORCE_VDD;
1506 
1507 		intel_de_write(dev_priv, regs.pp_ctrl, pp);
1508 	}
1509 
1510 	pp_on = REG_FIELD_PREP(PANEL_POWER_UP_DELAY_MASK, seq->t1_t3) |
1511 		REG_FIELD_PREP(PANEL_LIGHT_ON_DELAY_MASK, seq->t8);
1512 	pp_off = REG_FIELD_PREP(PANEL_LIGHT_OFF_DELAY_MASK, seq->t9) |
1513 		REG_FIELD_PREP(PANEL_POWER_DOWN_DELAY_MASK, seq->t10);
1514 
1515 	/* Haswell doesn't have any port selection bits for the panel
1516 	 * power sequencer any more. */
1517 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1518 		port_sel = PANEL_PORT_SELECT_VLV(port);
1519 	} else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
1520 		switch (port) {
1521 		case PORT_A:
1522 			port_sel = PANEL_PORT_SELECT_DPA;
1523 			break;
1524 		case PORT_C:
1525 			port_sel = PANEL_PORT_SELECT_DPC;
1526 			break;
1527 		case PORT_D:
1528 			port_sel = PANEL_PORT_SELECT_DPD;
1529 			break;
1530 		default:
1531 			MISSING_CASE(port);
1532 			break;
1533 		}
1534 	}
1535 
1536 	pp_on |= port_sel;
1537 
1538 	intel_de_write(dev_priv, regs.pp_on, pp_on);
1539 	intel_de_write(dev_priv, regs.pp_off, pp_off);
1540 
1541 	/*
1542 	 * Compute the divisor for the pp clock, simply match the Bspec formula.
1543 	 */
1544 	if (i915_mmio_reg_valid(regs.pp_div))
1545 		intel_de_write(dev_priv, regs.pp_div,
1546 			       REG_FIELD_PREP(PP_REFERENCE_DIVIDER_MASK, (100 * div) / 2 - 1) | REG_FIELD_PREP(PANEL_POWER_CYCLE_DELAY_MASK, DIV_ROUND_UP(seq->t11_t12, 1000)));
1547 	else
1548 		intel_de_rmw(dev_priv, regs.pp_ctrl, BXT_POWER_CYCLE_DELAY_MASK,
1549 			     REG_FIELD_PREP(BXT_POWER_CYCLE_DELAY_MASK,
1550 					    DIV_ROUND_UP(seq->t11_t12, 1000)));
1551 
1552 	drm_dbg_kms(&dev_priv->drm,
1553 		    "panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
1554 		    intel_de_read(dev_priv, regs.pp_on),
1555 		    intel_de_read(dev_priv, regs.pp_off),
1556 		    i915_mmio_reg_valid(regs.pp_div) ?
1557 		    intel_de_read(dev_priv, regs.pp_div) :
1558 		    (intel_de_read(dev_priv, regs.pp_ctrl) & BXT_POWER_CYCLE_DELAY_MASK));
1559 }
1560 
1561 void intel_pps_encoder_reset(struct intel_dp *intel_dp)
1562 {
1563 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1564 	intel_wakeref_t wakeref;
1565 
1566 	if (!intel_dp_is_edp(intel_dp))
1567 		return;
1568 
1569 	with_intel_pps_lock(intel_dp, wakeref) {
1570 		/*
1571 		 * Reinit the power sequencer also on the resume path, in case
1572 		 * BIOS did something nasty with it.
1573 		 */
1574 		if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
1575 			vlv_initial_power_sequencer_setup(intel_dp);
1576 
1577 		pps_init_delays(intel_dp);
1578 		pps_init_registers(intel_dp, false);
1579 		pps_vdd_init(intel_dp);
1580 
1581 		if (edp_have_panel_vdd(intel_dp))
1582 			edp_panel_vdd_schedule_off(intel_dp);
1583 	}
1584 }
1585 
1586 bool intel_pps_init(struct intel_dp *intel_dp)
1587 {
1588 	intel_wakeref_t wakeref;
1589 	bool ret;
1590 
1591 	intel_dp->pps.initializing = true;
1592 	INIT_DELAYED_WORK(&intel_dp->pps.panel_vdd_work, edp_panel_vdd_work);
1593 
1594 	pps_init_timestamps(intel_dp);
1595 
1596 	with_intel_pps_lock(intel_dp, wakeref) {
1597 		ret = pps_initial_setup(intel_dp);
1598 
1599 		pps_init_delays(intel_dp);
1600 		pps_init_registers(intel_dp, false);
1601 		pps_vdd_init(intel_dp);
1602 	}
1603 
1604 	return ret;
1605 }
1606 
1607 static void pps_init_late(struct intel_dp *intel_dp)
1608 {
1609 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1610 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1611 	struct intel_connector *connector = intel_dp->attached_connector;
1612 
1613 	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
1614 		return;
1615 
1616 	if (intel_num_pps(i915) < 2)
1617 		return;
1618 
1619 	drm_WARN(&i915->drm, connector->panel.vbt.backlight.controller >= 0 &&
1620 		 intel_dp->pps.pps_idx != connector->panel.vbt.backlight.controller,
1621 		 "[ENCODER:%d:%s] power sequencer mismatch: %d (initial) vs. %d (VBT)\n",
1622 		 encoder->base.base.id, encoder->base.name,
1623 		 intel_dp->pps.pps_idx, connector->panel.vbt.backlight.controller);
1624 
1625 	if (connector->panel.vbt.backlight.controller >= 0)
1626 		intel_dp->pps.pps_idx = connector->panel.vbt.backlight.controller;
1627 }
1628 
1629 void intel_pps_init_late(struct intel_dp *intel_dp)
1630 {
1631 	intel_wakeref_t wakeref;
1632 
1633 	with_intel_pps_lock(intel_dp, wakeref) {
1634 		/* Reinit delays after per-panel info has been parsed from VBT */
1635 		pps_init_late(intel_dp);
1636 
1637 		memset(&intel_dp->pps.pps_delays, 0, sizeof(intel_dp->pps.pps_delays));
1638 		pps_init_delays(intel_dp);
1639 		pps_init_registers(intel_dp, false);
1640 
1641 		intel_dp->pps.initializing = false;
1642 
1643 		if (edp_have_panel_vdd(intel_dp))
1644 			edp_panel_vdd_schedule_off(intel_dp);
1645 	}
1646 }
1647 
1648 void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
1649 {
1650 	int pps_num;
1651 	int pps_idx;
1652 
1653 	if (!HAS_DISPLAY(dev_priv) || HAS_DDI(dev_priv))
1654 		return;
1655 	/*
1656 	 * This w/a is needed at least on CPT/PPT, but to be sure apply it
1657 	 * everywhere where registers can be write protected.
1658 	 */
1659 	pps_num = intel_num_pps(dev_priv);
1660 
1661 	for (pps_idx = 0; pps_idx < pps_num; pps_idx++)
1662 		intel_de_rmw(dev_priv, PP_CONTROL(dev_priv, pps_idx),
1663 			     PANEL_UNLOCK_MASK, PANEL_UNLOCK_REGS);
1664 }
1665 
1666 void intel_pps_setup(struct drm_i915_private *i915)
1667 {
1668 	if (HAS_PCH_SPLIT(i915) || IS_GEMINILAKE(i915) || IS_BROXTON(i915))
1669 		i915->display.pps.mmio_base = PCH_PPS_BASE;
1670 	else if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
1671 		i915->display.pps.mmio_base = VLV_PPS_BASE;
1672 	else
1673 		i915->display.pps.mmio_base = PPS_BASE;
1674 }
1675 
1676 static int intel_pps_show(struct seq_file *m, void *data)
1677 {
1678 	struct intel_connector *connector = m->private;
1679 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1680 
1681 	if (connector->base.status != connector_status_connected)
1682 		return -ENODEV;
1683 
1684 	seq_printf(m, "Panel power up delay: %d\n",
1685 		   intel_dp->pps.panel_power_up_delay);
1686 	seq_printf(m, "Panel power down delay: %d\n",
1687 		   intel_dp->pps.panel_power_down_delay);
1688 	seq_printf(m, "Backlight on delay: %d\n",
1689 		   intel_dp->pps.backlight_on_delay);
1690 	seq_printf(m, "Backlight off delay: %d\n",
1691 		   intel_dp->pps.backlight_off_delay);
1692 
1693 	return 0;
1694 }
1695 DEFINE_SHOW_ATTRIBUTE(intel_pps);
1696 
1697 void intel_pps_connector_debugfs_add(struct intel_connector *connector)
1698 {
1699 	struct dentry *root = connector->base.debugfs_entry;
1700 	int connector_type = connector->base.connector_type;
1701 
1702 	if (connector_type == DRM_MODE_CONNECTOR_eDP)
1703 		debugfs_create_file("i915_panel_timings", 0444, root,
1704 				    connector, &intel_pps_fops);
1705 }
1706 
1707 void assert_pps_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
1708 {
1709 	i915_reg_t pp_reg;
1710 	u32 val;
1711 	enum pipe panel_pipe = INVALID_PIPE;
1712 	bool locked = true;
1713 
1714 	if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
1715 		return;
1716 
1717 	if (HAS_PCH_SPLIT(dev_priv)) {
1718 		u32 port_sel;
1719 
1720 		pp_reg = PP_CONTROL(dev_priv, 0);
1721 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(dev_priv, 0)) & PANEL_PORT_SELECT_MASK;
1722 
1723 		switch (port_sel) {
1724 		case PANEL_PORT_SELECT_LVDS:
1725 			intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
1726 			break;
1727 		case PANEL_PORT_SELECT_DPA:
1728 			g4x_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1729 			break;
1730 		case PANEL_PORT_SELECT_DPC:
1731 			g4x_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1732 			break;
1733 		case PANEL_PORT_SELECT_DPD:
1734 			g4x_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1735 			break;
1736 		default:
1737 			MISSING_CASE(port_sel);
1738 			break;
1739 		}
1740 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1741 		/* presumably write lock depends on pipe, not port select */
1742 		pp_reg = PP_CONTROL(dev_priv, pipe);
1743 		panel_pipe = pipe;
1744 	} else {
1745 		u32 port_sel;
1746 
1747 		pp_reg = PP_CONTROL(dev_priv, 0);
1748 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(dev_priv, 0)) & PANEL_PORT_SELECT_MASK;
1749 
1750 		drm_WARN_ON(&dev_priv->drm,
1751 			    port_sel != PANEL_PORT_SELECT_LVDS);
1752 		intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
1753 	}
1754 
1755 	val = intel_de_read(dev_priv, pp_reg);
1756 	if (!(val & PANEL_POWER_ON) ||
1757 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1758 		locked = false;
1759 
1760 	I915_STATE_WARN(dev_priv, panel_pipe == pipe && locked,
1761 			"panel assertion failure, pipe %c regs locked\n",
1762 			pipe_name(pipe));
1763 }
1764