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