mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Luke D. Jones" <luke@ljones.dev>
To: hdegoede@redhat.com
Cc: ilpo.jarvinen@linux.intel.com, corentin.chary@gmail.com,
	platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org, "Luke D. Jones" <luke@ljones.dev>
Subject: [PATCH 4/9] platform/x86: asus-wmi: reduce code duplication with macros
Date: Tue, 28 May 2024 13:36:21 +1200	[thread overview]
Message-ID: <20240528013626.14066-5-luke@ljones.dev> (raw)
In-Reply-To: <20240528013626.14066-1-luke@ljones.dev>

Over time many default patterns have emerged while adding functionality.
This patch consolidates those patterns in to a few macros to remove a lot
of copy/paste, and make it easier to add more of the same style of
features in the future.

Signed-off-by: Luke D. Jones <luke@ljones.dev>
---
 drivers/platform/x86/asus-wmi.c | 215 ++++++--------------------------
 1 file changed, 38 insertions(+), 177 deletions(-)

diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
index d016acb23789..5c03e28ff252 100644
--- a/drivers/platform/x86/asus-wmi.c
+++ b/drivers/platform/x86/asus-wmi.c
@@ -676,7 +676,7 @@ static void asus_wmi_input_exit(struct asus_wmi *asus)
 static ssize_t rog_tunable_store(struct asus_wmi *asus,
 				struct attribute *attr,
 				const char *buf, size_t count,
-				u32 min, u32 max, u32 defaultv,
+				u32 min, u32 max, int defaultv,
 				u32 *store_value, u32 wmi_dev)
 {
 	int result, err, value;
@@ -685,7 +685,7 @@ static ssize_t rog_tunable_store(struct asus_wmi *asus,
 	if (result)
 		return result;
 
-	if (value == -1 )
+	if (value == -1 && defaultv != -1)
 		value = defaultv;
 	if (value < min || value > max)
 		return -EINVAL;
@@ -708,6 +708,36 @@ static ssize_t rog_tunable_store(struct asus_wmi *asus,
 	return count;
 }
 
+#define WMI_SIMPLE_STORE(_fname, _min, _max, _wmi) \
+static ssize_t _fname##_store(struct device *dev, \
+	struct device_attribute *attr, const char *buf, size_t count) \
+{ \
+	struct asus_wmi *asus = dev_get_drvdata(dev); \
+	return rog_tunable_store(asus, &attr->attr, buf, count, \
+				_min, _max, -1, NULL, _wmi); \
+}
+
+#define WMI_SIMPLE_SHOW(_fname, _fmt, _wmi) \
+static ssize_t _fname##_show(struct device *dev, \
+	struct device_attribute *attr, char *buf) \
+{ \
+	struct asus_wmi *asus = dev_get_drvdata(dev); \
+	u32 result; \
+	asus_wmi_get_devstate(asus, _wmi, &result); \
+	if (result < 0) \
+		return result; \
+	return sysfs_emit(buf, _fmt, result & ~ASUS_WMI_DSTS_PRESENCE_BIT); \
+}
+
+#define WMI_ATTR_SIMPLE_RW(_fname, _minv, _maxv, _wmi) \
+WMI_SIMPLE_STORE(_fname, _minv, _maxv, _wmi); \
+WMI_SIMPLE_SHOW(_fname, "%d\n", _wmi); \
+static DEVICE_ATTR_RW(_fname)
+
+#define WMI_ATTR_SIMPLE_RO(_fname, _wmi) \
+WMI_SIMPLE_SHOW(_fname, "%d\n", _wmi); \
+static DEVICE_ATTR_RO(_fname)
+
 #define ROG_TUNABLE_STORE(_fname, _min, _max, _default, _wmi) \
 static ssize_t _fname##_store(struct device *dev, \
 	struct device_attribute *attr, const char *buf, size_t count) \
@@ -761,6 +791,12 @@ ROG_ATTR_RW(nv_dynamic_boost,
 	NVIDIA_BOOST_MIN, nv_boost_max, nv_boost_default, ASUS_WMI_DEVID_NV_DYN_BOOST);
 ROG_ATTR_RW(nv_temp_target,
 	NVIDIA_TEMP_MIN, nv_temp_max, nv_temp_default, ASUS_WMI_DEVID_NV_THERM_TARGET);
