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