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