Tuesday, January 4, 2022

[SOLVED] how to declare/define hwmon sensor attributes in linux client driver at runtime

Issue

I am looking for a way to create hwmon sysfs nodes at runtime from the Linux kernel driver since a client driver will not maintain sensor list in the driver.

Our Linux kernel client driver does not know the sensor attributes/list at compile time. This driver will get the list of sensors available from firmware at runtime. Based on the sensors, client driver has to create/register sysfs nodes with hwmon driver.

I am aware of declaring/adding sysfs node with hwmon device at compile time using below code snippet.

/* In client driver */
static ssize_t show_sensor1_info(struct device *dev, struct device_attribute *da, char *buf)
{
   // client driver displays sensor1 data
}

static struct sensor_device_attribute sensor_attr1 =
        SENSOR_ATTR(sensor1, 0444, show_sensor1_info, NULL, 0);

struct device *hdev = hwmon_device_register(&pdev->dev);

err = device_create_file(hdev, &sensor_attr1.dev_attr);

Output

$ ls /sys/class/hwmon/hwmon*/sensor1
sensor1

Query: Client driver will not maintain sensor list at compile time. So, how to do the same job at runtime as client driver doesn't know list of sensors available.

It will know only after this driver is loaded. Once this driver's probe is called, it requests all available sensors list from hardware monitor firmware.


Solution

I found a way to create hwmon sysfs nodes based on the runtime sensor count. I referred this code https://elixir.bootlin.com/linux/v4.3/source/drivers/hwmon/acpi_power_meter.c#L630 and it solved my requirement. Thanks.



Answered By - Raj