1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. 4 */ 5 6 #include <linux/acpi.h> 7 #include <linux/backlight.h> 8 #include <linux/mod_devicetable.h> 9 #include <linux/module.h> 10 #include <linux/platform_data/x86/nvidia-wmi-ec-backlight.h> 11 #include <linux/types.h> 12 #include <linux/wmi.h> 13 #include <acpi/video.h> 14 15 /** 16 * wmi_brightness_notify() - helper function for calling WMI-wrapped ACPI method 17 * @w: Pointer to the struct wmi_device identified by %WMI_BRIGHTNESS_GUID 18 * @id: The WMI method ID to call (e.g. %WMI_BRIGHTNESS_METHOD_LEVEL or 19 * %WMI_BRIGHTNESS_METHOD_SOURCE) 20 * @mode: The operation to perform on the method (e.g. %WMI_BRIGHTNESS_MODE_SET 21 * or %WMI_BRIGHTNESS_MODE_GET) 22 * @val: Pointer to a value passed in by the caller when @mode is 23 * %WMI_BRIGHTNESS_MODE_SET, or a value passed out to caller when @mode 24 * is %WMI_BRIGHTNESS_MODE_GET or %WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL. 25 * 26 * Returns 0 on success, or a negative error number on failure. 27 */ 28 static int wmi_brightness_notify(struct wmi_device *w, enum wmi_brightness_method id, enum wmi_brightness_mode mode, u32 *val) 29 { 30 struct wmi_brightness_args args = { 31 .mode = mode, 32 .val = 0, 33 .ret = 0, 34 }; 35 struct acpi_buffer buf = { (acpi_size)sizeof(args), &args }; 36 acpi_status status; 37 38 if (id < WMI_BRIGHTNESS_METHOD_LEVEL || 39 id >= WMI_BRIGHTNESS_METHOD_MAX || 40 mode < WMI_BRIGHTNESS_MODE_GET || mode >= WMI_BRIGHTNESS_MODE_MAX) 41 return -EINVAL; 42 43 if (mode == WMI_BRIGHTNESS_MODE_SET) 44 args.val = *val; 45 46 status = wmidev_evaluate_method(w, 0, id, &buf, &buf); 47 if (ACPI_FAILURE(status)) { 48 dev_err(&w->dev, "EC backlight control failed: %s\n", 49 acpi_format_exception(status)); 50 return -EIO; 51 } 52 53 if (mode != WMI_BRIGHTNESS_MODE_SET) 54 *val = args.ret; 55 56 return 0; 57 } 58 59 static int nvidia_wmi_ec_backlight_update_status(struct backlight_device *bd) 60 { 61 struct wmi_device *wdev = bl_get_data(bd); 62 63 return wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 64 WMI_BRIGHTNESS_MODE_SET, 65 &bd->props.brightness); 66 } 67 68 static int nvidia_wmi_ec_backlight_get_brightness(struct backlight_device *bd) 69 { 70 struct wmi_device *wdev = bl_get_data(bd); 71 u32 level; 72 int ret; 73 74 ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 75 WMI_BRIGHTNESS_MODE_GET, &level); 76 if (ret < 0) 77 return ret; 78 79 return level; 80 } 81 82 static const struct backlight_ops nvidia_wmi_ec_backlight_ops = { 83 .update_status = nvidia_wmi_ec_backlight_update_status, 84 .get_brightness = nvidia_wmi_ec_backlight_get_brightness, 85 }; 86 87 static int nvidia_wmi_ec_backlight_probe(struct wmi_device *wdev, const void *ctx) 88 { 89 struct backlight_properties props = {}; 90 struct backlight_device *bdev; 91 int ret; 92 93 /* drivers/acpi/video_detect.c also checks that SOURCE == EC */ 94 if (acpi_video_get_backlight_type() != acpi_backlight_nvidia_wmi_ec) 95 return -ENODEV; 96 97 /* 98 * Identify this backlight device as a firmware device so that it can 99 * be prioritized over any exposed GPU-driven raw device(s). 100 */ 101 props.type = BACKLIGHT_FIRMWARE; 102 103 ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 104 WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL, 105 &props.max_brightness); 106 if (ret) 107 return ret; 108 109 ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 110 WMI_BRIGHTNESS_MODE_GET, &props.brightness); 111 if (ret) 112 return ret; 113 114 bdev = devm_backlight_device_register(&wdev->dev, 115 "nvidia_wmi_ec_backlight", 116 &wdev->dev, wdev, 117 &nvidia_wmi_ec_backlight_ops, 118 &props); 119 return PTR_ERR_OR_ZERO(bdev); 120 } 121 122 static const struct wmi_device_id nvidia_wmi_ec_backlight_id_table[] = { 123 { .guid_string = WMI_BRIGHTNESS_GUID }, 124 { } 125 }; 126 MODULE_DEVICE_TABLE(wmi, nvidia_wmi_ec_backlight_id_table); 127 128 static struct wmi_driver nvidia_wmi_ec_backlight_driver = { 129 .driver = { 130 .name = "nvidia-wmi-ec-backlight", 131 }, 132 .probe = nvidia_wmi_ec_backlight_probe, 133 .id_table = nvidia_wmi_ec_backlight_id_table, 134 }; 135 module_wmi_driver(nvidia_wmi_ec_backlight_driver); 136 137 MODULE_AUTHOR("Daniel Dadap <ddadap@nvidia.com>"); 138 MODULE_DESCRIPTION("NVIDIA WMI EC Backlight driver"); 139 MODULE_LICENSE("GPL"); 140