Skip to contentSkip to conversation

switch platform profiles with Lenovo laptops

PATCH Superseded Gergo Koteles 3 participants 7 messages Apr 6, 2024 – Apr 8, 2024

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover switch platform profiles with Lenovo laptops

Hi All,

This patch series adds a platform_profile_cycle function to the platform profile module, which allows modules to easily switch between the enabled profiles.

Use it in ideapad-laptop and thinkpad-acpi modules.

Best regards,
Gergo

Changes in v5:
- use find_next_bit_wrap function to find the next enabled profile instead of multiple ffs calls

Changes in v4:
- move the cycle to the platform profile module where it can switch between the enabled profiles
- add a patch to use it in the thinkpad-acpi module

Changes in v3:
- add dytc_profile_cycle function

Changes in v2:
- only switch platform profiles if supported, otherwise keep the behavior.

[4]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/7c358ad8dd6de7889fa887954145a181501ac362.1712236099.git.soyer@irl.hu/
[2]: https://lore.kernel.org/all/797884d8cab030d3a2b656dba67f3c423cc58be7.1712174794.git.soyer@irl.hu/
[1]: https://lore.kernel.org/all/85254ce8e87570c05e7f04d6507701bef954ed75.1712149429.git.soyer@irl.hu/

Gergo Koteles (3):
ACPI: platform-profile: add platform_profile_cycle() platform/x86: ideapad-laptop: switch platform profiles using thermal
management key
platform/x86: thinkpad_acpi: use platform_profile_cycle()

 drivers/acpi/platform_profile.c       | 39 +++++++++++++++++++++++++++
 drivers/platform/x86/ideapad-laptop.c |  7 +++--
 drivers/platform/x86/thinkpad_acpi.c  | 19 ++-----------
 include/linux/platform_profile.h      |  1 +
 4 files changed, 47 insertions(+), 19 deletions(-)

base-commit: 39cd87c4eb2b893354f3b850f916353f2658ae6f

Signature · 1 line
2.44.0
#1 lore raw

PATCH v5 1/3 ACPI: platform-profile: add platform_profile_cycle()

Some laptops have a key to switch platform profiles.

Add a platform_profile_cycle() function to cycle between the enabled profiles.

---
 drivers/acpi/platform_profile.c  | 39 ++++++++++++++++++++++++++++++++
 include/linux/platform_profile.h |  1 +
 2 files changed, 40 insertions(+)
drivers/acpi/platform_profile.c +39 -0
index d418462ab791..acc606af812a 100644
@@ -136,6 +136,45 @@ void platform_profile_notify(void)
 }
 EXPORT_SYMBOL_GPL(platform_profile_notify);
 
