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