platform/loongarch: laptop: Add backlight power control support

loongson_laptop_turn_{on,off}_backlight() are designed for controlling
the power of the backlight, but they aren't really used in the driver
previously.

Unify these two functions since they only differ in arguments passed to
ACPI method, and wire up loongson_laptop_backlight_update() to update
the power state of the backlight as well. Tested on the TongFang L860-T2
Loongson-3A5000 laptop.

Cc: stable@vger.kernel.org
Fixes: 6246ed09111f ("LoongArch: Add ACPI-based generic laptop driver")
Signed-off-by: Yao Zi <ziyao@disroot.org>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>

authored by Yao Zi and committed by Huacai Chen 53c762b4 1205088f

+37 -36
+37 -36
drivers/platform/loongarch/loongson-laptop.c
··· 56 56 static acpi_handle hotkey_handle; 57 57 static struct key_entry hotkey_keycode_map[GENERIC_HOTKEY_MAP_MAX]; 58 58 59 - int loongson_laptop_turn_on_backlight(void); 60 - int loongson_laptop_turn_off_backlight(void); 59 + static bool bl_powered; 61 60 static int loongson_laptop_backlight_update(struct backlight_device *bd); 62 61 63 62 /* 2. ACPI Helpers and device model */ ··· 353 354 return level; 354 355 } 355 356 357 + static int ec_backlight_set_power(bool state) 358 + { 359 + int status; 360 + union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 361 + struct acpi_object_list args = { 1, &arg0 }; 362 + 363 + arg0.integer.value = state; 364 + status = acpi_evaluate_object(NULL, "\\BLSW", &args, NULL); 365 + if (ACPI_FAILURE(status)) { 366 + pr_info("Loongson lvds error: 0x%x\n", status); 367 + return -EIO; 368 + } 369 + 370 + return 0; 371 + } 372 + 356 373 static int loongson_laptop_backlight_update(struct backlight_device *bd) 357 374 { 358 - int lvl = ec_backlight_level(bd->props.brightness); 375 + bool target_powered = !backlight_is_blank(bd); 376 + int ret = 0, lvl = ec_backlight_level(bd->props.brightness); 359 377 360 378 if (lvl < 0) 361 379 return -EIO; 380 + 362 381 if (ec_set_brightness(lvl)) 363 382 return -EIO; 364 383 365 - return 0; 384 + if (target_powered != bl_powered) { 385 + ret = ec_backlight_set_power(target_powered); 386 + if (ret < 0) 387 + return ret; 388 + 389 + bl_powered = target_powered; 390 + } 391 + 392 + return ret; 366 393 } 367 394 368 395 static int loongson_laptop_get_brightness(struct backlight_device *bd) ··· 409 384 410 385 static int laptop_backlight_register(void) 411 386 { 412 - int status = 0; 387 + int status = 0, ret; 413 388 struct backlight_properties props; 414 389 415 390 memset(&props, 0, sizeof(props)); ··· 417 392 if (!acpi_evalf(hotkey_handle, &status, "ECLL", "d")) 418 393 return -EIO; 419 394 395 + ret = ec_backlight_set_power(true); 396 + if (ret) 397 + return ret; 398 + 399 + bl_powered = true; 400 + 420 401 props.max_brightness = status; 421 402 props.brightness = ec_get_brightness(); 403 + props.power = BACKLIGHT_POWER_ON; 422 404 props.type = BACKLIGHT_PLATFORM; 423 405 424 406 backlight_device_register("loongson_laptop", 425 407 NULL, NULL, &backlight_laptop_ops, &props); 426 408 427 - return 0; 428 - } 429 - 430 - int loongson_laptop_turn_on_backlight(void) 431 - { 432 - int status; 433 - union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 434 - struct acpi_object_list args = { 1, &arg0 }; 435 - 436 - arg0.integer.value = 1; 437 - status = acpi_evaluate_object(NULL, "\\BLSW", &args, NULL); 438 - if (ACPI_FAILURE(status)) { 439 - pr_info("Loongson lvds error: 0x%x\n", status); 440 - return -ENODEV; 441 - } 442 - 443 - return 0; 444 - } 445 - 446 - int loongson_laptop_turn_off_backlight(void) 447 - { 448 - int status; 449 - union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 450 - struct acpi_object_list args = { 1, &arg0 }; 451 - 452 - arg0.integer.value = 0; 453 - status = acpi_evaluate_object(NULL, "\\BLSW", &args, NULL); 454 - if (ACPI_FAILURE(status)) { 455 - pr_info("Loongson lvds error: 0x%x\n", status); 456 - return -ENODEV; 457 - } 458 409 459 410 return 0; 460 411 }