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