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