+/* Ally MCU Powersave */
+WMI_ATTR_SIMPLE_RW(mcu_powersave, 0, 1, ASUS_WMI_DEVID_MCU_POWERSAVE);
+WMI_ATTR_SIMPLE_RO(egpu_connected, ASUS_WMI_DEVID_EGPU_CONNECTED);
+WMI_ATTR_SIMPLE_RW(panel_od, 0, 1, ASUS_WMI_DEVID_PANEL_OD);
+WMI_ATTR_SIMPLE_RW(boot_sound, 0, 1, ASUS_WMI_DEVID_BOOT_SOUND);
+WMI_ATTR_SIMPLE_RO(charge_mode, ASUS_WMI_DEVID_CHARGE_MODE);
 
 /* Tablet mode ****************************************************************/
 
@@ -776,22 +812,6 @@ static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
 		asus_wmi_tablet_sw_report(asus, result);
 }
 
-/* Charging mode, 1=Barrel, 2=USB ******************************************/
-static ssize_t charge_mode_show(struct device *dev,
-				   struct device_attribute *attr, char *buf)
-{
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-	int result, value;
-
-	result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value);
-	if (result < 0)
-		return result;
-
-	return sysfs_emit(buf, "%d\n", value & 0xff);
-}
-
-static DEVICE_ATTR_RO(charge_mode);
-
 /* dGPU ********************************************************************/
 static ssize_t dgpu_disable_show(struct device *dev,
 				   struct device_attribute *attr, char *buf)
@@ -925,22 +945,6 @@ static ssize_t egpu_enable_store(struct device *dev,
 }
 static DEVICE_ATTR_RW(egpu_enable);
 
-/* Is eGPU connected? *********************************************************/
-static ssize_t egpu_connected_show(struct device *dev,
-				   struct device_attribute *attr, char *buf)
-{
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-	int result;
-
-	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
-	if (result < 0)
-		return result;
-
-	return sysfs_emit(buf, "%d\n", result);
-}
-
-static DEVICE_ATTR_RO(egpu_connected);
-
 /* gpu mux switch *************************************************************/
 static ssize_t gpu_mux_mode_show(struct device *dev,
 				 struct device_attribute *attr, char *buf)
@@ -1128,53 +1132,6 @@ static const struct attribute_group *kbd_rgb_mode_groups[] = {
 	NULL,
 };
 
-/* Ally MCU Powersave ********************************************************/
-static ssize_t mcu_powersave_show(struct device *dev,
-				   struct device_attribute *attr, char *buf)
-{
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-	int result;
-
-	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MCU_POWERSAVE);
-	if (result < 0)
-		return result;
-
-	return sysfs_emit(buf, "%d\n", result);
-}
-
-static ssize_t mcu_powersave_store(struct device *dev,
-				    struct device_attribute *attr,
-				    const char *buf, size_t count)
-{
-	int result, err;
-	u32 enable;
-
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-
-	result = kstrtou32(buf, 10, &enable);
-	if (result)
-		return result;
-
-	if (enable > 1)
-		return -EINVAL;
-
-	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MCU_POWERSAVE, enable, &result);
-	if (err) {
-		pr_warn("Failed to set MCU powersave: %d\n", err);
-		return err;
-	}
-
-	if (result > 1) {
-		pr_warn("Failed to set MCU powersave (result): 0x%x\n", result);
-		return -EIO;
-	}
-
-	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mcu_powersave");
-
-	return count;
-}
-static DEVICE_ATTR_RW(mcu_powersave);
-
 /* Battery ********************************************************************/
 
 /* The battery maximum charging percentage */
@@ -2002,102 +1959,6 @@ static int asus_wmi_rfkill_init(struct asus_wmi *asus)
 	return result;
 }
 
