nvidia-wmi-ec-backlight.c (7121B)
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/types.h> 11#include <linux/wmi.h> 12 13/** 14 * enum wmi_brightness_method - WMI method IDs 15 * @WMI_BRIGHTNESS_METHOD_LEVEL: Get/Set EC brightness level status 16 * @WMI_BRIGHTNESS_METHOD_SOURCE: Get/Set EC Brightness Source 17 */ 18enum wmi_brightness_method { 19 WMI_BRIGHTNESS_METHOD_LEVEL = 1, 20 WMI_BRIGHTNESS_METHOD_SOURCE = 2, 21 WMI_BRIGHTNESS_METHOD_MAX 22}; 23 24/** 25 * enum wmi_brightness_mode - Operation mode for WMI-wrapped method 26 * @WMI_BRIGHTNESS_MODE_GET: Get the current brightness level/source. 27 * @WMI_BRIGHTNESS_MODE_SET: Set the brightness level. 28 * @WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL: Get the maximum brightness level. This 29 * is only valid when the WMI method is 30 * %WMI_BRIGHTNESS_METHOD_LEVEL. 31 */ 32enum wmi_brightness_mode { 33 WMI_BRIGHTNESS_MODE_GET = 0, 34 WMI_BRIGHTNESS_MODE_SET = 1, 35 WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL = 2, 36 WMI_BRIGHTNESS_MODE_MAX 37}; 38 39/** 40 * enum wmi_brightness_source - Backlight brightness control source selection 41 * @WMI_BRIGHTNESS_SOURCE_GPU: Backlight brightness is controlled by the GPU. 42 * @WMI_BRIGHTNESS_SOURCE_EC: Backlight brightness is controlled by the 43 * system's Embedded Controller (EC). 44 * @WMI_BRIGHTNESS_SOURCE_AUX: Backlight brightness is controlled over the 45 * DisplayPort AUX channel. 46 */ 47enum wmi_brightness_source { 48 WMI_BRIGHTNESS_SOURCE_GPU = 1, 49 WMI_BRIGHTNESS_SOURCE_EC = 2, 50 WMI_BRIGHTNESS_SOURCE_AUX = 3, 51 WMI_BRIGHTNESS_SOURCE_MAX 52}; 53 54/** 55 * struct wmi_brightness_args - arguments for the WMI-wrapped ACPI method 56 * @mode: Pass in an &enum wmi_brightness_mode value to select between 57 * getting or setting a value. 58 * @val: In parameter for value to set when using %WMI_BRIGHTNESS_MODE_SET 59 * mode. Not used in conjunction with %WMI_BRIGHTNESS_MODE_GET or 60 * %WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL mode. 61 * @ret: Out parameter returning retrieved value when operating in 62 * %WMI_BRIGHTNESS_MODE_GET or %WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL 63 * mode. Not used in %WMI_BRIGHTNESS_MODE_SET mode. 64 * @ignored: Padding; not used. The ACPI method expects a 24 byte params struct. 65 * 66 * This is the parameters structure for the WmiBrightnessNotify ACPI method as 67 * wrapped by WMI. The value passed in to @val or returned by @ret will be a 68 * brightness value when the WMI method ID is %WMI_BRIGHTNESS_METHOD_LEVEL, or 69 * an &enum wmi_brightness_source value with %WMI_BRIGHTNESS_METHOD_SOURCE. 70 */ 71struct wmi_brightness_args { 72 u32 mode; 73 u32 val; 74 u32 ret; 75 u32 ignored[3]; 76}; 77 78/** 79 * wmi_brightness_notify() - helper function for calling WMI-wrapped ACPI method 80 * @w: Pointer to the struct wmi_device identified by %WMI_BRIGHTNESS_GUID 81 * @id: The WMI method ID to call (e.g. %WMI_BRIGHTNESS_METHOD_LEVEL or 82 * %WMI_BRIGHTNESS_METHOD_SOURCE) 83 * @mode: The operation to perform on the method (e.g. %WMI_BRIGHTNESS_MODE_SET 84 * or %WMI_BRIGHTNESS_MODE_GET) 85 * @val: Pointer to a value passed in by the caller when @mode is 86 * %WMI_BRIGHTNESS_MODE_SET, or a value passed out to caller when @mode 87 * is %WMI_BRIGHTNESS_MODE_GET or %WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL. 88 * 89 * Returns 0 on success, or a negative error number on failure. 90 */ 91static int wmi_brightness_notify(struct wmi_device *w, enum wmi_brightness_method id, enum wmi_brightness_mode mode, u32 *val) 92{ 93 struct wmi_brightness_args args = { 94 .mode = mode, 95 .val = 0, 96 .ret = 0, 97 }; 98 struct acpi_buffer buf = { (acpi_size)sizeof(args), &args }; 99 acpi_status status; 100 101 if (id < WMI_BRIGHTNESS_METHOD_LEVEL || 102 id >= WMI_BRIGHTNESS_METHOD_MAX || 103 mode < WMI_BRIGHTNESS_MODE_GET || mode >= WMI_BRIGHTNESS_MODE_MAX) 104 return -EINVAL; 105 106 if (mode == WMI_BRIGHTNESS_MODE_SET) 107 args.val = *val; 108 109 status = wmidev_evaluate_method(w, 0, id, &buf, &buf); 110 if (ACPI_FAILURE(status)) { 111 dev_err(&w->dev, "EC backlight control failed: %s\n", 112 acpi_format_exception(status)); 113 return -EIO; 114 } 115 116 if (mode != WMI_BRIGHTNESS_MODE_SET) 117 *val = args.ret; 118 119 return 0; 120} 121 122static int nvidia_wmi_ec_backlight_update_status(struct backlight_device *bd) 123{ 124 struct wmi_device *wdev = bl_get_data(bd); 125 126 return wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 127 WMI_BRIGHTNESS_MODE_SET, 128 &bd->props.brightness); 129} 130 131static int nvidia_wmi_ec_backlight_get_brightness(struct backlight_device *bd) 132{ 133 struct wmi_device *wdev = bl_get_data(bd); 134 u32 level; 135 int ret; 136 137 ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 138 WMI_BRIGHTNESS_MODE_GET, &level); 139 if (ret < 0) 140 return ret; 141 142 return level; 143} 144 145static const struct backlight_ops nvidia_wmi_ec_backlight_ops = { 146 .update_status = nvidia_wmi_ec_backlight_update_status, 147 .get_brightness = nvidia_wmi_ec_backlight_get_brightness, 148}; 149 150static int nvidia_wmi_ec_backlight_probe(struct wmi_device *wdev, const void *ctx) 151{ 152 struct backlight_properties props = {}; 153 struct backlight_device *bdev; 154 u32 source; 155 int ret; 156 157 ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_SOURCE, 158 WMI_BRIGHTNESS_MODE_GET, &source); 159 if (ret) 160 return ret; 161 162 /* 163 * This driver is only to be used when brightness control is handled 164 * by the EC; otherwise, the GPU driver(s) should control brightness. 165 */ 166 if (source != WMI_BRIGHTNESS_SOURCE_EC) 167 return -ENODEV; 168 169 /* 170 * Identify this backlight device as a firmware device so that it can 171 * be prioritized over any exposed GPU-driven raw device(s). 172 */ 173 props.type = BACKLIGHT_FIRMWARE; 174 175 ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 176 WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL, 177 &props.max_brightness); 178 if (ret) 179 return ret; 180 181 ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL, 182 WMI_BRIGHTNESS_MODE_GET, &props.brightness); 183 if (ret) 184 return ret; 185 186 bdev = devm_backlight_device_register(&wdev->dev, 187 "nvidia_wmi_ec_backlight", 188 &wdev->dev, wdev, 189 &nvidia_wmi_ec_backlight_ops, 190 &props); 191 return PTR_ERR_OR_ZERO(bdev); 192} 193 194#define WMI_BRIGHTNESS_GUID "603E9613-EF25-4338-A3D0-C46177516DB7" 195 196static const struct wmi_device_id nvidia_wmi_ec_backlight_id_table[] = { 197 { .guid_string = WMI_BRIGHTNESS_GUID }, 198 { } 199}; 200MODULE_DEVICE_TABLE(wmi, nvidia_wmi_ec_backlight_id_table); 201 202static struct wmi_driver nvidia_wmi_ec_backlight_driver = { 203 .driver = { 204 .name = "nvidia-wmi-ec-backlight", 205 }, 206 .probe = nvidia_wmi_ec_backlight_probe, 207 .id_table = nvidia_wmi_ec_backlight_id_table, 208}; 209module_wmi_driver(nvidia_wmi_ec_backlight_driver); 210 211MODULE_AUTHOR("Daniel Dadap <ddadap@nvidia.com>"); 212MODULE_DESCRIPTION("NVIDIA WMI EC Backlight driver"); 213MODULE_LICENSE("GPL");