From d43c92e2a1abc64e86c59529ec02d3eb394ccf4d Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Fri, 11 Aug 2023 09:48:06 +0200 Subject: [PATCH] Change sysfs architecture and add power,current and voltage metrics --- ina260.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/ina260.c b/ina260.c index 6bdaa6d..8a99b2e 100644 --- a/ina260.c +++ b/ina260.c @@ -16,7 +16,6 @@ #define INA260_REG_MANUFACTURER 0xFE #define INA260_REG_DIE 0xFF -#define INA260_LSB_POWER 10 // 10mW #define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0) // ina260 average modes list @@ -156,12 +155,26 @@ static ssize_t attr_metric_show(struct kobject *_kobj, char *buf) { int rvalue; + int op1, op2; struct client_data *cdata=container_of(_kobj,struct client_data,kobj); if(INA260_IS_ATTR(power)){ if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue)) - return -1; - return sprintf(buf, "%d.%d\n", INA260_LSB_POWER*rvalue/1000,(INA260_LSB_POWER*rvalue) % 1000); + return -1; + op1=10*rvalue; + return sprintf(buf, "%d.%d\n", op1/1000,(op1) % 1000); + } else if(INA260_IS_ATTR(voltage)){ + if(ina260_read_register(cdata, INA260_REG_VOLTAGE,&rvalue)) + return -1; + op1=rvalue*25/100 + rvalue; + op2=rvalue*25%100; + return sprintf(buf, "%d.%03d%d\n",(op1)/1000,(op1)%1000,op2); + } else if(INA260_IS_ATTR(current)){ + if(ina260_read_register(cdata, INA260_REG_CURRENT,&rvalue)) + return -1; + op1=rvalue*25/100 + rvalue; + op2=rvalue*25%100; + return sprintf(buf, "%d.%03d%d\n",(op1)/1000,(op1)%1000,op2); } return -1; @@ -359,13 +372,22 @@ static const struct attribute_group registers_group = { // ----- Metrics ----- static struct kobj_attribute metric_power_attribute = __ATTR(power, 0664, attr_metric_show, NULL); +static struct kobj_attribute metric_voltage_attribute = + __ATTR(voltage, 0664, attr_metric_show, NULL); +static struct kobj_attribute metric_current_attribute = { + .attr = {.name = "current", // current is a defined macro so need to do it manually + .mode = VERIFY_OCTAL_PERMISSIONS(0444) }, + .show = attr_metric_show, + .store = NULL +}; static struct attribute *metrics_attrs[] = { &metric_power_attribute.attr, + &metric_voltage_attribute.attr, + &metric_current_attribute.attr, NULL, }; static const struct attribute_group metrics_group = { - .attrs = metrics_attrs, - .name = "metrics" + .attrs = metrics_attrs }; // ----- Fields ----- static struct kobj_attribute reset_field_attribute = @@ -427,6 +449,7 @@ static struct attribute *fields_attrs[] = { }; static const struct attribute_group fields_group = { .attrs = fields_attrs, + .name = "fields" };