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