From patchwork Thu Jan 2 00:47:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derek John Clark X-Patchwork-Id: 13924286 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C14EEC0; Thu, 2 Jan 2025 00:49:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778948; cv=none; b=aIt/oTLhRUtEuj56vqi16jnqI7WlkGr5goa/kyYQuoG2W0sJVE1VqFFpz591UPaTDLHusOSfaYD654i79QEtM3oHmwlpRSPGv2vOM0cbfc7rT4sDBUZnD87ie8/pvvRq2ur8F5GLcw2Svm0Gl+TpQPiLlEirk7Tmsfkv0ET7cXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778948; c=relaxed/simple; bh=Fm8umMG1j2xGrV+1jdi95x1bQHup8Am9TCudsCMhPmo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ui53Es9zd1lQqVqgk+8xguudBnnWTEBN38B1Xj8Dr99mcV5AIRFLwdGVW33ZC/hLGaCRM5f1oJmQyhHGhu28h29njQbC6leuSVOZAVFHD9CoBV+EZtZ0xF+4F96K7rMkf/l7cwI2Amu5jZ1vkSjbhNITLCdqxD958h2fbJZpwiA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Wnkg7rB4; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Wnkg7rB4" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2162c0f6a39so155296845ad.0; Wed, 01 Jan 2025 16:49:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735778946; x=1736383746; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=i9SaF2LkjqbaLIv6FhYd2/XFxjdaP39aNDBiwfIJ0Q8=; b=Wnkg7rB4MVaCKWq445qbByJ82tLPpYtSnt02ahFEpTkN7k1E307zlK5ke8tAc4WXpH EXkjDB1/gczgatCBzFORdQO7+2uY8iDV6KDAi46+nLJh6h36q4SAUFkMXBB2fLuzelkm H4K5F3vUsCUlSKUNyg2zfyk3MkHomCk+4YGo/G7rWV98b5wYkTJJLHeXsZtZrDp/KEHv EGlhwBVguJiZVDY2VOyWz+7E4YpaitlTG3HtsPENEousQUC2ZhrHdk/zcJWQy0FzaGTV hrrZI8h9IBaiOhpbSO/MYUDseihg6CFNxokJn0FtUHFS9ytZ6KHfwpTGQgV56vxftYPI 7x/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735778946; x=1736383746; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i9SaF2LkjqbaLIv6FhYd2/XFxjdaP39aNDBiwfIJ0Q8=; b=DcmnLT+tqCDtj2ZENmrJpDlOzxSaP0K7VCU9/wbvDmMczyUawcCq4WPz/u0C4mytRG ZxGHjBu7kK1zOEJmcEt29eEs2pb+9zgMsce5ILDYkcNuDs21d90kFNU20NxJHKgpCZyA y7xG9aM6FNnAPwO7cnbZX/EfGfNz7A6bDOcJiQctUa7sZlFMBt3ZJWX3xQY3mU+UwUOm LwVd5Qoabo6yQflqE7IZ/IuyQkyZiRBsxwmiwZ/pPJlKrNLuLvIeZXmvtLf6Mdo1bL3N SO2nhmfUWci7xG7ziFwBWPg5kSpOq3s8OpI4F6u5Efke22Ggl6QCM1peTDloWJedNEmv +tzA== X-Forwarded-Encrypted: i=1; AJvYcCWEvFL3SsO5EQV4tKvu4VBaayYgTMCxYfjHLc9/mwlpKGYSw7ktCmC+93RBWuMLK9qIh1+89JO/dpQ=@vger.kernel.org, AJvYcCX4qJ3kG6Vxem+O8BMScnHq9dEYIz7EmNcR5BHKHwUqWx8iqS+Dbq/kNKf6QHA49ETy4YQoSvZF34Br4xN0@vger.kernel.org, AJvYcCXTz3JI0o73tyoGBdWYl4yXaBP4dfc2xx6Az2U4VGJ5XectNAwnuDVSeyKXn+cvJ52DSkk0TDexo1QxmtkM0/Eq2XlcgA==@vger.kernel.org X-Gm-Message-State: AOJu0YzF0jb/ZjFSrI9zYKJgSgmydExrjlOTAupZoF67ZjdlgfPj8ZpX F1y7Uyn6ovDt9wuuXo4M7BmU/FsRo5KOj+hbyrlJpEe24FrEt5VC X-Gm-Gg: ASbGncvrfkMOZuqrsIjOaGJ4b2uOXErim3Zf7tuQ8Oke9w22tDZosZBGKPRUdw2qNDw AaxtYZTsDm3Kw/UtAcOiOJgKtn3kJNdYAZKX9nMUy/uHG6BHOXgyGN4kNi7Mfyyx4XPuZ1i37Mc mmNbMh7ojCLVbjn0jVX0VFM0H4frtBOb4qYzrKIJWmZNpJMv55lDvJ/UjnDpz/8Mc6lN2OLYxEC Vw9o5mVE7w4JFEPLa0BK5mE6YXCmVZfaS9SxLPHgYjBXY7OpKwu7tsGxEtA26aJgUolvvz8ynDR SLSEoGLsUKHhbYcY+28FQeQgltpbFBDLNb6hTQC+CQoCkwUgL8hOrS4= X-Google-Smtp-Source: AGHT+IGK7a5ZGvU13YLSZ+73XHxv0quTwggdFT/Y5fNNqQCIY9l7UbqVUcqb/gHT7b+pOWVKQ1Tjyg== X-Received: by 2002:a05:6a21:3989:b0:1d9:a94:feec with SMTP id adf61e73a8af0-1e5e1e0460bmr74569802637.2.1735778946447; Wed, 01 Jan 2025 16:49:06 -0800 (PST) Received: from localhost.localdomain (108-228-232-20.lightspeed.sndgca.sbcglobal.net. [108.228.232.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad8351fasm23793594b3a.62.2025.01.01.16.49.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 16:49:05 -0800 (PST) From: "Derek J. Clark" To: Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= Cc: Jonathan Corbet , Mario Limonciello , Luke Jones , Xino Ni , Zhixin Zhang , Mia Shao , Mark Pearson , "Pierre-Loup A . Griffais" , "Cody T . -H . Chiu" , John Martens , "Derek J . Clark" , platform-driver-x86@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/4] platform/x86: Add lenovo-wmi drivers Documentation Date: Wed, 1 Jan 2025 16:47:19 -0800 Message-ID: <20250102004854.14874-2-derekjohn.clark@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250102004854.14874-1-derekjohn.clark@gmail.com> References: <20250102004854.14874-1-derekjohn.clark@gmail.com> Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adds documentation for all lenovo-wmi* drivers. v2: - Update description of Custom Profile to include the need to manually set it. - Remove all references to Legion hardware. - Add section for lemovo-wmi-camera.c driver as it follows the same naming convention. Signed-off-by: Derek J. Clark Reviewed-by: Mario Limonciello --- Documentation/wmi/devices/lenovo-wmi.rst | 104 +++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Documentation/wmi/devices/lenovo-wmi.rst diff --git a/Documentation/wmi/devices/lenovo-wmi.rst b/Documentation/wmi/devices/lenovo-wmi.rst new file mode 100644 index 000000000000..62c2ec9505bd --- /dev/null +++ b/Documentation/wmi/devices/lenovo-wmi.rst @@ -0,0 +1,104 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later +====================================================== +Lenovo WMI Interface Drivers (lenovo-wmi) +====================================================== + +Introduction +============ +Lenovo WMI interfaces are broken up into multiple GUIDs, some of which +require cross-references between GUIDs for some functionality. The "Custom +Mode" interface is a legacy interface for managing and displaying CPU & GPU +power and hwmon settings and readings. The "Other Method" interface is a +modern interface that replaces most "Custom Mode" interface methods. The +"GameZone" interface adds advanced features such as fan profiles and +overclocking. The "Lighting" interface adds control of various status +lights related to different hardware components. + +Each interface has a different data structure associated with it that +provides detailed information about each attribute provided by the +interface. These data structs are retrieved from additional WMI device +data block GUIDs: + - "Custom Mode" uses LENOVO_FAN_TABLE_DATA, LENOVO_FAN_TEST_DATA, + LENOVO_CPU_OVERCLOCKING_DATA, LENOVO_DISCRETE_DATA, and + LENOVO_GPU_OVERCLOCKING_DATA depending on the feature. + - "Other Method" uses LENOVO_CAPABILITY_DATA_00, + LENOVO_CAPABILITY_DATA_01, and LENOVO_CAPABILITY_DATA_02 depending on + the feature. + - "GameZone" uses LENOVO_GAMEZONE_CPU_OC_DATA and + LENOVO_GAMEZONE_GPU_OC_DATA depending on the feature. + - The "Lighting" interface uses LENOVO_LIGHTING_DATA. + +.. note:: + Currently only the "GameZone", "Other Method", and + LENOVO_CAPABILITY_DATA_01 interfaces are implemented by these drivers. + +GameZone +-------- +WMI GUID "887B54E3-DDDC-4B2C-8B88-68A26A8835D0" + +The GameZone WMI interface provides platform-profile and fan curve settings +for devices that fall under the "Gaming Series" of Lenovo devices. + +The following platform profiles are supported: + - quiet + - balanced + - performance + - custom + +Custom Profile +~~~~~~~~~~~~~~ +The custom profile represents a hardware mode on Lenovo devices that enables +user modifications to Package Power Tracking (PPT) settings. When an +attribute exposed by the "Other Method" WMI interface is to be modified, +the GameZone driver must first be switched to the "custom" profile manually +or the setting will have no effect. If another profile is set from the list +of supported profiles, the BIOS will override any user PPT settings when +switching to that profile. + + +Other Method +---------- +WMI GUID "DC2A8805-3A8C-41BA-A6F7-092E0089CD3B" + +The Other Method WMI interface uses the fw_attributes class to expose +various WMI attributes provided by the interface in the sysfs. This enables +CPU and GPU power limit tuning as well as various other attributes for +devices that fall under the "Gaming Series" of Lenovo devices. Each +attribute exposed by the Other Method interface has corresponding +capability data blocks which allow the driver to probe details about the +attribute. Each attibute has multiple pages, one for each of the platform +profiles managed by the "GameZone" interface. For all properties only the +"Custom" profile values are reported by this driver to ensure any userspace +applications reading them have accurate tunable value ranges. Attributes +are exposed in sysfs under the following path: +/sys/class/firmware-attributes/lenovo-wmi-other/attributes + +LENOVO_CAPABILITY_DATA_01 +~~~~~~~~~~~~~~~~~~~~~~~~~ +WMI GUID "7A8F5407-CB67-4D6E-B547-39B3BE018154" + +The LENOVO_CAPABILITY_DATA_01 interface provides information on various +power limits of integrated CPU and GPU components. + +The following attributes are supported: + - ppt_pl1_spl: Platform Profile Tracking Sustained Power Limit + - ppt_pl2_sppt: Platform Profile Tracking Slow Package Power Tracking + - ppt_pl3_fppt: Platform Profile Tracking Fast Package Power Tracking + +Each attribute has the following properties: + - current_value + - default_value + - display_name + - max_value + - min_value + - scalar_increment + - type + + + Camera + ______ + WMI GUID "50C76F1F-D8E4-D895-0A3D-62F4EA400013" + + The Camera driver provides WMI event notifications for camera button + toggling. + From patchwork Thu Jan 2 00:47:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derek John Clark X-Patchwork-Id: 13924287 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCEA14689; Thu, 2 Jan 2025 00:49:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778951; cv=none; b=J+b2W9inCMFynx3H88aZQB9W6Rpx2NAF01o+4m+HtEldRYmMs1KFRHJRy4nA4/DCdBnt9yLTmnpgAjRd8K4wTZ5H0xWsFL5pTUeXfu40hkHd29dQUShYCOf4jrjbuC06pcak6wCcMFibLcutsmvzzvdN0yqqRatrTyFet0blRC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778951; c=relaxed/simple; bh=WiP2RuQ+3vBwByJV33udqyrQPAWZBLVZKBQQiTrIGA8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bnR3QQyKoNKOiBeB5GRhvzxedlLXFiLdnApjIBf+lYkUuH5V2QAMYUrDEgonL6JjzPzm9zSAirqUZmtU1JZz9kfaKVw3dyso4wvksvCMvsLpoH/4/Nd+7LvND6+MRDh27H/JFULbc/yQ2BoC3hM71b16Trm3mtOOf0jb0s7vdv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=McWaIsw6; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="McWaIsw6" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2ee76befe58so15045192a91.2; Wed, 01 Jan 2025 16:49:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735778948; x=1736383748; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qgepQQY65Rjix+QY9dKxawxq+/tV33uZISaYDEwRLOo=; b=McWaIsw6MjL0E96pXv1uOL5K5UbyyfxU03GGpAjZ4V6PDAzRtSO/A/iZ6A05MXCK1l qQ4Leys0FgSQqqVy2RiGkPHNbTMkza03b2PLcHGfEurRbAmveoDrBkwRtcysDyIwmfSQ 31mQ7SlsSR+b1MhgzZjhL4WfYp8wG6SNQnvqPpmlvrTxyHpbg9Kbi4q6gK+X2xlzTiuP pUZ6TxCAdlNWMhHHKAffBMFCm2VqkYKpyk1w0tfEO5eRufFOn3BJ2NE7K2UkucPXiHMY AJEbqGLW6RU3eWi3bo1DlCRzsWR7PBIsgJfW+zpFl9QCOLncOTmmjGdQvBCfLeLJxaQk XcLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735778948; x=1736383748; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qgepQQY65Rjix+QY9dKxawxq+/tV33uZISaYDEwRLOo=; b=vqiEiVgbzJKZOml2+io9wCPFdJkHESVQ1uoQGddLVFVcOqGs9TyIyhNqEThYabN+bv R8Sd+uKsO94q9REH/prgTSCQwEmIoQG5TaKtOU32MnvxRyxK2jDOLoKT2dH608LbUVRt iN0TwFvX2oAqZcoBW2WwFPiAVsK+YLzmQ+RFFwlpSSv6MDbQKV7cUL7LomoryL7XUsHW c0xFpDcgWV8PzpjM2vXtkCMmOXUgUMZlNQcKKDgFGAdMcBZzRTjMuUt8agRcIOzhPJal wwcNEQOHP4Fg8GzppDrHL+NL9GJKa00eNMg4jRfhZ8oVJ+7h0szx0dkhD079laSkB9Nu grGQ== X-Forwarded-Encrypted: i=1; AJvYcCUXTRItPsOlOo3xzrZMxADp/wAwEkA4MvWh01kNzMbv8jL/fhs85dEb9je1AWGjWrRPEgG2kTMNXXY=@vger.kernel.org, AJvYcCV8IpxBX94rxgvm/oLvFzxDp8o2Wej8xHXYQp0HBvi25nI1D2lT4WcaEs3qDFcuF00j8CE4P/hqoH6NxhvQ@vger.kernel.org, AJvYcCWGRZuLU2oxv6bN1CFJIvG0hvAMVuunxuOZKoaXS1vMucrJhMRfr9XK03HZ6e+/LYXy9TBtiCiYVnnbwlyGRjc7WgjISQ==@vger.kernel.org X-Gm-Message-State: AOJu0Yx1pa6RNsvR0jXUpz/uHuAlCbGfvz/sm/XnEYvHXLrf1OvFiGAR abmvtBM238+XgcvM7tMFiIyLjLKwHZaO9q9FUIDqaxyryuHB9T5j X-Gm-Gg: ASbGncvpacld9e9R9lhvUHdo32pdKBf+STDg918d/xzbgi2oUUxK6R9Itz6pE0UIZM8 pzEZ+XIIaAF/g1XK6GjJRA9bjmMvT5hV4ejIF+3nyIXQAxIs5ez7c4c/aDi49tcaLUWkjhf3Kfc kqNzYyk+bBSuy+HC6jZvBfKQL3o239m8YeEvqFPD8doF+2n26AutN8RdwPzE1ckEHQJZQtUgNf+ XJAICXVe8fQmHkqIoE3ftzITIh5Aw/WTuRgZIOckS909JgjkzxpGcCT8Ib2LY4LLBa0f2hQRjUk 9B+YatWjsRoyU6UNomv/wKyqrBzY1yHMiTXP2Zj1WbPxwqwpCBM9zQM= X-Google-Smtp-Source: AGHT+IE5QUU5wnvCJxSubbbcoIbt6Ws+kq0omwrV6le+yagtye4NZi2IfM9bKrQTQSM77KhQqWBvxg== X-Received: by 2002:a05:6a21:3997:b0:1e0:c9a9:a950 with SMTP id adf61e73a8af0-1e5e081cb96mr70159809637.39.1735778948029; Wed, 01 Jan 2025 16:49:08 -0800 (PST) Received: from localhost.localdomain (108-228-232-20.lightspeed.sndgca.sbcglobal.net. [108.228.232.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad8351fasm23793594b3a.62.2025.01.01.16.49.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 16:49:07 -0800 (PST) From: "Derek J. Clark" To: Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= Cc: Jonathan Corbet , Mario Limonciello , Luke Jones , Xino Ni , Zhixin Zhang , Mia Shao , Mark Pearson , "Pierre-Loup A . Griffais" , "Cody T . -H . Chiu" , John Martens , "Derek J . Clark" , platform-driver-x86@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] platform/x86: Add Lenovo GameZone WMI Driver Date: Wed, 1 Jan 2025 16:47:20 -0800 Message-ID: <20250102004854.14874-3-derekjohn.clark@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250102004854.14874-1-derekjohn.clark@gmail.com> References: <20250102004854.14874-1-derekjohn.clark@gmail.com> Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adds lenovo-wmi-gamezone.c which provides a driver for the Lenovo GameZone WMI interface that comes on Lenovo "Gaming Series" hardware. Provides ACPI platform profiles over WMI. v2: - Use devm_kzalloc to ensure driver can be instanced, remove global reference. - Ensure reverse Christmas tree for all variable declarations. - Remove extra whitespace. - Use guard(mutex) in all mutex instances, global mutex. - Use pr_fmt instead of adding the driver name to each pr_err. - Remove noisy pr_info usage. - Rename gamezone_wmi to lenovo_wmi_gz_priv and gz_wmi to priv. - Remove GZ_WMI symbol exporting. Signed-off-by: Derek J. Clark --- MAINTAINERS | 7 + drivers/platform/x86/Kconfig | 11 ++ drivers/platform/x86/Makefile | 1 + drivers/platform/x86/lenovo-wmi-gamezone.c | 203 +++++++++++++++++++++ drivers/platform/x86/lenovo-wmi.h | 105 +++++++++++ 5 files changed, 327 insertions(+) create mode 100644 drivers/platform/x86/lenovo-wmi-gamezone.c create mode 100644 drivers/platform/x86/lenovo-wmi.h diff --git a/MAINTAINERS b/MAINTAINERS index baf0eeb9a355..8f8a6aec6b92 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13034,6 +13034,13 @@ S: Maintained W: http://legousb.sourceforge.net/ F: drivers/usb/misc/legousbtower.c +LENOVO WMI drivers +M: Derek J. Clark +L: platform-driver-x86@vger.kernel.org +S: Maintained +F: drivers/platform/x86/lenovo-wmi-gamezone.c +F: drivers/platform/x86/lenovo-wmi.h + LETSKETCH HID TABLET DRIVER M: Hans de Goede L: linux-input@vger.kernel.org diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index 0258dd879d64..9a6ac7fdec9f 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig @@ -459,6 +459,17 @@ config IBM_RTL state = 0 (BIOS SMIs on) state = 1 (BIOS SMIs off) +config LENOVO_WMI_GAMEZONE + tristate "Lenovo GameZone WMI Driver" + depends on ACPI_WMI + select ACPI_PLATFORM_PROFILE + help + Say Y here if you have a WMI aware Lenovo Legion device and would like to use the + platform-profile firmware interface. + + To compile this driver as a module, choose M here: the module will + be called lenovo_wmi_gamezone. + config IDEAPAD_LAPTOP tristate "Lenovo IdeaPad Laptop Extras" depends on ACPI diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile index e1b142947067..7cb29a480ed2 100644 --- a/drivers/platform/x86/Makefile +++ b/drivers/platform/x86/Makefile @@ -68,6 +68,7 @@ obj-$(CONFIG_THINKPAD_LMI) += think-lmi.o obj-$(CONFIG_YOGABOOK) += lenovo-yogabook.o obj-$(CONFIG_YT2_1380) += lenovo-yoga-tab2-pro-1380-fastcharger.o obj-$(CONFIG_LENOVO_WMI_CAMERA) += lenovo-wmi-camera.o +obj-$(CONFIG_LENOVO_WMI_GAMEZONE) += lenovo-wmi-gamezone.o # Intel obj-y += intel/ diff --git a/drivers/platform/x86/lenovo-wmi-gamezone.c b/drivers/platform/x86/lenovo-wmi-gamezone.c new file mode 100644 index 000000000000..da5e2bc41f39 --- /dev/null +++ b/drivers/platform/x86/lenovo-wmi-gamezone.c @@ -0,0 +1,203 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Lenovo GameZone WMI interface driver. The GameZone WMI interface provides + * platform profile and fan curve settings for devices that fall under the + * "Gaming Series" of Lenovo Legion devices. + * + * Copyright(C) 2024 Derek J. Clark + */ + +#include +#include "lenovo-wmi.h" + +#define LENOVO_GAMEZONE_GUID "887B54E3-DDDC-4B2C-8B88-68A26A8835D0" + +/* Method IDs */ +#define WMI_METHOD_ID_SMARTFAN_SUPP 43 /* IsSupportSmartFan */ +#define WMI_METHOD_ID_SMARTFAN_SET 44 /* SetSmartFanMode */ +#define WMI_METHOD_ID_SMARTFAN_GET 45 /* GetSmartFanMode */ + +static DEFINE_MUTEX(call_mutex); + +static const struct wmi_device_id lenovo_wmi_gamezone_id_table[] = { + { LENOVO_GAMEZONE_GUID, NULL }, /* LENOVO_GAMEZONE_DATA */ + {} +}; + +struct lenovo_wmi_gz_priv { + struct wmi_device *wdev; + enum platform_profile_option current_profile; + struct platform_profile_handler pprof; + bool platform_profile_support; +}; + +/* Platform Profile Methods */ +static int lenovo_wmi_gamezone_platform_profile_supported( + struct platform_profile_handler *pprof, int *supported) +{ + struct lenovo_wmi_gz_priv *priv; + + priv = container_of(pprof, struct lenovo_wmi_gz_priv, pprof); + + guard(mutex)(&call_mutex); + return lenovo_wmidev_evaluate_method_1( + priv->wdev, 0x0, WMI_METHOD_ID_SMARTFAN_SUPP, 0, supported); +} + +static int +lenovo_wmi_gamezone_profile_get(struct platform_profile_handler *pprof, + enum platform_profile_option *profile) +{ + struct lenovo_wmi_gz_priv *priv; + int sel_prof; + int err; + + priv = container_of(pprof, struct lenovo_wmi_gz_priv, pprof); + + guard(mutex)(&call_mutex); + err = lenovo_wmidev_evaluate_method_1( + priv->wdev, 0x0, WMI_METHOD_ID_SMARTFAN_GET, 0, &sel_prof); + if (err) { + pr_err("Error getting fan profile from WMI interface: %d\n", + err); + return err; + } + + switch (sel_prof) { + case SMARTFAN_MODE_QUIET: + *profile = PLATFORM_PROFILE_QUIET; + break; + case SMARTFAN_MODE_BALANCED: + *profile = PLATFORM_PROFILE_BALANCED; + break; + case SMARTFAN_MODE_PERFORMANCE: + *profile = PLATFORM_PROFILE_PERFORMANCE; + break; + case SMARTFAN_MODE_CUSTOM: + *profile = PLATFORM_PROFILE_CUSTOM; + break; + default: + return -EINVAL; + } + priv->current_profile = *profile; + + return 0; +} + +static int +lenovo_wmi_gamezone_profile_set(struct platform_profile_handler *pprof, + enum platform_profile_option profile) +{ + struct lenovo_wmi_gz_priv *priv; + int sel_prof; + int err; + + switch (profile) { + case PLATFORM_PROFILE_QUIET: + sel_prof = SMARTFAN_MODE_QUIET; + break; + case PLATFORM_PROFILE_BALANCED: + sel_prof = SMARTFAN_MODE_BALANCED; + break; + case PLATFORM_PROFILE_PERFORMANCE: + sel_prof = SMARTFAN_MODE_PERFORMANCE; + break; + case PLATFORM_PROFILE_CUSTOM: + sel_prof = SMARTFAN_MODE_CUSTOM; + break; + default: + return -EOPNOTSUPP; + } + + priv = container_of(pprof, struct lenovo_wmi_gz_priv, pprof); + + guard(mutex)(&call_mutex); + err = lenovo_wmidev_evaluate_method_1( + priv->wdev, 0x0, WMI_METHOD_ID_SMARTFAN_SET, sel_prof, NULL); + if (err) { + pr_err("Error setting fan profile on WMI interface: %u\n", err); + return err; + } + + priv->current_profile = profile; + return 0; +} + +/* Driver Setup */ +static int platform_profile_setup(struct lenovo_wmi_gz_priv *priv) +{ + int supported; + int err; + + err = lenovo_wmi_gamezone_platform_profile_supported(&priv->pprof, + &supported); + if (err) { + pr_err("Error checking platform profile support: %d\n", err); + return err; + } + + priv->platform_profile_support = supported; + + if (!supported) + return -EOPNOTSUPP; + + priv->pprof.name = "lenovo-wmi-gamezone"; + priv->pprof.profile_get = lenovo_wmi_gamezone_profile_get; + priv->pprof.profile_set = lenovo_wmi_gamezone_profile_set; + + set_bit(PLATFORM_PROFILE_QUIET, priv->pprof.choices); + set_bit(PLATFORM_PROFILE_BALANCED, priv->pprof.choices); + set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->pprof.choices); + set_bit(PLATFORM_PROFILE_CUSTOM, priv->pprof.choices); + + err = lenovo_wmi_gamezone_profile_get(&priv->pprof, + &priv->current_profile); + if (err) { + pr_err("Error getting current platform profile: %d\n", err); + return err; + } + + guard(mutex)(&call_mutex); + err = platform_profile_register(&priv->pprof); + if (err) { + pr_err("Error registering platform profile: %d\n", err); + return err; + } + + return 0; +} + +static int lenovo_wmi_gamezone_probe(struct wmi_device *wdev, + const void *context) +{ + struct lenovo_wmi_gz_priv *priv; + + priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->wdev = wdev; + return platform_profile_setup(priv); +} + +static void lenovo_wmi_gamezone_remove(struct wmi_device *wdev) +{ + struct lenovo_wmi_gz_priv *priv = dev_get_drvdata(&wdev->dev); + + guard(mutex)(&call_mutex); + platform_profile_remove(&priv->pprof); +} + +static struct wmi_driver lenovo_wmi_gamezone_driver = { + .driver = { .name = "lenovo_wmi_gamezone" }, + .id_table = lenovo_wmi_gamezone_id_table, + .probe = lenovo_wmi_gamezone_probe, + .remove = lenovo_wmi_gamezone_remove, +}; + +module_wmi_driver(lenovo_wmi_gamezone_driver); + +MODULE_DEVICE_TABLE(wmi, lenovo_wmi_gamezone_id_table); +MODULE_AUTHOR("Derek J. Clark "); +MODULE_DESCRIPTION("Lenovo GameZone WMI Driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/platform/x86/lenovo-wmi.h b/drivers/platform/x86/lenovo-wmi.h new file mode 100644 index 000000000000..8a302c6c47cb --- /dev/null +++ b/drivers/platform/x86/lenovo-wmi.h @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * + * Lenovo Legion WMI interface driver. The Lenovo Legion WMI interface is + * broken up into multiple GUID interfaces that require cross-references + * between GUID's for some functionality. The "Custom Mode" interface is a + * legacy interface for managing and displaying CPU & GPU power and hwmon + * settings and readings. The "Other Mode" interface is a modern interface + * that replaces or extends the "Custom Mode" interface methods. The + * "GameZone" interface adds advanced features such as fan profiles and + * overclocking. The "Lighting" interface adds control of various status + * lights related to different hardware components. "Other Method" uses + * the data structs LENOVO_CAPABILITY_DATA_00, LENOVO_CAPABILITY_DATA_01 + * and LENOVO_CAPABILITY_DATA_02 structs for capability information. + * + * Copyright(C) 2024 Derek J. Clark + * + */ + +#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__ + +#ifndef _LENOVO_WMI_H_ +#define _LENOVO_WMI_H_ + +#include +#include +#include + +/* Platform Profile Modes */ +#define SMARTFAN_MODE_QUIET 0x01 +#define SMARTFAN_MODE_BALANCED 0x02 +#define SMARTFAN_MODE_PERFORMANCE 0x03 +#define SMARTFAN_MODE_CUSTOM 0xFF + +struct wmi_method_args { + u32 arg0; + u32 arg1; +}; + +/* General Use functions */ +static int lenovo_wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, + u32 method_id, struct acpi_buffer *in, + struct acpi_buffer *out) +{ + acpi_status status; + + status = wmidev_evaluate_method(wdev, instance, method_id, in, out); + + if (ACPI_FAILURE(status)) + return -EIO; + + return 0; +}; + +int lenovo_wmidev_evaluate_method_2(struct wmi_device *wdev, u8 instance, + u32 method_id, u32 arg0, u32 arg1, + u32 *retval); + +int lenovo_wmidev_evaluate_method_2(struct wmi_device *wdev, u8 instance, + u32 method_id, u32 arg0, u32 arg1, + u32 *retval) +{ + struct wmi_method_args args = { arg0, arg1 }; + struct acpi_buffer input = { (acpi_size)sizeof(args), &args }; + struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; + union acpi_object *ret_obj = NULL; + int err; + + err = lenovo_wmidev_evaluate_method(wdev, instance, method_id, &input, + &output); + + if (err) { + pr_err("Attempt to get method value failed.\n"); + return err; + } + + if (retval) { + ret_obj = (union acpi_object *)output.pointer; + if (!ret_obj) { + pr_err("Failed to get valid ACPI object from WMI interface\n"); + return -EIO; + } + if (ret_obj->type != ACPI_TYPE_INTEGER) { + pr_err("WMI query returnd ACPI object with wrong type.\n"); + kfree(ret_obj); + return -EIO; + } + *retval = (u32)ret_obj->integer.value; + } + + kfree(ret_obj); + + return 0; +} + +int lenovo_wmidev_evaluate_method_1(struct wmi_device *wdev, u8 instance, + u32 method_id, u32 arg0, u32 *retval); + +int lenovo_wmidev_evaluate_method_1(struct wmi_device *wdev, u8 instance, + u32 method_id, u32 arg0, u32 *retval) +{ + return lenovo_wmidev_evaluate_method_2(wdev, instance, method_id, arg0, + 0, retval); +} + +#endif /* !_LENOVO_WMI_H_ */ From patchwork Thu Jan 2 00:47:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derek John Clark X-Patchwork-Id: 13924288 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8C75946C; Thu, 2 Jan 2025 00:49:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778952; cv=none; b=lZVa93RPkMCHh2hEXxkaAi8C1OKFqBmff7HXPk84j7erqLJqTI+GFN/U0V1GhO1Tyz2A6ZDliYS8o28t1nBaWzsq1EpHTydnuSSv9f1OqtiHXEG61ja8PYyvOoX2J9N9aF0UhcIU0QhRo6sFyx045enoaN6KYUcEBOTLNlfCyr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778952; c=relaxed/simple; bh=3xK4i6Y72IF7e2YfCs2OikbTfSJkNhd4dXRMwqyzO34=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i00pezplkRSTrIjbKM/eehBbUsIm9fmRVBPeezZY4YxUFQvjLm7UXfuB8Dxqft8bH2uO2WR8WPdyL6x0uYDRHJF6lDJVdiuIWsLVlOhFuPVUIVHsP/08BT5qgvMti2Tq7eaIwAvA5/v7PYIN1v+0EKFHxE8Oki7tmq5syns0/cI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TftqaFoI; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TftqaFoI" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21644aca3a0so62229075ad.3; Wed, 01 Jan 2025 16:49:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735778950; x=1736383750; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mutDscxqMcbCo3fe7ti2EhyV+2OLpjf/46O5Nen6FGg=; b=TftqaFoILKOeOSIJpsRpvXWwz5TS7dx4FiG3DTxIN8xGZA92Z9ycytFA8Lyw0BAarF y7rUeHWHiWFUpuU2MaDQUZE5+3C57G8oUmETug8YDIxV6IvhDuCEEmGVs8+gd0hHp3Bl qaW1GKJBVa6dJTpnDZaxK8tS+DBsnIZNanIxF/CUC/0PijV9Z9VlBjX7K+R8IrQ1rbDj dX2BQBWEPmM+BItxF5DpfcqXQqCVu35YcszeEVysPUf2nzIYelnFxRs//A459ngQ2c69 1XnWFn07Md+2R51FGB9kBIr/idQjYNBy6vd6vDHjA1pnWPyl59hdYFoEl9tA4QDTOmfx MuUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735778950; x=1736383750; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mutDscxqMcbCo3fe7ti2EhyV+2OLpjf/46O5Nen6FGg=; b=bFqbJiOZLOrQBShPDRQq2+iOw4lJn7+ggIN5CXWi2Rx9g9Knh//I9K4wYD0HoXIEUb CZgy+rGHhLSy1WbWPeztB199wU9kk2ssJrbi7qkczdzlv7h6TKTCFx+G2tKA977rim7v /BleSSXFl/htqov0ATx6z/qXhpFcvrC/oIgwojl5j3Z3Q0XYOeTj4igU86Op85f6bDum FKF9dG4eI4JD2LlfJtSnEOGevOqYw2v1x0HefjCEluEswXVBDpQ8Giw7Vqkzhz9wTd+Z bQX1zqCgDi1KHOocLKN5zRrxaHAuTUSIGPC7zVq9KNTmilEBcgFsICStW8uqla//gbcd nd3Q== X-Forwarded-Encrypted: i=1; AJvYcCVZEwhmmBcWFyK6jjBepa5O3mLiiBZqXhcIStPtDCXVe7ZLy/chRgfLkaczwG7rFXWs5otxMd9y7RLgqQ4DO1PNmiJRYQ==@vger.kernel.org, AJvYcCWlr7RpqMtjUrOe/jL5zXi/uCy1udvFfzsiL7uzbMosxP6nvrxR2Cf660jo2XGoRhEpQqh4r6gpJtd7M64+@vger.kernel.org, AJvYcCXDodVxiSCR0Qnr/JUvWNPXWo7YlM33gTdmQ4hi+e4oXHuSq7PQEFEkOhIp3iABzBR6nIT47lBP7k4=@vger.kernel.org X-Gm-Message-State: AOJu0YzUCsWrPZ4tah+EI3lKNPZjm4uj6kmoldXJD9ySEtdDSM2+HxYG o6Kg3eJhZtJ4NnWtULdD4y429fIwiSixiv/Mo7NmpOTbXOk0K8ov X-Gm-Gg: ASbGncuO0uW8gPyLk3Ic2QoCedkpPdBvmPLhnFAIzwKrzMiGOc3H1SBF64sPSuOxUv9 hhEVeCJDrFI6l81HAauQ2NGVBC4VTUpVNEYwwX7I6rlIOs+qFzKivs+wj4MfuNR9bCwY4cb4TD7 XlQg0b67GohyTPQozgzKg0RVtN2gzEKrPkz0QGaNzVbCuq09PfPInnbgNMRPfzDYr7c8+IBNs4Z PiYlabcKwkcmGdJPOFDYTKV39l6dCALRJjvb0LK3GagFeRoKdPGyM7U99BeAeBjfqAN1oOh8Wqo /Cd+HzY+hmpIu8aA6DaaGUlkBTuLuS6UFAEQs/b7QJBg+c0P1xAVsDQ= X-Google-Smtp-Source: AGHT+IHqStCvWNNK4mmH2QyCUI5xZd52gn5UcpaPIeaLd6J8VDUPdBuyROIsIO/y8GYZhyCrqhPx7w== X-Received: by 2002:a05:6a21:7e07:b0:1e6:5323:58c5 with SMTP id adf61e73a8af0-1e65323591dmr18209789637.12.1735778949841; Wed, 01 Jan 2025 16:49:09 -0800 (PST) Received: from localhost.localdomain (108-228-232-20.lightspeed.sndgca.sbcglobal.net. [108.228.232.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad8351fasm23793594b3a.62.2025.01.01.16.49.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 16:49:09 -0800 (PST) From: "Derek J. Clark" To: Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= Cc: Jonathan Corbet , Mario Limonciello , Luke Jones , Xino Ni , Zhixin Zhang , Mia Shao , Mark Pearson , "Pierre-Loup A . Griffais" , "Cody T . -H . Chiu" , John Martens , "Derek J . Clark" , platform-driver-x86@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/4] platform/x86: Add Lenovo Capability Data 01 WMI Driver Date: Wed, 1 Jan 2025 16:47:21 -0800 Message-ID: <20250102004854.14874-4-derekjohn.clark@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250102004854.14874-1-derekjohn.clark@gmail.com> References: <20250102004854.14874-1-derekjohn.clark@gmail.com> Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adds lenovo-wmi-capdata01.c which provides a driver for the LENOVO_CAPABILITY_DATA_01 WMI data block that comes on "Other Method" enabled hardware. Provides an interface for querying if a given attribute is supported by the hardware, as well as its default_value, max_value, min_value, and step increment. v2: - Use devm_kzalloc to ensure driver can be instanced, remove global reference. - Ensure reverse Christmas tree for all variable declarations. - Remove extra whitespace. - Use guard(mutex) in all mutex instances, global mutex. - Use pr_fmt instead of adding the driver name to each pr_err. - Remove noisy pr_info usage. - Rename capdata_wmi to lenovo_wmi_cd01_priv and cd01_wmi to priv. - Use list to get the lenovo_wmi_cd01_priv instance in lenovo_wmi_capdata01_get as none of the data provided by the macros that will use it can pass a member of the struct for use in container_of. Signed-off-by: Derek J. Clark --- MAINTAINERS | 1 + drivers/platform/x86/Kconfig | 11 ++ drivers/platform/x86/Makefile | 1 + drivers/platform/x86/lenovo-wmi-capdata01.c | 131 ++++++++++++++++++++ drivers/platform/x86/lenovo-wmi.h | 20 +++ 5 files changed, 164 insertions(+) create mode 100644 drivers/platform/x86/lenovo-wmi-capdata01.c diff --git a/MAINTAINERS b/MAINTAINERS index 8f8a6aec6b92..c9374c395905 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13038,6 +13038,7 @@ LENOVO WMI drivers M: Derek J. Clark L: platform-driver-x86@vger.kernel.org S: Maintained +F: drivers/platform/x86/lenovo-wmi-capdata01.c F: drivers/platform/x86/lenovo-wmi-gamezone.c F: drivers/platform/x86/lenovo-wmi.h diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index 9a6ac7fdec9f..a2c1ab47ad9e 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig @@ -470,6 +470,17 @@ config LENOVO_WMI_GAMEZONE To compile this driver as a module, choose M here: the module will be called lenovo_wmi_gamezone. +config LENOVO_WMI_DATA01 + tristate "Lenovo Legion WMI capability Data 01 Driver" + depends on ACPI_WMI + help + Say Y here if you have a WMI aware Lenovo Legion device in the "Gaming Series" + line of hardware. This interface is a dependency for exposing tunable power + settings. + + To compile this driver as a module, choose M here: the module will + be called lenovo_wmi_capdata01. + config IDEAPAD_LAPTOP tristate "Lenovo IdeaPad Laptop Extras" depends on ACPI diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile index 7cb29a480ed2..6c96cc3f3855 100644 --- a/drivers/platform/x86/Makefile +++ b/drivers/platform/x86/Makefile @@ -69,6 +69,7 @@ obj-$(CONFIG_YOGABOOK) += lenovo-yogabook.o obj-$(CONFIG_YT2_1380) += lenovo-yoga-tab2-pro-1380-fastcharger.o obj-$(CONFIG_LENOVO_WMI_CAMERA) += lenovo-wmi-camera.o obj-$(CONFIG_LENOVO_WMI_GAMEZONE) += lenovo-wmi-gamezone.o +obj-$(CONFIG_LENOVO_WMI_DATA01) += lenovo-wmi-capdata01.o # Intel obj-y += intel/ diff --git a/drivers/platform/x86/lenovo-wmi-capdata01.c b/drivers/platform/x86/lenovo-wmi-capdata01.c new file mode 100644 index 000000000000..b10a6e4b320f --- /dev/null +++ b/drivers/platform/x86/lenovo-wmi-capdata01.c @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * LENOVO_CAPABILITY_DATA_01 WMI data block driver. This interface provides + * information on tunable attributes used by the "Other Method" WMI interface, + * including if it is supported by the hardware, the default_value, max_value, + * min_value, and step increment. + * + * Copyright(C) 2024 Derek J. Clark + */ + +#include +#include "lenovo-wmi.h" + +#define LENOVO_CAPABILITY_DATA_01_GUID "7A8F5407-CB67-4D6E-B547-39B3BE018154" + +static DEFINE_MUTEX(cd01_call_mutex); +static DEFINE_MUTEX(cd01_list_mutex); +static LIST_HEAD(cd01_wmi_list); + +static const struct wmi_device_id lenovo_wmi_capdata01_id_table[] = { + { LENOVO_CAPABILITY_DATA_01_GUID, NULL }, + {} +}; + +struct lenovo_wmi_cd01_priv { + struct wmi_device *wdev; + struct list_head list; +}; + +static inline struct lenovo_wmi_cd01_priv *get_first_wmi_priv(void) +{ + guard(mutex)(&cd01_list_mutex); + return list_first_entry_or_null(&cd01_wmi_list, + struct lenovo_wmi_cd01_priv, list); +} + +int lenovo_wmi_capdata01_get(struct lenovo_wmi_attr_id attr_id, + struct capability_data_01 *cap_data) +{ + u32 attribute_id = *(int *)&attr_id; + struct lenovo_wmi_cd01_priv *priv; + union acpi_object *ret_obj; + int instance_idx; + int count; + + priv = get_first_wmi_priv(); + if (!priv) + return -ENODEV; + + guard(mutex)(&cd01_call_mutex); + count = wmidev_instance_count(priv->wdev); + pr_info("Got instance count: %u\n", count); + + for (instance_idx = 0; instance_idx < count; instance_idx++) { + ret_obj = wmidev_block_query(priv->wdev, instance_idx); + if (!ret_obj) { + pr_err("WMI Data block query failed.\n"); + continue; + } + + if (ret_obj->type != ACPI_TYPE_BUFFER) { + pr_err("WMI Data block query returned wrong type.\n"); + kfree(ret_obj); + continue; + } + + if (ret_obj->buffer.length != sizeof(*cap_data)) { + pr_err("WMI Data block query returned wrong buffer length: %u vice expected %lu.\n", + ret_obj->buffer.length, sizeof(*cap_data)); + kfree(ret_obj); + continue; + } + + memcpy(cap_data, ret_obj->buffer.pointer, + ret_obj->buffer.length); + kfree(ret_obj); + + if (cap_data->id != attribute_id) + continue; + break; + } + + if (cap_data->id != attribute_id) { + pr_err("Unable to find capability data for attribute_id %x\n", + attribute_id); + return -EINVAL; + } + + return 0; +} +EXPORT_SYMBOL_NS_GPL(lenovo_wmi_capdata01_get, "CAPDATA_WMI"); + +static int lenovo_wmi_capdata01_probe(struct wmi_device *wdev, + const void *context) + +{ + struct lenovo_wmi_cd01_priv *priv; + + priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->wdev = wdev; + + guard(mutex)(&cd01_list_mutex); + list_add_tail(&priv->list, &cd01_wmi_list); + + return 0; +} + +static void lenovo_wmi_capdata01_remove(struct wmi_device *wdev) +{ + struct lenovo_wmi_cd01_priv *priv = dev_get_drvdata(&wdev->dev); + + guard(mutex)(&cd01_list_mutex); + list_del(&priv->list); +} + +static struct wmi_driver lenovo_wmi_capdata01_driver = { + .driver = { .name = "lenovo_wmi_capdata01" }, + .id_table = lenovo_wmi_capdata01_id_table, + .probe = lenovo_wmi_capdata01_probe, + .remove = lenovo_wmi_capdata01_remove, +}; + +module_wmi_driver(lenovo_wmi_capdata01_driver); + +MODULE_DEVICE_TABLE(wmi, lenovo_wmi_capdata01_id_table); +MODULE_AUTHOR("Derek J. Clark "); +MODULE_DESCRIPTION("Lenovo Capability Data 01 WMI Driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/platform/x86/lenovo-wmi.h b/drivers/platform/x86/lenovo-wmi.h index 8a302c6c47cb..53cea84a956b 100644 --- a/drivers/platform/x86/lenovo-wmi.h +++ b/drivers/platform/x86/lenovo-wmi.h @@ -36,6 +36,22 @@ struct wmi_method_args { u32 arg1; }; +struct lenovo_wmi_attr_id { + u32 mode_id : 16; /* Fan profile */ + u32 feature_id : 8; /* Attribute (SPL/SPPT/...) */ + u32 device_id : 8; /* CPU/GPU/... */ +} __packed; + +/* Data struct for LENOVO_CAPABILITY_DATA_01 */ +struct capability_data_01 { + u32 id; + u32 supported; + u32 default_value; + u32 step; + u32 min_value; + u32 max_value; +}; + /* General Use functions */ static int lenovo_wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id, struct acpi_buffer *in, @@ -102,4 +118,8 @@ int lenovo_wmidev_evaluate_method_1(struct wmi_device *wdev, u8 instance, 0, retval); } +/* LENOVO_CAPABILITY_DATA_01 exported functions */ +int lenovo_wmi_capdata01_get(struct lenovo_wmi_attr_id attr_id, + struct capability_data_01 *cap_data); + #endif /* !_LENOVO_WMI_H_ */ From patchwork Thu Jan 2 00:47:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derek John Clark X-Patchwork-Id: 13924289 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4846ADF51; Thu, 2 Jan 2025 00:49:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778954; cv=none; b=Pme42PDOUWE1VguOADL/XpQZJ29FEmdwvbTUrScJcDD2rQVcNBgzxoAhrW7U/5bJrH7HaKu0b8i6ldbK0m6HXkR5PWScCPa/xrxvszlsznpUvYvNpIDIDeR63QliCyL/lnKYta7BnzqxdkQN40Tr4aYinYH9Xo/9OPbDxSxCiO4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735778954; c=relaxed/simple; bh=eK+vRQDEPonpk52tSxMvFuKbnk4BoEEpefZivEW6ydc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=muJl10ef5TMM0A2xXbr23zP/lCj6kEa/C4NnU2rWdmhOOlwEGymmn/tvcD1hMx/naKzS8HU75wuz4HNd1q7N72DRvGvQEVpXetSGhtDv8H89RKIJjbay3XMR0vdOSjgQB1anN8TOJFRDqP7eV8sgStecZFhPy+y4bD0pALpPrDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=habGRYJw; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="habGRYJw" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2ef6c56032eso11000946a91.2; Wed, 01 Jan 2025 16:49:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735778951; x=1736383751; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ShwJ8TLbOcKxKAI/KLvvEdy/KAkXaMIOwnIz5Y5uY0M=; b=habGRYJwgugQVFsXcA1eVc/D4AywbaBEU4mIvK35TqxGkp84b1fntqgg6w1a2dTj6g +VPqfUNAls6+vZoQ09i+ubukhgnDD9G5Xt3nhAE2vcaPZwUmt52uDEZTzi9cFMMsmBTM TQ3yOLNjSBvVlBWJxzS1oz8ipVlcEvJFCFHfWzRIohwkl9nSSHeE710ZDA72lyvwMCF8 3FMTcHiBFtfHjmya8mkCcavBHL0F1V/KbFHTUj6Y/ndeyWBAHlICr+scRf9PDJ/sj02A GgYx1O4vVKWcMi3BmVf3Ou2viclgV3nuyQmeUSn1w4CmmN76omCILIvqdI8J9fphYYxj zH8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735778951; x=1736383751; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ShwJ8TLbOcKxKAI/KLvvEdy/KAkXaMIOwnIz5Y5uY0M=; b=BXnBMHwLECLHscqbVs2oS+LeMYpO/c5Ey88Hl0OPaIlhVAbmuEZXwYGXvuCYJQC4bu WXMg8kQPTmVzrG/0jyDoyciPHmLfWZ2LMi4LlM8oONjcplRLb2v1VXgHuummFwsLIRHK Fit09053CocDi3+B3cfsEAs1sNFhejBQ6CjjO0M29QQgoMMxyEsV+HHEh5VtEGRRabMo BYEdmVjdIEMKHYo4NKC6hW/ryLeFPqWOITHDMxEz0NSXvlRcrBr5WkWsjnLuEbcog7xO CCOe18OTtzDqLSvTCtH/dDXzYlvHGafG2onhoaIDNBmHFEM8EPPAeVe6zABG1fu9VBzT hiLA== X-Forwarded-Encrypted: i=1; AJvYcCU6FYWdI6MA6XKoktubwTKMDu9v0XBhWFAdL/YlLron0wZXMIznS9U6QMs4ajLb39SFXqnSTdYAhcjb0Xp0rTd+FlF6CA==@vger.kernel.org, AJvYcCWBYxsupgqvzyO4wR2y8DpZ1ZxL6ybmAOijKHUAXPiVVim9YRNZ/v89/4/cKWUsDsJGyeZnBSwVkuna+qfG@vger.kernel.org, AJvYcCWhHZZ2QuXdUX0+CPrtIIcBr6yLcaHtudpo4VFSSrEUvik+2HaxyAMIjhA256saDINdPmEkfJ0vWdY=@vger.kernel.org X-Gm-Message-State: AOJu0YxVq55XABqiWr6fSZvvxwh43yQTslkFtm8eCJQmiLna2O2rmHji pAOn5u55n9Od+lr2cvxHxbgPs53mrIONk/OEgThB8wOTan7esx5i X-Gm-Gg: ASbGncs1SiR5Mz/vpfa02/GBGG8T1Ue3HR8ijTHDkEElLseNrAopL85+q/pxE1jw7vm pZFErf/k0hxuUtzF9nDz75H5G4tqvmDweQbNyXdctyfv0vwq/05xLi/W7Ou3jMFmROfbX+Yn6BQ ppMeRcUSgRe6TwKunIWV+Xf9uBDaP9Qlujc7l9HYpjDlQnff7IrdAn7W2tMhPoe10aRYmEmZIdL c4LCpAJa5B8svOLoeS/vS0UJYb+vMSvPxwm9MlbIbktj+DCPJ0y8QuaRxTdMPx9htYgHnK3LWgn pGH91l1NSrDn/vxcvRW3Fddg4KwtbvGt5NgE1NqRaIXIzm3qHiBtUic= X-Google-Smtp-Source: AGHT+IGT9043U9UunNh1m403Q4ZXdQUiSfcoRD7tn7EfkwYnFO3SH2gBIMdZ9zVE6kgIRhjRV2bgTQ== X-Received: by 2002:a05:6a00:4ace:b0:728:eb32:356c with SMTP id d2e1a72fcca58-72abde01b08mr52583223b3a.11.1735778951414; Wed, 01 Jan 2025 16:49:11 -0800 (PST) Received: from localhost.localdomain (108-228-232-20.lightspeed.sndgca.sbcglobal.net. [108.228.232.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad8351fasm23793594b3a.62.2025.01.01.16.49.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 16:49:11 -0800 (PST) From: "Derek J. Clark" To: Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= Cc: Jonathan Corbet , Mario Limonciello , Luke Jones , Xino Ni , Zhixin Zhang , Mia Shao , Mark Pearson , "Pierre-Loup A . Griffais" , "Cody T . -H . Chiu" , John Martens , "Derek J . Clark" , platform-driver-x86@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/4] platform/x86: Add Lenovo Other Mode WMI Driver Date: Wed, 1 Jan 2025 16:47:22 -0800 Message-ID: <20250102004854.14874-5-derekjohn.clark@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250102004854.14874-1-derekjohn.clark@gmail.com> References: <20250102004854.14874-1-derekjohn.clark@gmail.com> Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adds lenovo-wmi-other.c which provides a driver for the Lenovo "Other Mode" WMI interface that comes on some Lenovo "Gaming Series" hardware. Provides a firmware-attributes class which enables the use of tunable knobs for SPL, SPPT, and FPPT. v2: - Use devm_kzalloc to ensure driver can be instanced, remove global reference. - Ensure reverse Christmas tree for all variable declarations. - Remove extra whitespace. - Use guard(mutex) in all mutex instances, global mutex. - Use pr_fmt instead of adding the driver name to each pr_err. - Remove noisy pr_info usage. - Rename other_method_wmi to lenovo_wmi_om_priv and om_wmi to priv. - Use list to get the lenovo_wmi_om_priv instance in some macro called functions as the data provided by the macros that use it doesn't pass a member of the struct for use in container_of. - Do not rely on GameZone interface to grab the current fan mode. Signed-off-by: Derek J. Clark --- MAINTAINERS | 1 + drivers/platform/x86/Kconfig | 12 + drivers/platform/x86/Makefile | 1 + drivers/platform/x86/lenovo-wmi-other.c | 385 ++++++++++++++++++++++++ drivers/platform/x86/lenovo-wmi.h | 116 +++++++ 5 files changed, 515 insertions(+) create mode 100644 drivers/platform/x86/lenovo-wmi-other.c diff --git a/MAINTAINERS b/MAINTAINERS index c9374c395905..318e1e517eed 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13040,6 +13040,7 @@ L: platform-driver-x86@vger.kernel.org S: Maintained F: drivers/platform/x86/lenovo-wmi-capdata01.c F: drivers/platform/x86/lenovo-wmi-gamezone.c +F: drivers/platform/x86/lenovo-wmi-other.c F: drivers/platform/x86/lenovo-wmi.h LETSKETCH HID TABLET DRIVER diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index a2c1ab47ad9e..e2285ab987c5 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig @@ -481,6 +481,18 @@ config LENOVO_WMI_DATA01 To compile this driver as a module, choose M here: the module will be called lenovo_wmi_capdata01. +config LENOVO_WMI_TUNING + tristate "Lenovo Other Method WMI Driver" + depends on LENOVO_WMI_DATA01 + select FW_ATTR_CLASS + help + Say Y here if you have a WMI aware Lenovo Legion device and would like to use the + firmware_attributes API to control various tunable settings typically exposed by + Lenovo software in Windows. + + To compile this driver as a module, choose M here: the module will + be called lenovo_wmi_other. + config IDEAPAD_LAPTOP tristate "Lenovo IdeaPad Laptop Extras" depends on ACPI diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile index 6c96cc3f3855..3e059b3c3647 100644 --- a/drivers/platform/x86/Makefile +++ b/drivers/platform/x86/Makefile @@ -70,6 +70,7 @@ obj-$(CONFIG_YT2_1380) += lenovo-yoga-tab2-pro-1380-fastcharger.o obj-$(CONFIG_LENOVO_WMI_CAMERA) += lenovo-wmi-camera.o obj-$(CONFIG_LENOVO_WMI_GAMEZONE) += lenovo-wmi-gamezone.o obj-$(CONFIG_LENOVO_WMI_DATA01) += lenovo-wmi-capdata01.o +obj-$(CONFIG_LENOVO_WMI_TUNING) += lenovo-wmi-other.o # Intel obj-y += intel/ diff --git a/drivers/platform/x86/lenovo-wmi-other.c b/drivers/platform/x86/lenovo-wmi-other.c new file mode 100644 index 000000000000..2392faa74973 --- /dev/null +++ b/drivers/platform/x86/lenovo-wmi-other.c @@ -0,0 +1,385 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Lenovo Other Method WMI interface driver. This driver uses the fw_attributes + * class to expose the various WMI functions provided by the "Other Method" WMI + * interface. This enables CPU and GPU power limit as well as various other + * attributes for devices that fall under the "Gaming Series" of Lenovo laptop + * devices. Each attribute exposed by the "Other Method"" interface has a + * corresponding LENOVO_CAPABILITY_DATA_01 struct that allows the driver to + * probe details about the attribute such as set/get support, step, min, max, + * and default value. Each attibute has multiple pages, one for each of the + * fan profiles managed by the GameZone interface, so it must be probed prior + * to returning the current_value. + * + * These attributes typically don't fit anywhere else in the sysfs and are set + * in Windows using one of Lenovo's multiple user applications. + * + * Copyright(C) 2024 Derek J. Clark + */ + +#include +#include "lenovo-wmi.h" +#include "firmware_attributes_class.h" + +#define FW_ATTR_FOLDER "lenovo-wmi-other" +#define LENOVO_OTHER_METHOD_GUID "DC2A8805-3A8C-41BA-A6F7-092E0089CD3B" + +/* Device IDs */ +#define WMI_DEVICE_ID_CPU 0x01 + +/* WMI_DEVICE_ID_CPU feature IDs */ +#define WMI_FEATURE_ID_CPU_SPPT 0x01 /* Short Term Power Limit */ +#define WMI_FEATURE_ID_CPU_FPPT 0x03 /* Long Term Power Limit */ +#define WMI_FEATURE_ID_CPU_SPL 0x02 /* Peak Power Limit */ +#define WMI_FEATURE_ID_CPU_FPPT_BAD 0x03 /* Long Term Power Limit */ + +/* Method IDs */ +#define WMI_METHOD_ID_VALUE_GET 17 /* Other Method Getter */ +#define WMI_METHOD_ID_VALUE_SET 18 /* Other Method Setter */ + +static DEFINE_MUTEX(call_mutex); +static DEFINE_MUTEX(om_list_mutex); +static LIST_HEAD(om_wmi_list); + +struct lenovo_wmi_om_priv { + struct wmi_device *wdev; + struct device *fw_attr_dev; + struct kset *fw_attr_kset; + struct list_head list; +}; + +static inline struct lenovo_wmi_om_priv *get_first_wmi_priv(void) +{ + guard(mutex)(&om_list_mutex); + return list_first_entry_or_null(&om_wmi_list, struct lenovo_wmi_om_priv, + list); +} + +static const struct wmi_device_id lenovo_wmi_other_id_table[] = { + { LENOVO_OTHER_METHOD_GUID, NULL }, + {} +}; + +/* Tunable Attributes */ +struct tunable_attr_01 ppt_pl1_spl = { .device_id = WMI_DEVICE_ID_CPU, + .feature_id = WMI_FEATURE_ID_CPU_SPL }; +struct tunable_attr_01 ppt_pl2_sppt = { .device_id = WMI_DEVICE_ID_CPU, + .feature_id = WMI_FEATURE_ID_CPU_SPPT }; +struct tunable_attr_01 ppt_pl3_fppt = { .device_id = WMI_DEVICE_ID_CPU, + .feature_id = WMI_FEATURE_ID_CPU_FPPT }; + +struct capdata01_attr_group { + const struct attribute_group *attr_group; + struct tunable_attr_01 *tunable_attr; +}; + +static const struct class *fw_attr_class; + +/** + * attr_capdata01_setup() - Get the data of the specified attribute + * from LENOVO_CAPABILITY_DATA_01 and store it. + * @tunable_attr: The attribute to be populated. + * + * Returns: Either 0 or an error. + */ +static int attr_capdata01_setup(struct tunable_attr_01 *tunable_attr) +{ + struct capability_data_01 cap_data; + int mode = SMARTFAN_MODE_CUSTOM; + int err; + + struct lenovo_wmi_attr_id attr_id = { mode << 8, + tunable_attr->feature_id, + tunable_attr->device_id }; + + err = lenovo_wmi_capdata01_get(attr_id, &cap_data); + if (err) { + pr_err("Failed to get capability data: %u\n", err); + return err; + } + + if (cap_data.supported < 1) + return -EOPNOTSUPP; + + tunable_attr->capdata = cap_data; + + return 0; +} + +/** + * attr_capdata01_show() - Get the value of the specified attribute property + * from LENOVO_CAPABILITY_DATA_01. + * @kobj: Pointer to the driver object. + * @kobj_attribute: Pointer to the attribute calling this function. + * @buf: The buffer to write to. + * @tunable_attr: The attribute to be read. + * @prop: The property of this attribute to be read. + * + * This function is intended to be generic so it can be called from any "_show" + * attribute which works only with integers. + * + * If the WMI is success, then the sysfs attribute is notified. + * + * Returns: Either count, or an error. + */ +ssize_t attr_capdata01_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf, struct tunable_attr_01 *tunable_attr, + enum attribute_property prop) +{ + struct capability_data_01 cap_data; + int retval; + + cap_data = tunable_attr->capdata; + + switch (prop) { + case DEFAULT_VAL: + retval = cap_data.default_value; + break; + case MAX_VAL: + retval = cap_data.max_value; + break; + case MIN_VAL: + retval = cap_data.min_value; + break; + case STEP_VAL: + retval = cap_data.step; + break; + default: + return -EINVAL; + } + return sysfs_emit(buf, "%u\n", retval); +} + +/* Simple attribute creation */ + +/* + * att_current_value_store() - Set the current value of the given attribute + * @kobj: Pointer to the driver object. + * @kobj_attribute: Pointer to the attribute calling this function. + * @buf: The buffer to read from, this is parsed to `int` type. + * @count: Required by sysfs attribute macros, pass in from the callee attr. + * @tunable_attr: The attribute to be stored. + * + * This function is intended to be generic so it can be called from any + * attribute's "current_value_store" which works only with integers. The + * integer to be sent to the WMI method is range checked and an error returned + * if out of range. + * + * If the value is valid and WMI is success, then the sysfs attribute is + * notified. + * + * Returns: Either count, or an error. + */ +ssize_t attr_current_value_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, + size_t count, + struct tunable_attr_01 *tunable_attr) +{ + struct capability_data_01 cap_data; + struct lenovo_wmi_om_priv *priv; + int mode = SMARTFAN_MODE_CUSTOM; + u32 value; + int err; + + struct lenovo_wmi_attr_id attr_id = { mode << 8, + tunable_attr->feature_id, + tunable_attr->device_id }; + + err = kstrtouint(buf, 10, &value); + if (err) { + pr_err("Error converting value to int: %u\n", err); + return err; + } + + cap_data = tunable_attr->capdata; + + if (value < cap_data.min_value || value > cap_data.max_value) + return -EINVAL; + + priv = get_first_wmi_priv(); + if (!priv) + return -ENODEV; + + guard(mutex)(&call_mutex); + err = lenovo_wmidev_evaluate_method_2(priv->wdev, 0x0, + WMI_METHOD_ID_VALUE_SET, + *(int *)&attr_id, value, NULL); + + if (err) { + pr_err("Error setting attribute: %u\n", err); + return err; + } + + tunable_attr->store_value = value; + + sysfs_notify(kobj, NULL, attr->attr.name); + + return count; +}; + +/* + * attr_current_value_show() - Get the current value of the given attribute + * @kobj: Pointer to the driver object. + * @kobj_attribute: Pointer to the attribute calling this function. + * @buf: The buffer to write to. + * @tunable_attr: The attribute to be read. + * + * This function is intended to be generic so it can be called from any "_show" + * attribute which works only with integers. + * + * If the WMI is success, then the sysfs attribute is notified. + * + * Returns: Either count, or an error. + */ +ssize_t attr_current_value_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf, + struct tunable_attr_01 *tunable_attr) +{ + struct lenovo_wmi_om_priv *priv; + int mode = SMARTFAN_MODE_CUSTOM; + int retval; + int err; + + struct lenovo_wmi_attr_id attr_id = { mode << 8, + tunable_attr->feature_id, + tunable_attr->device_id }; + + priv = get_first_wmi_priv(); + if (!priv) + return -ENODEV; + + guard(mutex)(&call_mutex); + err = lenovo_wmidev_evaluate_method_1(priv->wdev, 0x0, + WMI_METHOD_ID_VALUE_GET, + *(int *)&attr_id, &retval); + + if (err) { + pr_err("Error getting attribute: %u\n", err); + return err; + } + + return sysfs_emit(buf, "%u\n", retval); +} + +ATTR_GROUP_LL_TUNABLE_CAP01(ppt_pl1_spl, "ppt_pl1_spl", + "Set the CPU sustained power limit"); +ATTR_GROUP_LL_TUNABLE_CAP01(ppt_pl2_sppt, "ppt_pl2_sppt", + "Set the CPU slow package power tracking limit"); +ATTR_GROUP_LL_TUNABLE_CAP01(ppt_pl3_fppt, "ppt_pl3_fppt", + "Set the CPU fast package power tracking limit"); + +static const struct capdata01_attr_group capdata01_attr_groups[] = { + { &ppt_pl1_spl_attr_group, &ppt_pl1_spl }, + { &ppt_pl2_sppt_attr_group, &ppt_pl2_sppt }, + { &ppt_pl3_fppt_attr_group, &ppt_pl3_fppt }, + {}, +}; + +static int other_method_fw_attr_add(struct lenovo_wmi_om_priv *priv) +{ + int err, i; + + err = fw_attributes_class_get(&fw_attr_class); + if (err) { + pr_err("Failed to get firmware_attributes_class: %u\n", err); + return err; + } + + priv->fw_attr_dev = device_create(fw_attr_class, NULL, MKDEV(0, 0), + NULL, "%s", FW_ATTR_FOLDER); + if (IS_ERR(priv->fw_attr_dev)) { + err = PTR_ERR(priv->fw_attr_dev); + pr_err("Failed to create firmware_attributes_class device: %u\n", + err); + goto fail_class_get; + } + + priv->fw_attr_kset = kset_create_and_add("attributes", NULL, + &priv->fw_attr_dev->kobj); + if (!priv->fw_attr_kset) { + err = -ENOMEM; + pr_err("Failed to create firmware_attributes_class kset: %u\n", + err); + goto err_destroy_classdev; + } + + for (i = 0; i < ARRAY_SIZE(capdata01_attr_groups) - 1; i++) { + err = attr_capdata01_setup( + capdata01_attr_groups[i].tunable_attr); + if (err) { + pr_err("Failed to populate capability data for %s: %u\n", + capdata01_attr_groups[i].attr_group->name, err); + continue; + } + + err = sysfs_create_group(&priv->fw_attr_kset->kobj, + capdata01_attr_groups[i].attr_group); + if (err) { + pr_err("Failed to create sysfs-group for %s: %u\n", + capdata01_attr_groups[i].attr_group->name, err); + goto err_remove_groups; + } + } + + return 0; + +err_remove_groups: + while (i-- > 0) { + sysfs_remove_group(&priv->fw_attr_kset->kobj, + capdata01_attr_groups[i].attr_group); + } + + return err; + +err_destroy_classdev: + device_destroy(fw_attr_class, MKDEV(0, 0)); + + return err; + +fail_class_get: + fw_attributes_class_put(); + + return err; +} + +static int lenovo_wmi_other_probe(struct wmi_device *wdev, const void *context) +{ + struct lenovo_wmi_om_priv *priv; + + priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->wdev = wdev; + + guard(mutex)(&om_list_mutex); + list_add_tail(&priv->list, &om_wmi_list); + + return other_method_fw_attr_add(priv); +} + +static void lenovo_wmi_other_remove(struct wmi_device *wdev) +{ + struct lenovo_wmi_om_priv *priv = dev_get_drvdata(&wdev->dev); + + guard(mutex)(&om_list_mutex); + list_del(&priv->list); + kset_unregister(priv->fw_attr_kset); + device_destroy(fw_attr_class, MKDEV(0, 0)); + fw_attributes_class_put(); +} + +static struct wmi_driver lenovo_wmi_other_driver = { + .driver = { .name = "lenovo_wmi_other" }, + .id_table = lenovo_wmi_other_id_table, + .probe = lenovo_wmi_other_probe, + .remove = lenovo_wmi_other_remove, +}; + +module_wmi_driver(lenovo_wmi_other_driver); + +MODULE_IMPORT_NS("CAPDATA_WMI"); +MODULE_DEVICE_TABLE(wmi, lenovo_wmi_other_id_table); +MODULE_AUTHOR("Derek J. Clark "); +MODULE_DESCRIPTION("Lenovo Other Method WMI Driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/platform/x86/lenovo-wmi.h b/drivers/platform/x86/lenovo-wmi.h index 53cea84a956b..1c8358551ba6 100644 --- a/drivers/platform/x86/lenovo-wmi.h +++ b/drivers/platform/x86/lenovo-wmi.h @@ -42,6 +42,14 @@ struct lenovo_wmi_attr_id { u32 device_id : 8; /* CPU/GPU/... */ } __packed; +enum attribute_property { + DEFAULT_VAL, + MAX_VAL, + MIN_VAL, + STEP_VAL, + SUPPORTED, +}; + /* Data struct for LENOVO_CAPABILITY_DATA_01 */ struct capability_data_01 { u32 id; @@ -52,6 +60,14 @@ struct capability_data_01 { u32 max_value; }; +/* Tunable attribute that uses LENOVO_CAPABILITY_DATA_01 */ +struct tunable_attr_01 { + struct capability_data_01 capdata; + u32 device_id; + u32 feature_id; + u32 store_value; +}; + /* General Use functions */ static int lenovo_wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id, struct acpi_buffer *in, @@ -122,4 +138,104 @@ int lenovo_wmidev_evaluate_method_1(struct wmi_device *wdev, u8 instance, int lenovo_wmi_capdata01_get(struct lenovo_wmi_attr_id attr_id, struct capability_data_01 *cap_data); +/* Other Method attribute functions */ +ssize_t attr_capdata01_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf, struct tunable_attr_01 *tunable_attr, + enum attribute_property prop); + +ssize_t attr_current_value_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, + size_t count, + struct tunable_attr_01 *tunable_attr); + +ssize_t attr_current_value_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf, + struct tunable_attr_01 *tunable_attr); + +ssize_t int_type_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf); + +ssize_t int_type_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "integer\n"); +} + +/* Other Method attribute macros */ +#define __LL_ATTR_RO(_func, _name) \ + { \ + .attr = { .name = __stringify(_name), .mode = 0444 }, \ + .show = _func##_##_name##_show, \ + } + +#define __LL_ATTR_RO_AS(_name, _show) \ + { \ + .attr = { .name = __stringify(_name), .mode = 0444 }, \ + .show = _show, \ + } + +#define __LL_ATTR_RW(_func, _name) \ + __ATTR(_name, 0644, _func##_##_name##_show, _func##_##_name##_store) + +/* Shows a formatted static variable */ +#define __ATTR_SHOW_FMT(_prop, _attrname, _fmt, _val) \ + static ssize_t _attrname##_##_prop##_show( \ + struct kobject *kobj, struct kobj_attribute *attr, char *buf) \ + { \ + return sysfs_emit(buf, _fmt, _val); \ + } \ + static struct kobj_attribute attr_##_attrname##_##_prop = \ + __LL_ATTR_RO(_attrname, _prop) + +/* Attribute current_value show/store */ +#define __LL_TUNABLE_RW_CAP01(_attrname) \ + static ssize_t _attrname##_current_value_store( \ + struct kobject *kobj, struct kobj_attribute *attr, \ + const char *buf, size_t count) \ + { \ + return attr_current_value_store(kobj, attr, buf, count, \ + &_attrname); \ + } \ + static ssize_t _attrname##_current_value_show( \ + struct kobject *kobj, struct kobj_attribute *attr, char *buf) \ + { \ + return attr_current_value_show(kobj, attr, buf, &_attrname); \ + } \ + static struct kobj_attribute attr_##_attrname##_current_value = \ + __LL_ATTR_RW(_attrname, current_value) + +/* Attribute property show only */ +#define __LL_TUNABLE_RO_CAP01(_prop, _attrname, _prop_type) \ + static ssize_t _attrname##_##_prop##_show( \ + struct kobject *kobj, struct kobj_attribute *attr, char *buf) \ + { \ + return attr_capdata01_show(kobj, attr, buf, &_attrname, \ + _prop_type); \ + } \ + static struct kobj_attribute attr_##_attrname##_##_prop = \ + __LL_ATTR_RO(_attrname, _prop) + +#define ATTR_GROUP_LL_TUNABLE_CAP01(_attrname, _fsname, _dispname) \ + __LL_TUNABLE_RW_CAP01(_attrname); \ + __LL_TUNABLE_RO_CAP01(default_value, _attrname, DEFAULT_VAL); \ + __ATTR_SHOW_FMT(display_name, _attrname, "%s\n", _dispname); \ + __LL_TUNABLE_RO_CAP01(max_value, _attrname, MAX_VAL); \ + __LL_TUNABLE_RO_CAP01(min_value, _attrname, MIN_VAL); \ + __LL_TUNABLE_RO_CAP01(scalar_increment, _attrname, STEP_VAL); \ + static struct kobj_attribute attr_##_attrname##_type = \ + __LL_ATTR_RO_AS(type, int_type_show); \ + static struct attribute *_attrname##_attrs[] = { \ + &attr_##_attrname##_current_value.attr, \ + &attr_##_attrname##_default_value.attr, \ + &attr_##_attrname##_display_name.attr, \ + &attr_##_attrname##_max_value.attr, \ + &attr_##_attrname##_min_value.attr, \ + &attr_##_attrname##_scalar_increment.attr, \ + &attr_##_attrname##_type.attr, \ + NULL, \ + }; \ + static const struct attribute_group _attrname##_attr_group = { \ + .name = _fsname, .attrs = _attrname##_attrs \ + } + #endif /* !_LENOVO_WMI_H_ */