1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include <linux/backlight.h>
7 #include <linux/kernel.h>
8 #include <linux/pwm.h>
9 #include <linux/string_helpers.h>
10 
11 #include <acpi/video.h>
12 
13 #include "i915_reg.h"
14 #include "intel_backlight.h"
15 #include "intel_backlight_regs.h"
16 #include "intel_connector.h"
17 #include "intel_de.h"
18 #include "intel_display_types.h"
19 #include "intel_dp_aux_backlight.h"
20 #include "intel_dsi_dcs_backlight.h"
21 #include "intel_panel.h"
22 #include "intel_pci_config.h"
23 #include "intel_pps.h"
24 #include "intel_quirks.h"
25 
26 /**
27  * scale - scale values from one range to another
28  * @source_val: value in range [@source_min..@source_max]
29  * @source_min: minimum legal value for @source_val
30  * @source_max: maximum legal value for @source_val
31  * @target_min: corresponding target value for @source_min
32  * @target_max: corresponding target value for @source_max
33  *
34  * Return @source_val in range [@source_min..@source_max] scaled to range
35  * [@target_min..@target_max].
36  */
37 static u32 scale(u32 source_val,
38 		 u32 source_min, u32 source_max,
39 		 u32 target_min, u32 target_max)
40 {
41 	u64 target_val;
42 
43 	WARN_ON(source_min > source_max);
44 	WARN_ON(target_min > target_max);
45 
46 	/* defensive */
47 	source_val = clamp(source_val, source_min, source_max);
48 
49 	/* avoid overflows */
50 	target_val = mul_u32_u32(source_val - source_min,
51 				 target_max - target_min);
52 	target_val = DIV_ROUND_CLOSEST_ULL(target_val, source_max - source_min);
53 	target_val += target_min;
54 
55 	return target_val;
56 }
57 
58 /*
59  * Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
60  * to [hw_min..hw_max].
61  */
62 static u32 clamp_user_to_hw(struct intel_connector *connector,
63 			    u32 user_level, u32 user_max)
64 {
65 	struct intel_panel *panel = &connector->panel;
66 	u32 hw_level;
67 
68 	hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
69 	hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
70 
71 	return hw_level;
72 }
73 
74 /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
75 static u32 scale_hw_to_user(struct intel_connector *connector,
76 			    u32 hw_level, u32 user_max)
77 {
78 	struct intel_panel *panel = &connector->panel;
79 
80 	return scale(hw_level, panel->backlight.min, panel->backlight.max,
81 		     0, user_max);
82 }
83 
84 u32 intel_backlight_invert_pwm_level(struct intel_connector *connector, u32 val)
85 {
86 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
87 	struct intel_panel *panel = &connector->panel;
88 
89 	drm_WARN_ON(&dev_priv->drm, panel->backlight.pwm_level_max == 0);
90 
91 	if (dev_priv->params.invert_brightness < 0)
92 		return val;
93 
94 	if (dev_priv->params.invert_brightness > 0 ||
95 	    intel_has_quirk(dev_priv, QUIRK_INVERT_BRIGHTNESS)) {
96 		return panel->backlight.pwm_level_max - val + panel->backlight.pwm_level_min;
97 	}
98 
99 	return val;
100 }
101 
102 void intel_backlight_set_pwm_level(const struct drm_connector_state *conn_state, u32 val)
103 {
104 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
105 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
106 	struct intel_panel *panel = &connector->panel;
107 
108 	drm_dbg_kms(&i915->drm, "set backlight PWM = %d\n", val);
109 	panel->backlight.pwm_funcs->set(conn_state, val);
110 }
111 
112 u32 intel_backlight_level_to_pwm(struct intel_connector *connector, u32 val)
113 {
114 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
115 	struct intel_panel *panel = &connector->panel;
116 
117 	drm_WARN_ON_ONCE(&dev_priv->drm,
118 			 panel->backlight.max == 0 || panel->backlight.pwm_level_max == 0);
119 
120 	val = scale(val, panel->backlight.min, panel->backlight.max,
121 		    panel->backlight.pwm_level_min, panel->backlight.pwm_level_max);
122 
123 	return intel_backlight_invert_pwm_level(connector, val);
124 }
125 
126 u32 intel_backlight_level_from_pwm(struct intel_connector *connector, u32 val)
127 {
128 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
129 	struct intel_panel *panel = &connector->panel;
130 
131 	drm_WARN_ON_ONCE(&dev_priv->drm,
132 			 panel->backlight.max == 0 || panel->backlight.pwm_level_max == 0);
133 
134 	if (dev_priv->params.invert_brightness > 0 ||
135 	    (dev_priv->params.invert_brightness == 0 && intel_has_quirk(dev_priv, QUIRK_INVERT_BRIGHTNESS)))
136 		val = panel->backlight.pwm_level_max - (val - panel->backlight.pwm_level_min);
137 
138 	return scale(val, panel->backlight.pwm_level_min, panel->backlight.pwm_level_max,
139 		     panel->backlight.min, panel->backlight.max);
140 }
141 
142 static u32 lpt_get_backlight(struct intel_connector *connector, enum pipe unused)
143 {
144 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
145 
146 	return intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
147 }
148 
149 static u32 pch_get_backlight(struct intel_connector *connector, enum pipe unused)
150 {
151 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
152 
153 	return intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
154 }
155 
156 static u32 i9xx_get_backlight(struct intel_connector *connector, enum pipe unused)
157 {
158 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
159 	struct intel_panel *panel = &connector->panel;
160 	u32 val;
161 
162 	val = intel_de_read(dev_priv, BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
163 	if (DISPLAY_VER(dev_priv) < 4)
164 		val >>= 1;
165 
166 	if (panel->backlight.combination_mode) {
167 		u8 lbpc;
168 
169 		pci_read_config_byte(to_pci_dev(dev_priv->drm.dev), LBPC, &lbpc);
170 		val *= lbpc;
171 	}
172 
173 	return val;
174 }
175 
176 static u32 vlv_get_backlight(struct intel_connector *connector, enum pipe pipe)
177 {
178 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
179 
180 	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
181 		return 0;
182 
183 	return intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
184 }
185 
186 static u32 bxt_get_backlight(struct intel_connector *connector, enum pipe unused)
187 {
188 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
189 	struct intel_panel *panel = &connector->panel;
190 
191 	return intel_de_read(dev_priv,
192 			     BXT_BLC_PWM_DUTY(panel->backlight.controller));
193 }
194 
195 static u32 ext_pwm_get_backlight(struct intel_connector *connector, enum pipe unused)
196 {
197 	struct intel_panel *panel = &connector->panel;
198 	struct pwm_state state;
199 
200 	pwm_get_state(panel->backlight.pwm, &state);
201 	return pwm_get_relative_duty_cycle(&state, 100);
202 }
203 
204 static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
205 {
206 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
207 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
208 
209 	u32 val = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
210 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, val | level);
211 }
212 
213 static void pch_set_backlight(const struct drm_connector_state *conn_state, u32 level)
214 {
215 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
216 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
217 	u32 tmp;
218 
219 	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
220 	intel_de_write(dev_priv, BLC_PWM_CPU_CTL, tmp | level);
221 }
222 
223 static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
224 {
225 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
226 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
227 	struct intel_panel *panel = &connector->panel;
228 	u32 tmp, mask;
229 
230 	drm_WARN_ON(&dev_priv->drm, panel->backlight.pwm_level_max == 0);
231 
232 	if (panel->backlight.combination_mode) {
233 		u8 lbpc;
234 
235 		lbpc = level * 0xfe / panel->backlight.pwm_level_max + 1;
236 		level /= lbpc;
237 		pci_write_config_byte(to_pci_dev(dev_priv->drm.dev), LBPC, lbpc);
238 	}
239 
240 	if (DISPLAY_VER(dev_priv) == 4) {
241 		mask = BACKLIGHT_DUTY_CYCLE_MASK;
242 	} else {
243 		level <<= 1;
244 		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
245 	}
246 
247 	tmp = intel_de_read(dev_priv, BLC_PWM_CTL) & ~mask;
248 	intel_de_write(dev_priv, BLC_PWM_CTL, tmp | level);
249 }
250 
251 static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
252 {
253 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
254 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
255 	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
256 	u32 tmp;
257 
258 	tmp = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
259 	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), tmp | level);
260 }
261 
262 static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
263 {
264 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
265 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
266 	struct intel_panel *panel = &connector->panel;
267 
268 	intel_de_write(dev_priv,
269 		       BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
270 }
271 
272 static void ext_pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
273 {
274 	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
275 
276 	pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100);
277 	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
278 }
279 
280 static void
281 intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
282 {
283 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
284 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
285 	struct intel_panel *panel = &connector->panel;
286 
287 	drm_dbg_kms(&i915->drm, "set backlight level = %d\n", level);
288 
289 	panel->backlight.funcs->set(conn_state, level);
290 }
291 
292 /* set backlight brightness to level in range [0..max], assuming hw min is
293  * respected.
294  */
295 void intel_backlight_set_acpi(const struct drm_connector_state *conn_state,
296 			      u32 user_level, u32 user_max)
297 {
298 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
299 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
300 	struct intel_panel *panel = &connector->panel;
301 	u32 hw_level;
302 
303 	/*
304 	 * Lack of crtc may occur during driver init because
305 	 * connection_mutex isn't held across the entire backlight
306 	 * setup + modeset readout, and the BIOS can issue the
307 	 * requests at any time.
308 	 */
309 	if (!panel->backlight.present || !conn_state->crtc)
310 		return;
311 
312 	mutex_lock(&dev_priv->display.backlight.lock);
313 
314 	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
315 
316 	hw_level = clamp_user_to_hw(connector, user_level, user_max);
317 	panel->backlight.level = hw_level;
318 
319 	if (panel->backlight.device)
320 		panel->backlight.device->props.brightness =
321 			scale_hw_to_user(connector,
322 					 panel->backlight.level,
323 					 panel->backlight.device->props.max_brightness);
324 
325 	if (panel->backlight.enabled)
326 		intel_panel_actually_set_backlight(conn_state, hw_level);
327 
328 	mutex_unlock(&dev_priv->display.backlight.lock);
329 }
330 
331 static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state, u32 level)
332 {
333 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
334 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
335 	u32 tmp;
336 
337 	intel_backlight_set_pwm_level(old_conn_state, level);
338 
339 	/*
340 	 * Although we don't support or enable CPU PWM with LPT/SPT based
341 	 * systems, it may have been enabled prior to loading the
342 	 * driver. Disable to avoid warnings on LCPLL disable.
343 	 *
344 	 * This needs rework if we need to add support for CPU PWM on PCH split
345 	 * platforms.
346 	 */
347 	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
348 	if (tmp & BLM_PWM_ENABLE) {
349 		drm_dbg_kms(&dev_priv->drm,
350 			    "cpu backlight was enabled, disabling\n");
351 		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
352 			       tmp & ~BLM_PWM_ENABLE);
353 	}
354 
355 	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
356 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
357 }
358 
359 static void pch_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
360 {
361 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
362 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
363 	u32 tmp;
364 
365 	intel_backlight_set_pwm_level(old_conn_state, val);
366 
367 	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
368 	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
369 
370 	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
371 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
372 }
373 
374 static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
375 {
376 	intel_backlight_set_pwm_level(old_conn_state, val);
377 }
378 
379 static void i965_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
380 {
381 	struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
382 	u32 tmp;
383 
384 	intel_backlight_set_pwm_level(old_conn_state, val);
385 
386 	tmp = intel_de_read(dev_priv, BLC_PWM_CTL2);
387 	intel_de_write(dev_priv, BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
388 }
389 
390 static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
391 {
392 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
393 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
394 	enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
395 	u32 tmp;
396 
397 	intel_backlight_set_pwm_level(old_conn_state, val);
398 
399 	tmp = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
400 	intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe),
401 		       tmp & ~BLM_PWM_ENABLE);
402 }
403 
404 static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
405 {
406 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
407 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
408 	struct intel_panel *panel = &connector->panel;
409 	u32 tmp;
410 
411 	intel_backlight_set_pwm_level(old_conn_state, val);
412 
413 	tmp = intel_de_read(dev_priv,
414 			    BXT_BLC_PWM_CTL(panel->backlight.controller));
415 	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
416 		       tmp & ~BXT_BLC_PWM_ENABLE);
417 
418 	if (panel->backlight.controller == 1) {
419 		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
420 		val &= ~UTIL_PIN_ENABLE;
421 		intel_de_write(dev_priv, UTIL_PIN_CTL, val);
422 	}
423 }
424 
425 static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
426 {
427 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
428 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
429 	struct intel_panel *panel = &connector->panel;
430 	u32 tmp;
431 
432 	intel_backlight_set_pwm_level(old_conn_state, val);
433 
434 	tmp = intel_de_read(dev_priv,
435 			    BXT_BLC_PWM_CTL(panel->backlight.controller));
436 	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
437 		       tmp & ~BXT_BLC_PWM_ENABLE);
438 }
439 
440 static void ext_pwm_disable_backlight(const struct drm_connector_state *old_conn_state, u32 level)
441 {
442 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
443 	struct intel_panel *panel = &connector->panel;
444 
445 	intel_backlight_set_pwm_level(old_conn_state, level);
446 
447 	panel->backlight.pwm_state.enabled = false;
448 	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
449 }
450 
451 void intel_backlight_disable(const struct drm_connector_state *old_conn_state)
452 {
453 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
454 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
455 	struct intel_panel *panel = &connector->panel;
456 
457 	if (!panel->backlight.present)
458 		return;
459 
460 	/*
461 	 * Do not disable backlight on the vga_switcheroo path. When switching
462 	 * away from i915, the other client may depend on i915 to handle the
463 	 * backlight. This will leave the backlight on unnecessarily when
464 	 * another client is not activated.
465 	 */
466 	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
467 		drm_dbg_kms(&dev_priv->drm,
468 			    "Skipping backlight disable on vga switch\n");
469 		return;
470 	}
471 
472 	mutex_lock(&dev_priv->display.backlight.lock);
473 
474 	if (panel->backlight.device)
475 		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
476 	panel->backlight.enabled = false;
477 	panel->backlight.funcs->disable(old_conn_state, 0);
478 
479 	mutex_unlock(&dev_priv->display.backlight.lock);
480 }
481 
482 static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
483 				 const struct drm_connector_state *conn_state, u32 level)
484 {
485 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
486 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
487 	struct intel_panel *panel = &connector->panel;
488 	u32 pch_ctl1, pch_ctl2, schicken;
489 
490 	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
491 	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
492 		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
493 		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
494 		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
495 	}
496 
497 	if (HAS_PCH_LPT(dev_priv)) {
498 		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN2);
499 		if (panel->backlight.alternate_pwm_increment)
500 			schicken |= LPT_PWM_GRANULARITY;
501 		else
502 			schicken &= ~LPT_PWM_GRANULARITY;
503 		intel_de_write(dev_priv, SOUTH_CHICKEN2, schicken);
504 	} else {
505 		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN1);
506 		if (panel->backlight.alternate_pwm_increment)
507 			schicken |= SPT_PWM_GRANULARITY;
508 		else
509 			schicken &= ~SPT_PWM_GRANULARITY;
510 		intel_de_write(dev_priv, SOUTH_CHICKEN1, schicken);
511 	}
512 
513 	pch_ctl2 = panel->backlight.pwm_level_max << 16;
514 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
515 
516 	pch_ctl1 = 0;
517 	if (panel->backlight.active_low_pwm)
518 		pch_ctl1 |= BLM_PCH_POLARITY;
519 
520 	/* After LPT, override is the default. */
521 	if (HAS_PCH_LPT(dev_priv))
522 		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
523 
524 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
525 	intel_de_posting_read(dev_priv, BLC_PWM_PCH_CTL1);
526 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
527 		       pch_ctl1 | BLM_PCH_PWM_ENABLE);
528 
529 	/* This won't stick until the above enable. */
530 	intel_backlight_set_pwm_level(conn_state, level);
531 }
532 
533 static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
534 				 const struct drm_connector_state *conn_state, u32 level)
535 {
536 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
537 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
538 	struct intel_panel *panel = &connector->panel;
539 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
540 	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
541 
542 	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
543 	if (cpu_ctl2 & BLM_PWM_ENABLE) {
544 		drm_dbg_kms(&dev_priv->drm, "cpu backlight already enabled\n");
545 		cpu_ctl2 &= ~BLM_PWM_ENABLE;
546 		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
547 	}
548 
549 	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
550 	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
551 		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
552 		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
553 		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
554 	}
555 
556 	if (cpu_transcoder == TRANSCODER_EDP)
557 		cpu_ctl2 = BLM_TRANSCODER_EDP;
558 	else
559 		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
560 	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
561 	intel_de_posting_read(dev_priv, BLC_PWM_CPU_CTL2);
562 	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
563 
564 	/* This won't stick until the above enable. */
565 	intel_backlight_set_pwm_level(conn_state, level);
566 
567 	pch_ctl2 = panel->backlight.pwm_level_max << 16;
568 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
569 
570 	pch_ctl1 = 0;
571 	if (panel->backlight.active_low_pwm)
572 		pch_ctl1 |= BLM_PCH_POLARITY;
573 
574 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
575 	intel_de_posting_read(dev_priv, BLC_PWM_PCH_CTL1);
576 	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
577 		       pch_ctl1 | BLM_PCH_PWM_ENABLE);
578 }
579 
580 static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
581 				  const struct drm_connector_state *conn_state, u32 level)
582 {
583 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
584 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
585 	struct intel_panel *panel = &connector->panel;
586 	u32 ctl, freq;
587 
588 	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
589 	if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
590 		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
591 		intel_de_write(dev_priv, BLC_PWM_CTL, 0);
592 	}
593 
594 	freq = panel->backlight.pwm_level_max;
595 	if (panel->backlight.combination_mode)
596 		freq /= 0xff;
597 
598 	ctl = freq << 17;
599 	if (panel->backlight.combination_mode)
600 		ctl |= BLM_LEGACY_MODE;
601 	if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
602 		ctl |= BLM_POLARITY_PNV;
603 
604 	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
605 	intel_de_posting_read(dev_priv, BLC_PWM_CTL);
606 
607 	/* XXX: combine this into above write? */
608 	intel_backlight_set_pwm_level(conn_state, level);
609 
610 	/*
611 	 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
612 	 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
613 	 * that has backlight.
614 	 */
615 	if (DISPLAY_VER(dev_priv) == 2)
616 		intel_de_write(dev_priv, BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
617 }
618 
619 static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
620 				  const struct drm_connector_state *conn_state, u32 level)
621 {
622 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
623 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
624 	struct intel_panel *panel = &connector->panel;
625 	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
626 	u32 ctl, ctl2, freq;
627 
628 	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
629 	if (ctl2 & BLM_PWM_ENABLE) {
630 		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
631 		ctl2 &= ~BLM_PWM_ENABLE;
632 		intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2);
633 	}
634 
635 	freq = panel->backlight.pwm_level_max;
636 	if (panel->backlight.combination_mode)
637 		freq /= 0xff;
638 
639 	ctl = freq << 16;
640 	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
641 
642 	ctl2 = BLM_PIPE(pipe);
643 	if (panel->backlight.combination_mode)
644 		ctl2 |= BLM_COMBINATION_MODE;
645 	if (panel->backlight.active_low_pwm)
646 		ctl2 |= BLM_POLARITY_I965;
647 	intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2);
648 	intel_de_posting_read(dev_priv, BLC_PWM_CTL2);
649 	intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
650 
651 	intel_backlight_set_pwm_level(conn_state, level);
652 }
653 
654 static void vlv_enable_backlight(const struct intel_crtc_state *crtc_state,
655 				 const struct drm_connector_state *conn_state, u32 level)
656 {
657 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
658 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
659 	struct intel_panel *panel = &connector->panel;
660 	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
661 	u32 ctl, ctl2;
662 
663 	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
664 	if (ctl2 & BLM_PWM_ENABLE) {
665 		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
666 		ctl2 &= ~BLM_PWM_ENABLE;
667 		intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
668 	}
669 
670 	ctl = panel->backlight.pwm_level_max << 16;
671 	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), ctl);
672 
673 	/* XXX: combine this into above write? */
674 	intel_backlight_set_pwm_level(conn_state, level);
675 
676 	ctl2 = 0;
677 	if (panel->backlight.active_low_pwm)
678 		ctl2 |= BLM_POLARITY_I965;
679 	intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
680 	intel_de_posting_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
681 	intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe),
682 		       ctl2 | BLM_PWM_ENABLE);
683 }
684 
685 static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
686 				 const struct drm_connector_state *conn_state, u32 level)
687 {
688 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
689 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
690 	struct intel_panel *panel = &connector->panel;
691 	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
692 	u32 pwm_ctl, val;
693 
694 	/* Controller 1 uses the utility pin. */
695 	if (panel->backlight.controller == 1) {
696 		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
697 		if (val & UTIL_PIN_ENABLE) {
698 			drm_dbg_kms(&dev_priv->drm,
699 				    "util pin already enabled\n");
700 			val &= ~UTIL_PIN_ENABLE;
701 			intel_de_write(dev_priv, UTIL_PIN_CTL, val);
702 		}
703 
704 		val = 0;
705 		if (panel->backlight.util_pin_active_low)
706 			val |= UTIL_PIN_POLARITY;
707 		intel_de_write(dev_priv, UTIL_PIN_CTL,
708 			       val | UTIL_PIN_PIPE(pipe) | UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
709 	}
710 
711 	pwm_ctl = intel_de_read(dev_priv,
712 				BXT_BLC_PWM_CTL(panel->backlight.controller));
713 	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
714 		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
715 		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
716 		intel_de_write(dev_priv,
717 			       BXT_BLC_PWM_CTL(panel->backlight.controller),
718 			       pwm_ctl);
719 	}
720 
721 	intel_de_write(dev_priv,
722 		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
723 		       panel->backlight.pwm_level_max);
724 
725 	intel_backlight_set_pwm_level(conn_state, level);
726 
727 	pwm_ctl = 0;
728 	if (panel->backlight.active_low_pwm)
729 		pwm_ctl |= BXT_BLC_PWM_POLARITY;
730 
731 	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
732 		       pwm_ctl);
733 	intel_de_posting_read(dev_priv,
734 			      BXT_BLC_PWM_CTL(panel->backlight.controller));
735 	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
736 		       pwm_ctl | BXT_BLC_PWM_ENABLE);
737 }
738 
739 static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
740 				 const struct drm_connector_state *conn_state, u32 level)
741 {
742 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
743 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
744 	struct intel_panel *panel = &connector->panel;
745 	u32 pwm_ctl;
746 
747 	pwm_ctl = intel_de_read(dev_priv,
748 				BXT_BLC_PWM_CTL(panel->backlight.controller));
749 	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
750 		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
751 		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
752 		intel_de_write(dev_priv,
753 			       BXT_BLC_PWM_CTL(panel->backlight.controller),
754 			       pwm_ctl);
755 	}
756 
757 	intel_de_write(dev_priv,
758 		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
759 		       panel->backlight.pwm_level_max);
760 
761 	intel_backlight_set_pwm_level(conn_state, level);
762 
763 	pwm_ctl = 0;
764 	if (panel->backlight.active_low_pwm)
765 		pwm_ctl |= BXT_BLC_PWM_POLARITY;
766 
767 	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
768 		       pwm_ctl);
769 	intel_de_posting_read(dev_priv,
770 			      BXT_BLC_PWM_CTL(panel->backlight.controller));
771 	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
772 		       pwm_ctl | BXT_BLC_PWM_ENABLE);
773 }
774 
775 static void ext_pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
776 				     const struct drm_connector_state *conn_state, u32 level)
777 {
778 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
779 	struct intel_panel *panel = &connector->panel;
780 
781 	pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100);
782 	panel->backlight.pwm_state.enabled = true;
783 	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
784 }
785 
786 static void __intel_backlight_enable(const struct intel_crtc_state *crtc_state,
787 				     const struct drm_connector_state *conn_state)
788 {
789 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
790 	struct intel_panel *panel = &connector->panel;
791 
792 	WARN_ON(panel->backlight.max == 0);
793 
794 	if (panel->backlight.level <= panel->backlight.min) {
795 		panel->backlight.level = panel->backlight.max;
796 		if (panel->backlight.device)
797 			panel->backlight.device->props.brightness =
798 				scale_hw_to_user(connector,
799 						 panel->backlight.level,
800 						 panel->backlight.device->props.max_brightness);
801 	}
802 
803 	panel->backlight.funcs->enable(crtc_state, conn_state, panel->backlight.level);
804 	panel->backlight.enabled = true;
805 	if (panel->backlight.device)
806 		panel->backlight.device->props.power = FB_BLANK_UNBLANK;
807 }
808 
809 void intel_backlight_enable(const struct intel_crtc_state *crtc_state,
810 			    const struct drm_connector_state *conn_state)
811 {
812 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
813 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
814 	struct intel_panel *panel = &connector->panel;
815 	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
816 
817 	if (!panel->backlight.present)
818 		return;
819 
820 	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(pipe));
821 
822 	mutex_lock(&dev_priv->display.backlight.lock);
823 
824 	__intel_backlight_enable(crtc_state, conn_state);
825 
826 	mutex_unlock(&dev_priv->display.backlight.lock);
827 }
828 
829 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
830 static u32 intel_panel_get_backlight(struct intel_connector *connector)
831 {
832 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
833 	struct intel_panel *panel = &connector->panel;
834 	u32 val = 0;
835 
836 	mutex_lock(&dev_priv->display.backlight.lock);
837 
838 	if (panel->backlight.enabled)
839 		val = panel->backlight.funcs->get(connector, intel_connector_get_pipe(connector));
840 
841 	mutex_unlock(&dev_priv->display.backlight.lock);
842 
843 	drm_dbg_kms(&dev_priv->drm, "get backlight PWM = %d\n", val);
844 	return val;
845 }
846 
847 /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
848 static u32 scale_user_to_hw(struct intel_connector *connector,
849 			    u32 user_level, u32 user_max)
850 {
851 	struct intel_panel *panel = &connector->panel;
852 
853 	return scale(user_level, 0, user_max,
854 		     panel->backlight.min, panel->backlight.max);
855 }
856 
857 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
858 static void intel_panel_set_backlight(const struct drm_connector_state *conn_state,
859 				      u32 user_level, u32 user_max)
860 {
861 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
862 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
863 	struct intel_panel *panel = &connector->panel;
864 	u32 hw_level;
865 
866 	if (!panel->backlight.present)
867 		return;
868 
869 	mutex_lock(&dev_priv->display.backlight.lock);
870 
871 	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
872 
873 	hw_level = scale_user_to_hw(connector, user_level, user_max);
874 	panel->backlight.level = hw_level;
875 
876 	if (panel->backlight.enabled)
877 		intel_panel_actually_set_backlight(conn_state, hw_level);
878 
879 	mutex_unlock(&dev_priv->display.backlight.lock);
880 }
881 
882 static int intel_backlight_device_update_status(struct backlight_device *bd)
883 {
884 	struct intel_connector *connector = bl_get_data(bd);
885 	struct intel_panel *panel = &connector->panel;
886 	struct drm_device *dev = connector->base.dev;
887 
888 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
889 	DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
890 		      bd->props.brightness, bd->props.max_brightness);
891 	intel_panel_set_backlight(connector->base.state, bd->props.brightness,
892 				  bd->props.max_brightness);
893 
894 	/*
895 	 * Allow flipping bl_power as a sub-state of enabled. Sadly the
896 	 * backlight class device does not make it easy to differentiate
897 	 * between callbacks for brightness and bl_power, so our backlight_power
898 	 * callback needs to take this into account.
899 	 */
900 	if (panel->backlight.enabled) {
901 		if (panel->backlight.power) {
902 			bool enable = bd->props.power == FB_BLANK_UNBLANK &&
903 				bd->props.brightness != 0;
904 			panel->backlight.power(connector, enable);
905 		}
906 	} else {
907 		bd->props.power = FB_BLANK_POWERDOWN;
908 	}
909 
910 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
911 	return 0;
912 }
913 
914 static int intel_backlight_device_get_brightness(struct backlight_device *bd)
915 {
916 	struct intel_connector *connector = bl_get_data(bd);
917 	struct drm_device *dev = connector->base.dev;
918 	struct drm_i915_private *dev_priv = to_i915(dev);
919 	intel_wakeref_t wakeref;
920 	int ret = 0;
921 
922 	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
923 		u32 hw_level;
924 
925 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
926 
927 		hw_level = intel_panel_get_backlight(connector);
928 		ret = scale_hw_to_user(connector,
929 				       hw_level, bd->props.max_brightness);
930 
931 		drm_modeset_unlock(&dev->mode_config.connection_mutex);
932 	}
933 
934 	return ret;
935 }
936 
937 static const struct backlight_ops intel_backlight_device_ops = {
938 	.update_status = intel_backlight_device_update_status,
939 	.get_brightness = intel_backlight_device_get_brightness,
940 };
941 
942 int intel_backlight_device_register(struct intel_connector *connector)
943 {
944 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
945 	struct intel_panel *panel = &connector->panel;
946 	struct backlight_properties props;
947 	struct backlight_device *bd;
948 	const char *name;
949 	int ret = 0;
950 
951 	if (WARN_ON(panel->backlight.device))
952 		return -ENODEV;
953 
954 	if (!panel->backlight.present)
955 		return 0;
956 
957 	WARN_ON(panel->backlight.max == 0);
958 
959 	if (!acpi_video_backlight_use_native()) {
960 		drm_info(&i915->drm, "Skipping intel_backlight registration\n");
961 		return 0;
962 	}
963 
964 	memset(&props, 0, sizeof(props));
965 	props.type = BACKLIGHT_RAW;
966 
967 	/*
968 	 * Note: Everything should work even if the backlight device max
969 	 * presented to the userspace is arbitrarily chosen.
970 	 */
971 	props.max_brightness = panel->backlight.max;
972 	props.brightness = scale_hw_to_user(connector,
973 					    panel->backlight.level,
974 					    props.max_brightness);
975 
976 	if (panel->backlight.enabled)
977 		props.power = FB_BLANK_UNBLANK;
978 	else
979 		props.power = FB_BLANK_POWERDOWN;
980 
981 	name = kstrdup("intel_backlight", GFP_KERNEL);
982 	if (!name)
983 		return -ENOMEM;
984 
985 	bd = backlight_device_get_by_name(name);
986 	if (bd) {
987 		put_device(&bd->dev);
988 		/*
989 		 * Using the same name independent of the drm device or connector
990 		 * prevents registration of multiple backlight devices in the
991 		 * driver. However, we need to use the default name for backward
992 		 * compatibility. Use unique names for subsequent backlight devices as a
993 		 * fallback when the default name already exists.
994 		 */
995 		kfree(name);
996 		name = kasprintf(GFP_KERNEL, "card%d-%s-backlight",
997 				 i915->drm.primary->index, connector->base.name);
998 		if (!name)
999 			return -ENOMEM;
1000 	}
1001 	bd = backlight_device_register(name, connector->base.kdev, connector,
1002 				       &intel_backlight_device_ops, &props);
1003 
1004 	if (IS_ERR(bd)) {
1005 		drm_err(&i915->drm,
1006 			"[CONNECTOR:%d:%s] backlight device %s register failed: %ld\n",
1007 			connector->base.base.id, connector->base.name, name, PTR_ERR(bd));
1008 		ret = PTR_ERR(bd);
1009 		goto out;
1010 	}
1011 
1012 	panel->backlight.device = bd;
1013 
1014 	drm_dbg_kms(&i915->drm,
1015 		    "[CONNECTOR:%d:%s] backlight device %s registered\n",
1016 		    connector->base.base.id, connector->base.name, name);
1017 
1018 out:
1019 	kfree(name);
1020 
1021 	return ret;
1022 }
1023 
1024 void intel_backlight_device_unregister(struct intel_connector *connector)
1025 {
1026 	struct intel_panel *panel = &connector->panel;
1027 
1028 	if (panel->backlight.device) {
1029 		backlight_device_unregister(panel->backlight.device);
1030 		panel->backlight.device = NULL;
1031 	}
1032 }
1033 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1034 
1035 /*
1036  * CNP: PWM clock frequency is 19.2 MHz or 24 MHz.
1037  *      PWM increment = 1
1038  */
1039 static u32 cnp_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1040 {
1041 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1042 
1043 	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
1044 				 pwm_freq_hz);
1045 }
1046 
1047 /*
1048  * BXT: PWM clock frequency = 19.2 MHz.
1049  */
1050 static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1051 {
1052 	return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1053 }
1054 
1055 /*
1056  * SPT: This value represents the period of the PWM stream in clock periods
1057  * multiplied by 16 (default increment) or 128 (alternate increment selected in
1058  * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
1059  */
1060 static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1061 {
1062 	struct intel_panel *panel = &connector->panel;
1063 	u32 mul;
1064 
1065 	if (panel->backlight.alternate_pwm_increment)
1066 		mul = 128;
1067 	else
1068 		mul = 16;
1069 
1070 	return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1071 }
1072 
1073 /*
1074  * LPT: This value represents the period of the PWM stream in clock periods
1075  * multiplied by 128 (default increment) or 16 (alternate increment, selected in
1076  * LPT SOUTH_CHICKEN2 register bit 5).
1077  */
1078 static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1079 {
1080 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1081 	struct intel_panel *panel = &connector->panel;
1082 	u32 mul, clock;
1083 
1084 	if (panel->backlight.alternate_pwm_increment)
1085 		mul = 16;
1086 	else
1087 		mul = 128;
1088 
1089 	if (HAS_PCH_LPT_H(dev_priv))
1090 		clock = MHz(135); /* LPT:H */
1091 	else
1092 		clock = MHz(24); /* LPT:LP */
1093 
1094 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1095 }
1096 
1097 /*
1098  * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
1099  * display raw clocks multiplied by 128.
1100  */
1101 static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1102 {
1103 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1104 
1105 	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
1106 				 pwm_freq_hz * 128);
1107 }
1108 
1109 /*
1110  * Gen2: This field determines the number of time base events (display core
1111  * clock frequency/32) in total for a complete cycle of modulated backlight
1112  * control.
1113  *
1114  * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
1115  * divided by 32.
1116  */
1117 static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1118 {
1119 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1120 	int clock;
1121 
1122 	if (IS_PINEVIEW(dev_priv))
1123 		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1124 	else
1125 		clock = KHz(dev_priv->display.cdclk.hw.cdclk);
1126 
1127 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1128 }
1129 
1130 /*
1131  * Gen4: This value represents the period of the PWM stream in display core
1132  * clocks ([DevCTG] HRAW clocks) multiplied by 128.
1133  *
1134  */
1135 static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1136 {
1137 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1138 	int clock;
1139 
1140 	if (IS_G4X(dev_priv))
1141 		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1142 	else
1143 		clock = KHz(dev_priv->display.cdclk.hw.cdclk);
1144 
1145 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1146 }
1147 
1148 /*
1149  * VLV: This value represents the period of the PWM stream in display core
1150  * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
1151  * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
1152  */
1153 static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1154 {
1155 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1156 	int mul, clock;
1157 
1158 	if ((intel_de_read(dev_priv, CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1159 		if (IS_CHERRYVIEW(dev_priv))
1160 			clock = KHz(19200);
1161 		else
1162 			clock = MHz(25);
1163 		mul = 16;
1164 	} else {
1165 		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1166 		mul = 128;
1167 	}
1168 
1169 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1170 }
1171 
1172 static u16 get_vbt_pwm_freq(struct intel_connector *connector)
1173 {
1174 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1175 	u16 pwm_freq_hz = connector->panel.vbt.backlight.pwm_freq_hz;
1176 
1177 	if (pwm_freq_hz) {
1178 		drm_dbg_kms(&dev_priv->drm,
1179 			    "VBT defined backlight frequency %u Hz\n",
1180 			    pwm_freq_hz);
1181 	} else {
1182 		pwm_freq_hz = 200;
1183 		drm_dbg_kms(&dev_priv->drm,
1184 			    "default backlight frequency %u Hz\n",
1185 			    pwm_freq_hz);
1186 	}
1187 
1188 	return pwm_freq_hz;
1189 }
1190 
1191 static u32 get_backlight_max_vbt(struct intel_connector *connector)
1192 {
1193 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1194 	struct intel_panel *panel = &connector->panel;
1195 	u16 pwm_freq_hz = get_vbt_pwm_freq(connector);
1196 	u32 pwm;
1197 
1198 	if (!panel->backlight.pwm_funcs->hz_to_pwm) {
1199 		drm_dbg_kms(&dev_priv->drm,
1200 			    "backlight frequency conversion not supported\n");
1201 		return 0;
1202 	}
1203 
1204 	pwm = panel->backlight.pwm_funcs->hz_to_pwm(connector, pwm_freq_hz);
1205 	if (!pwm) {
1206 		drm_dbg_kms(&dev_priv->drm,
1207 			    "backlight frequency conversion failed\n");
1208 		return 0;
1209 	}
1210 
1211 	return pwm;
1212 }
1213 
1214 /*
1215  * Note: The setup hooks can't assume pipe is set!
1216  */
1217 static u32 get_backlight_min_vbt(struct intel_connector *connector)
1218 {
1219 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1220 	struct intel_panel *panel = &connector->panel;
1221 	int min;
1222 
1223 	drm_WARN_ON(&dev_priv->drm, panel->backlight.pwm_level_max == 0);
1224 
1225 	/*
1226 	 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1227 	 * to problems. There are such machines out there. Either our
1228 	 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1229 	 * against this by letting the minimum be at most (arbitrarily chosen)
1230 	 * 25% of the max.
1231 	 */
1232 	min = clamp_t(int, connector->panel.vbt.backlight.min_brightness, 0, 64);
1233 	if (min != connector->panel.vbt.backlight.min_brightness) {
1234 		drm_dbg_kms(&dev_priv->drm,
1235 			    "clamping VBT min backlight %d/255 to %d/255\n",
1236 			    connector->panel.vbt.backlight.min_brightness, min);
1237 	}
1238 
1239 	/* vbt value is a coefficient in range [0..255] */
1240 	return scale(min, 0, 255, 0, panel->backlight.pwm_level_max);
1241 }
1242 
1243 static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1244 {
1245 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1246 	struct intel_panel *panel = &connector->panel;
1247 	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1248 	bool alt, cpu_mode;
1249 
1250 	if (HAS_PCH_LPT(dev_priv))
1251 		alt = intel_de_read(dev_priv, SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1252 	else
1253 		alt = intel_de_read(dev_priv, SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1254 	panel->backlight.alternate_pwm_increment = alt;
1255 
1256 	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1257 	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1258 
1259 	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1260 	panel->backlight.pwm_level_max = pch_ctl2 >> 16;
1261 
1262 	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1263 
1264 	if (!panel->backlight.pwm_level_max)
1265 		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1266 
1267 	if (!panel->backlight.pwm_level_max)
1268 		return -ENODEV;
1269 
1270 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1271 
1272 	panel->backlight.pwm_enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
1273 
1274 	cpu_mode = panel->backlight.pwm_enabled && HAS_PCH_LPT(dev_priv) &&
1275 		   !(pch_ctl1 & BLM_PCH_OVERRIDE_ENABLE) &&
1276 		   (cpu_ctl2 & BLM_PWM_ENABLE);
1277 
1278 	if (cpu_mode) {
1279 		val = pch_get_backlight(connector, unused);
1280 
1281 		drm_dbg_kms(&dev_priv->drm,
1282 			    "CPU backlight register was enabled, switching to PCH override\n");
1283 
1284 		/* Write converted CPU PWM value to PCH override register */
1285 		lpt_set_backlight(connector->base.state, val);
1286 		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
1287 			       pch_ctl1 | BLM_PCH_OVERRIDE_ENABLE);
1288 
1289 		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
1290 			       cpu_ctl2 & ~BLM_PWM_ENABLE);
1291 	}
1292 
1293 	return 0;
1294 }
1295 
1296 static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
1297 {
1298 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1299 	struct intel_panel *panel = &connector->panel;
1300 	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
1301 
1302 	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1303 	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1304 
1305 	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1306 	panel->backlight.pwm_level_max = pch_ctl2 >> 16;
1307 
1308 	if (!panel->backlight.pwm_level_max)
1309 		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1310 
1311 	if (!panel->backlight.pwm_level_max)
1312 		return -ENODEV;
1313 
1314 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1315 
1316 	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1317 	panel->backlight.pwm_enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1318 		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
1319 
1320 	return 0;
1321 }
1322 
1323 static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1324 {
1325 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1326 	struct intel_panel *panel = &connector->panel;
1327 	u32 ctl, val;
1328 
1329 	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1330 
1331 	if (DISPLAY_VER(dev_priv) == 2 || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1332 		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1333 
1334 	if (IS_PINEVIEW(dev_priv))
1335 		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1336 
1337 	panel->backlight.pwm_level_max = ctl >> 17;
1338 
1339 	if (!panel->backlight.pwm_level_max) {
1340 		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1341 		panel->backlight.pwm_level_max >>= 1;
1342 	}
1343 
1344 	if (!panel->backlight.pwm_level_max)
1345 		return -ENODEV;
1346 
1347 	if (panel->backlight.combination_mode)
1348 		panel->backlight.pwm_level_max *= 0xff;
1349 
1350 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1351 
1352 	val = i9xx_get_backlight(connector, unused);
1353 	val = intel_backlight_invert_pwm_level(connector, val);
1354 	val = clamp(val, panel->backlight.pwm_level_min, panel->backlight.pwm_level_max);
1355 
1356 	panel->backlight.pwm_enabled = val != 0;
1357 
1358 	return 0;
1359 }
1360 
1361 static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1362 {
1363 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1364 	struct intel_panel *panel = &connector->panel;
1365 	u32 ctl, ctl2;
1366 
1367 	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1368 	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1369 	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1370 
1371 	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1372 	panel->backlight.pwm_level_max = ctl >> 16;
1373 
1374 	if (!panel->backlight.pwm_level_max)
1375 		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1376 
1377 	if (!panel->backlight.pwm_level_max)
1378 		return -ENODEV;
1379 
1380 	if (panel->backlight.combination_mode)
1381 		panel->backlight.pwm_level_max *= 0xff;
1382 
1383 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1384 
1385 	panel->backlight.pwm_enabled = ctl2 & BLM_PWM_ENABLE;
1386 
1387 	return 0;
1388 }
1389 
1390 static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1391 {
1392 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1393 	struct intel_panel *panel = &connector->panel;
1394 	u32 ctl, ctl2;
1395 
1396 	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1397 		return -ENODEV;
1398 
1399 	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1400 	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1401 
1402 	ctl = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe));
1403 	panel->backlight.pwm_level_max = ctl >> 16;
1404 
1405 	if (!panel->backlight.pwm_level_max)
1406 		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1407 
1408 	if (!panel->backlight.pwm_level_max)
1409 		return -ENODEV;
1410 
1411 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1412 
1413 	panel->backlight.pwm_enabled = ctl2 & BLM_PWM_ENABLE;
1414 
1415 	return 0;
1416 }
1417 
1418 static int
1419 bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1420 {
1421 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1422 	struct intel_panel *panel = &connector->panel;
1423 	u32 pwm_ctl, val;
1424 
1425 	panel->backlight.controller = connector->panel.vbt.backlight.controller;
1426 
1427 	pwm_ctl = intel_de_read(dev_priv,
1428 				BXT_BLC_PWM_CTL(panel->backlight.controller));
1429 
1430 	/* Controller 1 uses the utility pin. */
1431 	if (panel->backlight.controller == 1) {
1432 		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1433 		panel->backlight.util_pin_active_low =
1434 					val & UTIL_PIN_POLARITY;
1435 	}
1436 
1437 	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1438 	panel->backlight.pwm_level_max =
1439 		intel_de_read(dev_priv, BXT_BLC_PWM_FREQ(panel->backlight.controller));
1440 
1441 	if (!panel->backlight.pwm_level_max)
1442 		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1443 
1444 	if (!panel->backlight.pwm_level_max)
1445 		return -ENODEV;
1446 
1447 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1448 
1449 	panel->backlight.pwm_enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1450 
1451 	return 0;
1452 }
1453 
1454 static int
1455 cnp_setup_backlight(struct intel_connector *connector, enum pipe unused)
1456 {
1457 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1458 	struct intel_panel *panel = &connector->panel;
1459 	u32 pwm_ctl;
1460 
1461 	/*
1462 	 * CNP has the BXT implementation of backlight, but with only one
1463 	 * controller. TODO: ICP has multiple controllers but we only use
1464 	 * controller 0 for now.
1465 	 */
1466 	panel->backlight.controller = 0;
1467 
1468 	pwm_ctl = intel_de_read(dev_priv,
1469 				BXT_BLC_PWM_CTL(panel->backlight.controller));
1470 
1471 	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1472 	panel->backlight.pwm_level_max =
1473 		intel_de_read(dev_priv, BXT_BLC_PWM_FREQ(panel->backlight.controller));
1474 
1475 	if (!panel->backlight.pwm_level_max)
1476 		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1477 
1478 	if (!panel->backlight.pwm_level_max)
1479 		return -ENODEV;
1480 
1481 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1482 
1483 	panel->backlight.pwm_enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1484 
1485 	return 0;
1486 }
1487 
1488 static int ext_pwm_setup_backlight(struct intel_connector *connector,
1489 				   enum pipe pipe)
1490 {
1491 	struct drm_device *dev = connector->base.dev;
1492 	struct drm_i915_private *dev_priv = to_i915(dev);
1493 	struct intel_panel *panel = &connector->panel;
1494 	const char *desc;
1495 	u32 level;
1496 
1497 	/* Get the right PWM chip for DSI backlight according to VBT */
1498 	if (connector->panel.vbt.dsi.config->pwm_blc == PPS_BLC_PMIC) {
1499 		panel->backlight.pwm = pwm_get(dev->dev, "pwm_pmic_backlight");
1500 		desc = "PMIC";
1501 	} else {
1502 		panel->backlight.pwm = pwm_get(dev->dev, "pwm_soc_backlight");
1503 		desc = "SoC";
1504 	}
1505 
1506 	if (IS_ERR(panel->backlight.pwm)) {
1507 		drm_err(&dev_priv->drm, "Failed to get the %s PWM chip\n",
1508 			desc);
1509 		panel->backlight.pwm = NULL;
1510 		return -ENODEV;
1511 	}
1512 
1513 	panel->backlight.pwm_level_max = 100; /* 100% */
1514 	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1515 
1516 	if (pwm_is_enabled(panel->backlight.pwm)) {
1517 		/* PWM is already enabled, use existing settings */
1518 		pwm_get_state(panel->backlight.pwm, &panel->backlight.pwm_state);
1519 
1520 		level = pwm_get_relative_duty_cycle(&panel->backlight.pwm_state,
1521 						    100);
1522 		level = intel_backlight_invert_pwm_level(connector, level);
1523 		panel->backlight.pwm_enabled = true;
1524 
1525 		drm_dbg_kms(&dev_priv->drm, "PWM already enabled at freq %ld, VBT freq %d, level %d\n",
1526 			    NSEC_PER_SEC / (unsigned long)panel->backlight.pwm_state.period,
1527 			    get_vbt_pwm_freq(connector), level);
1528 	} else {
1529 		/* Set period from VBT frequency, leave other settings at 0. */
1530 		panel->backlight.pwm_state.period =
1531 			NSEC_PER_SEC / get_vbt_pwm_freq(connector);
1532 	}
1533 
1534 	drm_info(&dev_priv->drm, "Using %s PWM for LCD backlight control\n",
1535 		 desc);
1536 	return 0;
1537 }
1538 
1539 static void intel_pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
1540 {
1541 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1542 	struct intel_panel *panel = &connector->panel;
1543 
1544 	panel->backlight.pwm_funcs->set(conn_state,
1545 					intel_backlight_invert_pwm_level(connector, level));
1546 }
1547 
1548 static u32 intel_pwm_get_backlight(struct intel_connector *connector, enum pipe pipe)
1549 {
1550 	struct intel_panel *panel = &connector->panel;
1551 
1552 	return intel_backlight_invert_pwm_level(connector,
1553 					    panel->backlight.pwm_funcs->get(connector, pipe));
1554 }
1555 
1556 static void intel_pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
1557 				       const struct drm_connector_state *conn_state, u32 level)
1558 {
1559 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1560 	struct intel_panel *panel = &connector->panel;
1561 
1562 	panel->backlight.pwm_funcs->enable(crtc_state, conn_state,
1563 					   intel_backlight_invert_pwm_level(connector, level));
1564 }
1565 
1566 static void intel_pwm_disable_backlight(const struct drm_connector_state *conn_state, u32 level)
1567 {
1568 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1569 	struct intel_panel *panel = &connector->panel;
1570 
1571 	panel->backlight.pwm_funcs->disable(conn_state,
1572 					    intel_backlight_invert_pwm_level(connector, level));
1573 }
1574 
1575 static int intel_pwm_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1576 {
1577 	struct intel_panel *panel = &connector->panel;
1578 	int ret = panel->backlight.pwm_funcs->setup(connector, pipe);
1579 
1580 	if (ret < 0)
1581 		return ret;
1582 
1583 	panel->backlight.min = panel->backlight.pwm_level_min;
1584 	panel->backlight.max = panel->backlight.pwm_level_max;
1585 	panel->backlight.level = intel_pwm_get_backlight(connector, pipe);
1586 	panel->backlight.enabled = panel->backlight.pwm_enabled;
1587 
1588 	return 0;
1589 }
1590 
1591 void intel_backlight_update(struct intel_atomic_state *state,
1592 			    struct intel_encoder *encoder,
1593 			    const struct intel_crtc_state *crtc_state,
1594 			    const struct drm_connector_state *conn_state)
1595 {
1596 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1597 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1598 	struct intel_panel *panel = &connector->panel;
1599 
1600 	if (!panel->backlight.present)
1601 		return;
1602 
1603 	mutex_lock(&dev_priv->display.backlight.lock);
1604 	if (!panel->backlight.enabled)
1605 		__intel_backlight_enable(crtc_state, conn_state);
1606 
1607 	mutex_unlock(&dev_priv->display.backlight.lock);
1608 }
1609 
1610 int intel_backlight_setup(struct intel_connector *connector, enum pipe pipe)
1611 {
1612 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1613 	struct intel_panel *panel = &connector->panel;
1614 	int ret;
1615 
1616 	if (!connector->panel.vbt.backlight.present) {
1617 		if (intel_has_quirk(dev_priv, QUIRK_BACKLIGHT_PRESENT)) {
1618 			drm_dbg_kms(&dev_priv->drm,
1619 				    "no backlight present per VBT, but present per quirk\n");
1620 		} else {
1621 			drm_dbg_kms(&dev_priv->drm,
1622 				    "no backlight present per VBT\n");
1623 			return 0;
1624 		}
1625 	}
1626 
1627 	/* ensure intel_panel has been initialized first */
1628 	if (drm_WARN_ON(&dev_priv->drm, !panel->backlight.funcs))
1629 		return -ENODEV;
1630 
1631 	/* set level and max in panel struct */
1632 	mutex_lock(&dev_priv->display.backlight.lock);
1633 	ret = panel->backlight.funcs->setup(connector, pipe);
1634 	mutex_unlock(&dev_priv->display.backlight.lock);
1635 
1636 	if (ret) {
1637 		drm_dbg_kms(&dev_priv->drm,
1638 			    "failed to setup backlight for connector %s\n",
1639 			    connector->base.name);
1640 		return ret;
1641 	}
1642 
1643 	panel->backlight.present = true;
1644 
1645 	drm_dbg_kms(&dev_priv->drm,
1646 		    "Connector %s backlight initialized, %s, brightness %u/%u\n",
1647 		    connector->base.name,
1648 		    str_enabled_disabled(panel->backlight.enabled),
1649 		    panel->backlight.level, panel->backlight.max);
1650 
1651 	return 0;
1652 }
1653 
1654 void intel_backlight_destroy(struct intel_panel *panel)
1655 {
1656 	/* dispose of the pwm */
1657 	if (panel->backlight.pwm)
1658 		pwm_put(panel->backlight.pwm);
1659 
1660 	panel->backlight.present = false;
1661 }
1662 
1663 static const struct intel_panel_bl_funcs bxt_pwm_funcs = {
1664 	.setup = bxt_setup_backlight,
1665 	.enable = bxt_enable_backlight,
1666 	.disable = bxt_disable_backlight,
1667 	.set = bxt_set_backlight,
1668 	.get = bxt_get_backlight,
1669 	.hz_to_pwm = bxt_hz_to_pwm,
1670 };
1671 
1672 static const struct intel_panel_bl_funcs cnp_pwm_funcs = {
1673 	.setup = cnp_setup_backlight,
1674 	.enable = cnp_enable_backlight,
1675 	.disable = cnp_disable_backlight,
1676 	.set = bxt_set_backlight,
1677 	.get = bxt_get_backlight,
1678 	.hz_to_pwm = cnp_hz_to_pwm,
1679 };
1680 
1681 static const struct intel_panel_bl_funcs lpt_pwm_funcs = {
1682 	.setup = lpt_setup_backlight,
1683 	.enable = lpt_enable_backlight,
1684 	.disable = lpt_disable_backlight,
1685 	.set = lpt_set_backlight,
1686 	.get = lpt_get_backlight,
1687 	.hz_to_pwm = lpt_hz_to_pwm,
1688 };
1689 
1690 static const struct intel_panel_bl_funcs spt_pwm_funcs = {
1691 	.setup = lpt_setup_backlight,
1692 	.enable = lpt_enable_backlight,
1693 	.disable = lpt_disable_backlight,
1694 	.set = lpt_set_backlight,
1695 	.get = lpt_get_backlight,
1696 	.hz_to_pwm = spt_hz_to_pwm,
1697 };
1698 
1699 static const struct intel_panel_bl_funcs pch_pwm_funcs = {
1700 	.setup = pch_setup_backlight,
1701 	.enable = pch_enable_backlight,
1702 	.disable = pch_disable_backlight,
1703 	.set = pch_set_backlight,
1704 	.get = pch_get_backlight,
1705 	.hz_to_pwm = pch_hz_to_pwm,
1706 };
1707 
1708 static const struct intel_panel_bl_funcs ext_pwm_funcs = {
1709 	.setup = ext_pwm_setup_backlight,
1710 	.enable = ext_pwm_enable_backlight,
1711 	.disable = ext_pwm_disable_backlight,
1712 	.set = ext_pwm_set_backlight,
1713 	.get = ext_pwm_get_backlight,
1714 };
1715 
1716 static const struct intel_panel_bl_funcs vlv_pwm_funcs = {
1717 	.setup = vlv_setup_backlight,
1718 	.enable = vlv_enable_backlight,
1719 	.disable = vlv_disable_backlight,
1720 	.set = vlv_set_backlight,
1721 	.get = vlv_get_backlight,
1722 	.hz_to_pwm = vlv_hz_to_pwm,
1723 };
1724 
1725 static const struct intel_panel_bl_funcs i965_pwm_funcs = {
1726 	.setup = i965_setup_backlight,
1727 	.enable = i965_enable_backlight,
1728 	.disable = i965_disable_backlight,
1729 	.set = i9xx_set_backlight,
1730 	.get = i9xx_get_backlight,
1731 	.hz_to_pwm = i965_hz_to_pwm,
1732 };
1733 
1734 static const struct intel_panel_bl_funcs i9xx_pwm_funcs = {
1735 	.setup = i9xx_setup_backlight,
1736 	.enable = i9xx_enable_backlight,
1737 	.disable = i9xx_disable_backlight,
1738 	.set = i9xx_set_backlight,
1739 	.get = i9xx_get_backlight,
1740 	.hz_to_pwm = i9xx_hz_to_pwm,
1741 };
1742 
1743 static const struct intel_panel_bl_funcs pwm_bl_funcs = {
1744 	.setup = intel_pwm_setup_backlight,
1745 	.enable = intel_pwm_enable_backlight,
1746 	.disable = intel_pwm_disable_backlight,
1747 	.set = intel_pwm_set_backlight,
1748 	.get = intel_pwm_get_backlight,
1749 };
1750 
1751 /* Set up chip specific backlight functions */
1752 void intel_backlight_init_funcs(struct intel_panel *panel)
1753 {
1754 	struct intel_connector *connector =
1755 		container_of(panel, struct intel_connector, panel);
1756 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1757 
1758 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
1759 	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
1760 		return;
1761 
1762 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1763 		panel->backlight.pwm_funcs = &bxt_pwm_funcs;
1764 	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP) {
1765 		panel->backlight.pwm_funcs = &cnp_pwm_funcs;
1766 	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_LPT) {
1767 		if (HAS_PCH_LPT(dev_priv))
1768 			panel->backlight.pwm_funcs = &lpt_pwm_funcs;
1769 		else
1770 			panel->backlight.pwm_funcs = &spt_pwm_funcs;
1771 	} else if (HAS_PCH_SPLIT(dev_priv)) {
1772 		panel->backlight.pwm_funcs = &pch_pwm_funcs;
1773 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1774 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
1775 			panel->backlight.pwm_funcs = &ext_pwm_funcs;
1776 		} else {
1777 			panel->backlight.pwm_funcs = &vlv_pwm_funcs;
1778 		}
1779 	} else if (DISPLAY_VER(dev_priv) == 4) {
1780 		panel->backlight.pwm_funcs = &i965_pwm_funcs;
1781 	} else {
1782 		panel->backlight.pwm_funcs = &i9xx_pwm_funcs;
1783 	}
1784 
1785 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP) {
1786 		if (intel_dp_aux_init_backlight_funcs(connector) == 0)
1787 			return;
1788 
1789 		if (!intel_has_quirk(dev_priv, QUIRK_NO_PPS_BACKLIGHT_POWER_HOOK))
1790 			connector->panel.backlight.power = intel_pps_backlight_power;
1791 	}
1792 
1793 	/* We're using a standard PWM backlight interface */
1794 	panel->backlight.funcs = &pwm_bl_funcs;
1795 }
1796