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