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