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