-/* Panel Overdrive ************************************************************/
-static ssize_t panel_od_show(struct device *dev,
-				   struct device_attribute *attr, char *buf)
-{
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-	int result;
-
-	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD);
-	if (result < 0)
-		return result;
-
-	return sysfs_emit(buf, "%d\n", result);
-}
-
-static ssize_t panel_od_store(struct device *dev,
-				    struct device_attribute *attr,
-				    const char *buf, size_t count)
-{
-	int result, err;
-	u32 overdrive;
-
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-
-	result = kstrtou32(buf, 10, &overdrive);
-	if (result)
-		return result;
-
-	if (overdrive > 1)
-		return -EINVAL;
-
-	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result);
-
-	if (err) {
-		pr_warn("Failed to set panel overdrive: %d\n", err);
-		return err;
-	}
-
-	if (result > 1) {
-		pr_warn("Failed to set panel overdrive (result): 0x%x\n", result);
-		return -EIO;
-	}
-
-	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
-
-	return count;
-}
-static DEVICE_ATTR_RW(panel_od);
-
-/* Bootup sound ***************************************************************/
-
-static ssize_t boot_sound_show(struct device *dev,
-			     struct device_attribute *attr, char *buf)
-{
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-	int result;
-
-	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BOOT_SOUND);
-	if (result < 0)
-		return result;
-
-	return sysfs_emit(buf, "%d\n", result);
-}
-
-static ssize_t boot_sound_store(struct device *dev,
-			      struct device_attribute *attr,
-			      const char *buf, size_t count)
-{
-	int result, err;
-	u32 snd;
-
-	struct asus_wmi *asus = dev_get_drvdata(dev);
-
-	result = kstrtou32(buf, 10, &snd);
-	if (result)
-		return result;
-
-	if (snd > 1)
-		return -EINVAL;
-
-	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BOOT_SOUND, snd, &result);
-	if (err) {
-		pr_warn("Failed to set boot sound: %d\n", err);
-		return err;
-	}
-
-	if (result > 1) {
-		pr_warn("Failed to set panel boot sound (result): 0x%x\n", result);
-		return -EIO;
-	}
-
-	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "boot_sound");
-
-	return count;
-}
-static DEVICE_ATTR_RW(boot_sound);
-
 /* Mini-LED mode **************************************************************/
 static ssize_t mini_led_mode_show(struct device *dev,
 				   struct device_attribute *attr, char *buf)
-- 
2.45.1


  parent reply	other threads:[~2024-05-28  1:36 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-28  1:36 [PATCH 0/9] platform-x86-asus-wmi: multiple fixes, improvements, features Luke D. Jones
2024-05-28  1:36 ` [PATCH 1/9] platform/x86: asus-wmi: add debug print in more key places Luke D. Jones
2024-05-28  1:36 ` [PATCH 2/9] platform/x86: asus-wmi: don't fail if platform_profile already registered Luke D. Jones
2024-05-28  1:36 ` [PATCH 3/9] platform/x86: asus-wmi: add macros and expose min/max sysfs for ppt tunables Luke D. Jones
2024-05-28  8:50   ` Ilpo Järvinen
2024-06-04  5:14   ` kernel test robot
2024-05-28  1:36 ` Luke D. Jones [this message]
2024-05-28  8:55   ` [PATCH 4/9] platform/x86: asus-wmi: reduce code duplication with macros Ilpo Järvinen
2024-05-28  9:03   ` Ilpo Järvinen
2024-05-28  1:36 ` [PATCH 5/9] platform/x86: asus-wmi: use WMI_SIMPLE_SHOW in more places Luke D. Jones
2024-05-28  9:06   ` Ilpo Järvinen
2024-05-28  1:36 ` [PATCH 6/9] platform/x86: asus-wmi: add panel-fhd functionality Luke D. Jones
2024-05-28  9:18   ` Ilpo Järvinen
2024-05-28  1:36 ` [PATCH 7/9] platform/x86: asus-wmi: add enable/disable CPU cores Luke D. Jones
2024-05-28  9:27   ` Ilpo Järvinen
2024-05-28 21:37     ` Luke Jones
2024-05-28  1:36 ` [PATCH 8/9] platform/x86: asus-wmi: add apu_mem setting Luke D. Jones
2024-05-28  2:19   ` Limonciello, Mario
2024-05-28  2:40     ` Luke Jones
2024-05-28 13:27       ` Mario Limonciello
2024-05-28 21:04         ` Luke Jones
2024-05-28 21:16           ` Mario Limonciello
2024-05-28 21:34             ` Luke Jones
2024-05-28 21:36               ` Mario Limonciello
2024-05-28 21:37               ` Mario Limonciello
2024-05-28  1:36 ` [PATCH 9/9] platform/x86: asus-wmi: add setting dGPU TGP Luke D. Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240528013626.14066-5-luke@ljones.dev \
    --to=luke@ljones.dev \
    --cc=corentin.chary@gmail.com \
    --cc=hdegoede@redhat.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®