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