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