Some laptops have a key to switch platform profiles.
Add a platform_profile_cycle() function to cycle between the enabled
profiles.
Signed-off-by: Gergo Koteles <soyer@irl.hu>
---
drivers/acpi/platform_profile.c | 42 ++++++++++++++++++++++++++++++++
include/linux/platform_profile.h | 1 +
2 files changed, 43 insertions(+)
diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
index d418462ab791..1579f380d469 100644
--- a/drivers/acpi/platform_profile.c
+++ b/drivers/acpi/platform_profile.c
@@ -136,6 +136,48 @@ 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 = ffs(cur_profile->choices[0] >> (profile + 1)) + profile;
+
+ /* current profile is the highest, select the lowest */
+ if (next == profile)
+ next = ffs(cur_profile->choices[0]) - 1;
+
+ if (WARN_ON((next < 0) || (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;
diff --git a/include/linux/platform_profile.h b/include/linux/platform_profile.h
index e5cbb6841f3a..f5492ed413f3 100644
--- a/include/linux/platform_profile.h
+++ b/include/linux/platform_profile.h
@@ -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_*/
--
2.44.0
Hi
2024. április 5., péntek 5:05 keltezéssel, Gergo Koteles <soyer@irl.hu> írta:
> Some laptops have a key to switch platform profiles.
>
> Add a platform_profile_cycle() function to cycle between the enabled
> profiles.
>
> Signed-off-by: Gergo Koteles <soyer@irl.hu>
> ---
> drivers/acpi/platform_profile.c | 42 ++++++++++++++++++++++++++++++++
> include/linux/platform_profile.h | 1 +
> 2 files changed, 43 insertions(+)
>
> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
> index d418462ab791..1579f380d469 100644
> --- a/drivers/acpi/platform_profile.c
> +++ b/drivers/acpi/platform_profile.c
> @@ -136,6 +136,48 @@ 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 = ffs(cur_profile->choices[0] >> (profile + 1)) + profile;
> +
> + /* current profile is the highest, select the lowest */
> + if (next == profile)
> + next = ffs(cur_profile->choices[0]) - 1;
I think you can use `find_next_bit()` or similar instead.
> +
> + if (WARN_ON((next < 0) || (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;
> diff --git a/include/linux/platform_profile.h b/include/linux/platform_profile.h
> index e5cbb6841f3a..f5492ed413f3 100644
> --- a/include/linux/platform_profile.h
> +++ b/include/linux/platform_profile.h
> @@ -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_*/
> --
> 2.44.0
>
Regards,
Barnabás Pőcze
Hi Barnabás, On Fri, 2024-04-05 at 16:34 +0000, Barnabás Pőcze wrote: > > + next = ffs(cur_profile->choices[0] >> (profile + 1)) + profile; > > + > > + /* current profile is the highest, select the lowest */ > > + if (next == profile) > > + next = ffs(cur_profile->choices[0]) - 1; > > I think you can use `find_next_bit()` or similar instead. > Thanks, it looks much better with find_next_bit_wrap. > Best regards, Gergo
Hi Gergo,
please Cc people who commented your changes.
see below:
On 05/04/2024 05:05, Gergo Koteles wrote:
> Some laptops have a key to switch platform profiles.
>
> Add a platform_profile_cycle() function to cycle between the enabled
> profiles.
>
> Signed-off-by: Gergo Koteles <soyer@irl.hu>
> ---
> drivers/acpi/platform_profile.c | 42 ++++++++++++++++++++++++++++++++
> include/linux/platform_profile.h | 1 +
> 2 files changed, 43 insertions(+)
>
> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
> index d418462ab791..1579f380d469 100644
> --- a/drivers/acpi/platform_profile.c
> +++ b/drivers/acpi/platform_profile.c
> @@ -136,6 +136,48 @@ 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 = ffs(cur_profile->choices[0] >> (profile + 1)) + profile;
> +
> + /* current profile is the highest, select the lowest */
> + if (next == profile)
> + next = ffs(cur_profile->choices[0]) - 1;
> +
> + if (WARN_ON((next < 0) || (next >= ARRAY_SIZE(profile_names)))) {
> + mutex_unlock(&profile_lock);
> + return -EINVAL;
> + }
Why do you need to do this?
That can be simplified by:
[ ... ]
err = cur_profile->profile_get(cur_profile, &profile);
if (err)
goto out;
profile = (profile + 1) % ARRAY_SIZE(profile_names);
err = cur_profile->profile_set(cur_profile, next);
out:
mutex_unlock(&profile_lock);
[ ... ]
> + 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;
> diff --git a/include/linux/platform_profile.h b/include/linux/platform_profile.h
> index e5cbb6841f3a..f5492ed413f3 100644
> --- a/include/linux/platform_profile.h
> +++ b/include/linux/platform_profile.h
> @@ -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_*/
--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs
Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
Hi Daniel,
On Fri, 2024-04-05 at 08:48 +0200, Daniel Lezcano wrote:
> Hi Gergo,
>
> please Cc people who commented your changes.
Thanks for this info, next time :)
> > +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 = ffs(cur_profile->choices[0] >> (profile + 1)) + profile;
> > +
> > + /* current profile is the highest, select the lowest */
> > + if (next == profile)
> > + next = ffs(cur_profile->choices[0]) - 1;
> > +
> > + if (WARN_ON((next < 0) || (next >= ARRAY_SIZE(profile_names)))) {
> > + mutex_unlock(&profile_lock);
> > + return -EINVAL;
> > + }
>
> Why do you need to do this?
>
Many platform drivers use the platform profile module. They support
different sets of profiles, not all. They sets the corresponding bits
in the choices variable.
like this:
set_bit(PLATFORM_PROFILE_BALANCED, platform_profile_handler.choices);
The platform_profile_cycle() cycles through the enabled profiles.
Best regards,
Gergo
> That can be simplified by:
>
> [ ... ]
>
> err = cur_profile->profile_get(cur_profile, &profile);
> if (err)
> goto out;
>
> profile = (profile + 1) % ARRAY_SIZE(profile_names);
>
> err = cur_profile->profile_set(cur_profile, next);
> out:
> mutex_unlock(&profile_lock);
>
> [ ... ]
>
> > + 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;
> > diff --git a/include/linux/platform_profile.h b/include/linux/platform_profile.h
> > index e5cbb6841f3a..f5492ed413f3 100644
> > --- a/include/linux/platform_profile.h
> > +++ b/include/linux/platform_profile.h
> > @@ -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_*/
>
© 2016 - 2026 Red Hat, Inc.