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