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