Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Armin Wolf <W_Armin@gmx.de>
To: Antheas Kapenekakis <lkml@antheas.dev>
Cc: platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Derek John Clark" <derekjohn.clark@gmail.com>,
	"Joaquín Ignacio Aramendía" <samsagax@gmail.com>,
	"Jean Delvare" <jdelvare@suse.com>,
	"Guenter Roeck" <linux@roeck-us.net>
Subject: Re: [PATCH v1 2/5] platform/x86: ayaneo-ec: Add hwmon support
Date: Fri, 5 Sep 2025 20:49:02 +0200	[thread overview]
Message-ID: <d59d64b4-653c-4a78-9c6f-47fbe40555b4@gmx.de> (raw)
In-Reply-To: <CAGwozwEV6QVqZKVfO++W0v0g7w9rWE2Joup=w9vPibP=hAzx4A@mail.gmail.com>

Am 22.08.25 um 22:56 schrieb Antheas Kapenekakis:

> On Fri, 22 Aug 2025 at 14:35, Armin Wolf <W_Armin@gmx.de> wrote:
>> Am 20.08.25 um 18:06 schrieb Antheas Kapenekakis:
>>
>>> Add hwmon single fan sensor reads and control for Ayaneo devices.
>>> The register and method of access is the same for all devices.
>>>
>>> Signed-off-by: Antheas Kapenekakis <lkml@antheas.dev>
>>> ---
>>>    drivers/platform/x86/Kconfig     |   2 +
>>>    drivers/platform/x86/ayaneo-ec.c | 132 +++++++++++++++++++++++++++++++
>>>    2 files changed, 134 insertions(+)
>>>
>>> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
>>> index 6d4a33791cc1..0a7ca2c78456 100644
>>> --- a/drivers/platform/x86/Kconfig
>>> +++ b/drivers/platform/x86/Kconfig
>>> @@ -307,6 +307,8 @@ config ASUS_TF103C_DOCK
>>>    config AYANEO_EC
>>>        tristate "Ayaneo EC platform control"
>>>        depends on X86
>>> +     depends on ACPI_EC
>>> +     depends on HWMON
>>>        help
>>>          Enables support for the platform EC of Ayaneo devices. This
>>>          includes fan control, fan speed, charge limit, magic
>>> diff --git a/drivers/platform/x86/ayaneo-ec.c b/drivers/platform/x86/ayaneo-ec.c
>>> index 90b86527ab0d..8b1902706b81 100644
>>> --- a/drivers/platform/x86/ayaneo-ec.c
>>> +++ b/drivers/platform/x86/ayaneo-ec.c
>>> @@ -7,13 +7,23 @@
>>>     * Copyright (C) 2025 Antheas Kapenekakis <lkml@antheas.dev>
>>>     */
>>>
>>> +#include <linux/acpi.h>
>>>    #include <linux/dmi.h>
>>> +#include <linux/hwmon.h>
>>>    #include <linux/init.h>
>>>    #include <linux/kernel.h>
>>>    #include <linux/module.h>
>>>    #include <linux/platform_device.h>
>>>
>>> +#define AYANEO_PWM_ENABLE_REG         0x4A
>>> +#define AYANEO_PWM_REG                0x4B
>>> +#define AYANEO_PWM_MODE_AUTO  0x00
>>> +#define AYANEO_PWM_MODE_MANUAL        0x01
>>> +
>>> +#define AYANEO_FAN_REG                0x76
>>> +
>>>    struct ayaneo_ec_quirk {
>>> +     bool has_fan_control;
>>>    };
>>>
>>>    struct ayaneo_ec_platform_data {
>>> @@ -22,6 +32,7 @@ struct ayaneo_ec_platform_data {
>>>    };
>>>
>>>    static const struct ayaneo_ec_quirk ayaneo3 = {
>>> +     .has_fan_control = true,
>>>    };
>>>
>>>    static const struct dmi_system_id dmi_table[] = {
>>> @@ -35,10 +46,124 @@ static const struct dmi_system_id dmi_table[] = {
>>>        {},
>>>    };
>>>
>>> +/* Callbacks for hwmon interface */
>>> +static umode_t ayaneo_ec_hwmon_is_visible(const void *drvdata,
>>> +                                    enum hwmon_sensor_types type, u32 attr,
>>> +                                    int channel)
>>> +{
>>> +     switch (type) {
>>> +     case hwmon_fan:
>>> +             return 0444;
>>> +     case hwmon_pwm:
>>> +             return 0644;
>>> +     default:
>>> +             return 0;
>>> +     }
>>> +}
>>> +
>>> +static int ayaneo_ec_read(struct device *dev, enum hwmon_sensor_types type,
>>> +                          u32 attr, int channel, long *val)
>>> +{
>>> +     u8 tmp;
>>> +     int ret;
>>> +
>>> +     switch (type) {
>>> +     case hwmon_fan:
>>> +             switch (attr) {
>>> +             case hwmon_fan_input:
>>> +                     ret = ec_read(AYANEO_FAN_REG, &tmp);
>>> +                     if (ret)
>>> +                             return ret;
>>> +                     *val = tmp << 8;
>>> +                     ret = ec_read(AYANEO_FAN_REG + 1, &tmp);
>>> +                     if (ret)
>>> +                             return ret;
>>> +                     *val += tmp;
>>> +                     return 0;
>>> +             default:
>>> +                     break;
>>> +             }
>>> +             break;
>>> +     case hwmon_pwm:
>>> +             switch (attr) {
>>> +             case hwmon_pwm_input:
>>> +                     ret = ec_read(AYANEO_PWM_REG, &tmp);
>>> +                     if (ret)
>>> +                             return ret;
>>> +                     *val = (255 * tmp) / 100;
>>> +                     if (*val < 0 || *val > 255)
>>> +                             return -EINVAL;
>>> +                     return 0;
>>> +             case hwmon_pwm_enable:
>>> +                     ret = ec_read(AYANEO_PWM_ENABLE_REG, &tmp);
>>> +                     if (ret)
>>> +                             return ret;
>>> +                     if (tmp == AYANEO_PWM_MODE_MANUAL)
>>> +                             *val = 1;
>>> +                     else
>>> +                             *val = 2;
>>> +                     return 0;
>>> +             default:
>>> +                     break;
>>> +             }
>>> +             break;
>>> +     default:
>>> +             break;
>>> +     }
>>> +     return -EOPNOTSUPP;
>>> +}
>>> +
>>> +static int ayaneo_ec_write(struct device *dev, enum hwmon_sensor_types type,
>>> +                           u32 attr, int channel, long val)
>>> +{
>>> +     switch (type) {
>>> +     case hwmon_pwm:
>>> +             switch (attr) {
>>> +             case hwmon_pwm_enable:
>>> +                     if (val == 1)
>>> +                             return ec_write(AYANEO_PWM_ENABLE_REG,
>>> +                                             AYANEO_PWM_MODE_MANUAL);
>>> +                     else if (val == 2)
>>> +                             return ec_write(AYANEO_PWM_ENABLE_REG,
>>> +                                             AYANEO_PWM_MODE_AUTO);
>>> +                     else
>>> +                             return -EINVAL;
>> Consider using a switch statement here.
>>
>>> +             case hwmon_pwm_input:
>>> +                     if (val < 0 || val > 255)
>>> +                             return -EINVAL;
>>> +                     return ec_write(AYANEO_PWM_REG, (val * 100) / 255);
>> Better use fixp_linear_interpolate() for that. Also you need to restore those registers
>> after suspend. I suggest that you use regmap for that.
> The registers appear to maintain their state after sleep. If I get a
> report that some devices do not, I will amend the driver with a
> suspend hook.

Even during hibernation?

Thanks,
Armin Wolf

>
> ack on the interpolate.
>
> Antheas
>
>> Thanks,
>> Armin Wolf
>>
>>> +             default:
>>> +                     break;
>>> +             }
>>> +             break;
>>> +     default:
>>> +             break;
>>> +     }
>>> +     return -EOPNOTSUPP;
>>> +}
>>> +
>>> +static const struct hwmon_ops ayaneo_ec_hwmon_ops = {
>>> +     .is_visible = ayaneo_ec_hwmon_is_visible,
>>> +     .read = ayaneo_ec_read,
>>> +     .write = ayaneo_ec_write,
>>> +};
>>> +
>>> +static const struct hwmon_channel_info *const ayaneo_ec_sensors[] = {
>>> +     HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
>>> +     HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
>>> +     NULL,
>>> +};
>>> +
>>> +static const struct hwmon_chip_info ayaneo_ec_chip_info = {
>>> +     .ops = &ayaneo_ec_hwmon_ops,
>>> +     .info = ayaneo_ec_sensors,
>>> +};
>>> +
>>>    static int ayaneo_ec_probe(struct platform_device *pdev)
>>>    {
>>>        const struct dmi_system_id *dmi_entry;
>>>        struct ayaneo_ec_platform_data *data;
>>> +     struct device *hwdev;
>>>
>>>        dmi_entry = dmi_first_match(dmi_table);
>>>        if (!dmi_entry)
>>> @@ -52,6 +177,13 @@ static int ayaneo_ec_probe(struct platform_device *pdev)
>>>        data->quirks = dmi_entry->driver_data;
>>>        platform_set_drvdata(pdev, data);
>>>
>>> +     if (data->quirks->has_fan_control) {
>>> +             hwdev = devm_hwmon_device_register_with_info(
>>> +                     &pdev->dev, "ayaneo_ec", NULL, &ayaneo_ec_chip_info, NULL);
>>> +             if (IS_ERR(hwdev))
>>> +                     return PTR_ERR(hwdev);
>>> +     }
>>> +
>>>        return 0;
>>>    }
>>>

  reply	other threads:[~2025-09-05 18:49 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-20 16:06 [PATCH v1 0/5] platform/x86: ayaneo-ec: Add Ayaneo Embedded Controller platform driver Antheas Kapenekakis
2025-08-20 16:06 ` [PATCH v1 1/5] " Antheas Kapenekakis
2025-08-20 16:06 ` [PATCH v1 2/5] platform/x86: ayaneo-ec: Add hwmon support Antheas Kapenekakis
2025-08-22 12:35   ` Armin Wolf
2025-08-22 20:56     ` Antheas Kapenekakis
2025-09-05 18:49       ` Armin Wolf [this message]
2025-08-20 16:06 ` [PATCH v1 3/5] platform/x86: ayaneo-ec: Add charge control support Antheas Kapenekakis
2025-08-22 12:38   ` Armin Wolf
2025-08-22 21:04     ` Antheas Kapenekakis
2025-09-05 18:54       ` Armin Wolf
2025-08-20 16:06 ` [PATCH v1 4/5] platform/x86: ayaneo-ec: Add controller power and modules attributes Antheas Kapenekakis
2025-08-21  8:41   ` kernel test robot
2025-08-21  8:41   ` kernel test robot
2025-08-22 12:40   ` Armin Wolf
2025-08-22 21:08     ` Antheas Kapenekakis
2025-09-05 19:05       ` Armin Wolf
2025-08-20 16:06 ` [PATCH v1 5/5] platform/x86: ayaneo-ec: Move Ayaneo devices from oxpec to ayaneo-ec Antheas Kapenekakis
2025-08-23  0:07   ` Derek John Clark

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=d59d64b4-653c-4a78-9c6f-47fbe40555b4@gmx.de \
    --to=w_armin@gmx.de \
    --cc=derekjohn.clark@gmail.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=lkml@antheas.dev \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=samsagax@gmail.com \
    /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