+int platform_profile_cycle(void)
+{
+	enum platform_profile_option profile;
+	enum platform_profile_option next;
+	int err;
+
+	err = mutex_lock_interruptible(&profile_lock);
+	if (err)
+		return err;
+
+	if (!cur_profile) {
+		mutex_unlock(&profile_lock);
+		return -ENODEV;
+	}
+
+	err = cur_profile->profile_get(cur_profile, &profile);
+	if (err) {
+		mutex_unlock(&profile_lock);
+		return err;
+	}
+
+	next = find_next_bit_wrap(cur_profile->choices,
+				  ARRAY_SIZE(profile_names), profile + 1);
+
+	if (WARN_ON(next == ARRAY_SIZE(profile_names))) {
+		mutex_unlock(&profile_lock);
+		return -EINVAL;
+	}
+
+	err = cur_profile->profile_set(cur_profile, next);
+	mutex_unlock(&profile_lock);
+
+	if (!err)
+		sysfs_notify(acpi_kobj, NULL, "platform_profile");
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(platform_profile_cycle);
+
 int platform_profile_register(struct platform_profile_handler *pprof)
 {
 	int err;
include/linux/platform_profile.h +1 -0
index e5cbb6841f3a..f5492ed413f3 100644
@@ -36,6 +36,7 @@ struct platform_profile_handler {
 
 int platform_profile_register(struct platform_profile_handler *pprof);
 int platform_profile_remove(void);
+int platform_profile_cycle(void);
 void platform_profile_notify(void);
 
 #endif  /*_PLATFORM_PROFILE_H_*/
Signature · 1 line
2.44.0
#5 lore raw

PATCH v5 2/3 platform/x86: ideapad-laptop: switch platform profiles using thermal management key

In reply to Gergo Koteles

Ideapad laptops have thermal management or performance mode switch key (Fn + Q). They report KEY_PROG4.

If supported, cycle between platform profiles instead.

Tested on Yoga7 14ARB7.

---
 drivers/platform/x86/ideapad-laptop.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/platform/x86/ideapad-laptop.c +5 -2
index 901849810ce2..4893b1aef696 100644
@@ -1181,8 +1181,11 @@ static void ideapad_check_special_buttons(struct ideapad_private *priv)
 		switch (bit) {
 		case 6:	/* Z570 */
 		case 0:	/* Z580 */
-			/* Thermal Management button */
-			ideapad_input_report(priv, 65);
+			/* Thermal Management / Performance Mode button */
+			if (priv->dytc)
+				platform_profile_cycle();
+			else
+				ideapad_input_report(priv, 65);
 			break;
 		case 1:
 			/* OneKey Theater button */
Signature · 1 line
2.44.0
#6 lore raw

PATCH v5 3/3 platform/x86: thinkpad_acpi: use platform_profile_cycle()

In reply to Gergo Koteles

Some Thinkpads have a 'mode' button that switches between platform profiles.

Use the new platform_module_cycle function instead of the existing switch-based one.

---
 drivers/platform/x86/thinkpad_acpi.c | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +2 -17
index 82429e59999d..771aaa7ae4cf 100644
@@ -11190,23 +11190,8 @@ static void tpacpi_driver_event(const unsigned int hkey_event)
 		else
 			dytc_control_amt(!dytc_amt_active);
 	}
-	if (hkey_event == TP_HKEY_EV_PROFILE_TOGGLE) {
-		switch (dytc_current_profile) {
-		case PLATFORM_PROFILE_LOW_POWER:
-			dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
-			break;
-		case PLATFORM_PROFILE_BALANCED:
-			dytc_profile_set(NULL, PLATFORM_PROFILE_PERFORMANCE);
-			break;
-		case PLATFORM_PROFILE_PERFORMANCE:
-			dytc_profile_set(NULL, PLATFORM_PROFILE_LOW_POWER);
-			break;
-		default:
-			pr_warn("Profile HKEY unexpected profile %d", dytc_current_profile);
-		}
-		/* Notify user space the profile changed */
-		platform_profile_notify();
-	}
+	if (hkey_event == TP_HKEY_EV_PROFILE_TOGGLE)
+		platform_profile_cycle();
 }
 
 static void hotkey_driver_event(const unsigned int scancode)
Signature · 1 line
2.44.0
#2 lore raw

Re: [PATCH v5 1/3] ACPI: platform-profile: add platform_profile_cycle()

In reply to Gergo Koteles

Hi Gergo,

Quoted 6 lines · Gergo Koteles

Some laptops have a key to switch platform profiles.

Add a platform_profile_cycle() function to cycle between the enabled profiles.

Thank you for your patch, 1 small remark below,
otherwise this looks good to me.

Rafael, may I have your Ack for merging this through the pdx86 tree together with the rest of the series once my remark has been addressed ?

Quoted 28 lines
---
 drivers/acpi/platform_profile.c  | 39 ++++++++++++++++++++++++++++++++
 include/linux/platform_profile.h |  1 +
 2 files changed, 40 insertions(+)
drivers/acpi/platform_profile.c +15 -0
index d418462ab791..acc606af812a 100644
@@ -136,6 +136,45 @@ void platform_profile_notify(void)
 }
 EXPORT_SYMBOL_GPL(platform_profile_notify);
 
+int platform_profile_cycle(void)
+{
+	enum platform_profile_option profile;
+	enum platform_profile_option next;
+	int err;
+
+	err = mutex_lock_interruptible(&profile_lock);
+	if (err)
+		return err;
+
+	if (!cur_profile) {
+		mutex_unlock(&profile_lock);
+		return -ENODEV;
+	}
+

drivers/acpi/platform_profile.c void platform_profile_notify(void) · line 163

Quoted 10 lines
drivers/acpi/platform_profile.c +10 -0
+	err = cur_profile->profile_get(cur_profile, &profile);
+	if (err) {
+		mutex_unlock(&profile_lock);
+		return err;
+	}
+
+	next = find_next_bit_wrap(cur_profile->choices,
+				  ARRAY_SIZE(profile_names), profile + 1);
+
+	if (WARN_ON(next == ARRAY_SIZE(profile_names))) {

Other code in drivers/acpi/platform_profile.c uses PLATFORM_PROFILE_LAST instead of ARRAY_SIZE(profile_names) (these are guaranteed to be equal) please switch to using PLATFORM_PROFILE_LAST for consistency.

Regards,

Hans

Quoted 29 lines
drivers/acpi/platform_profile.c +14 -0
+		mutex_unlock(&profile_lock);
+		return -EINVAL;
+	}
+
+	err = cur_profile->profile_set(cur_profile, next);
+	mutex_unlock(&profile_lock);
+
+	if (!err)
+		sysfs_notify(acpi_kobj, NULL, "platform_profile");
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(platform_profile_cycle);
+
 int platform_profile_register(struct platform_profile_handler *pprof)
 {
 	int err;
include/linux/platform_profile.h +1 -0
index e5cbb6841f3a..f5492ed413f3 100644
@@ -36,6 +36,7 @@ struct platform_profile_handler {
 
 int platform_profile_register(struct platform_profile_handler *pprof);
 int platform_profile_remove(void);
+int platform_profile_cycle(void);
 void platform_profile_notify(void);
 
 #endif  /*_PLATFORM_PROFILE_H_*/
maintainer Acked-by
#3 lore raw

Re: [PATCH v5 1/3] ACPI: platform-profile: add platform_profile_cycle()

Quoted 9 lines · Hans de Goede

Hi Gergo,

Quoted 5 lines · Gergo Koteles

Some laptops have a key to switch platform profiles.

Add a platform_profile_cycle() function to cycle between the enabled profiles.

Quoted 7 lines
Quoted 1 line

Thank you for your patch, 1 small remark below,
otherwise this looks good to me.

Rafael, may I have your Ack for merging this through the pdx86 tree together with the rest of the series once my remark has been addressed ?

Sure, please feel free to add

to this patch. Thanks!

Quoted 81 lines
Quoted 28 lines
---
 drivers/acpi/platform_profile.c  | 39 ++++++++++++++++++++++++++++++++
 include/linux/platform_profile.h |  1 +
 2 files changed, 40 insertions(+)
drivers/acpi/platform_profile.c +15 -0
index d418462ab791..acc606af812a 100644
@@ -136,6 +136,45 @@ void platform_profile_notify(void)
 }
 EXPORT_SYMBOL_GPL(platform_profile_notify);
 
+int platform_profile_cycle(void)
+{
+     enum platform_profile_option profile;
+     enum platform_profile_option next;
+     int err;
+
+     err = mutex_lock_interruptible(&profile_lock);
+     if (err)
+             return err;
+
+     if (!cur_profile) {
+             mutex_unlock(&profile_lock);
+             return -ENODEV;
+     }
+

drivers/acpi/platform_profile.c void platform_profile_notify(void) · line 163

Quoted 10 lines
drivers/acpi/platform_profile.c +10 -0
+     err = cur_profile->profile_get(cur_profile, &profile);
+     if (err) {
+             mutex_unlock(&profile_lock);
+             return err;
+     }
+
+     next = find_next_bit_wrap(cur_profile->choices,
+                               ARRAY_SIZE(profile_names), profile + 1);
+
+     if (WARN_ON(next == ARRAY_SIZE(profile_names))) {

Other code in drivers/acpi/platform_profile.c uses PLATFORM_PROFILE_LAST instead of ARRAY_SIZE(profile_names) (these are guaranteed to be equal) please switch to using PLATFORM_PROFILE_LAST for consistency.

Regards,

Hans

Quoted 29 lines
drivers/acpi/platform_profile.c +14 -0
+             mutex_unlock(&profile_lock);
+             return -EINVAL;
+     }
+
+     err = cur_profile->profile_set(cur_profile, next);
+     mutex_unlock(&profile_lock);
+
+     if (!err)
+             sysfs_notify(acpi_kobj, NULL, "platform_profile");
+
+     return err;
+}
+EXPORT_SYMBOL_GPL(platform_profile_cycle);
+
 int platform_profile_register(struct platform_profile_handler *pprof)
 {
      int err;
include/linux/platform_profile.h +1 -0
index e5cbb6841f3a..f5492ed413f3 100644
@@ -36,6 +36,7 @@ struct platform_profile_handler {
 
 int platform_profile_register(struct platform_profile_handler *pprof);
 int platform_profile_remove(void);
+int platform_profile_cycle(void);
 void platform_profile_notify(void);
 
 #endif  /*_PLATFORM_PROFILE_H_*/
#4 lore raw

Re: [PATCH v5 1/3] ACPI: platform-profile: add platform_profile_cycle()

In reply to Hans de Goede

Hi Hans,

Quoted 9 lines · Hans de Goede

drivers/acpi/platform_profile.c void platform_profile_notify(void) · line 163

Quoted 4 lines
drivers/acpi/platform_profile.c +4 -0
+	next = find_next_bit_wrap(cur_profile->choices,
+				  ARRAY_SIZE(profile_names), profile + 1);
+
+	if (WARN_ON(next == ARRAY_SIZE(profile_names))) {

Other code in drivers/acpi/platform_profile.c uses PLATFORM_PROFILE_LAST instead of ARRAY_SIZE(profile_names) (these are guaranteed to be equal) please switch to using PLATFORM_PROFILE_LAST for consistency.

Thanks for the review. I changed these in v6.

Best regards,
Gergo