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