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