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