mirror of
https://gitlab.com/manzerbredes/ina260-sysfs-driver.git
synced 2025-04-19 04:09:45 +00:00
Change sysfs architecture and add power,current and voltage metrics
This commit is contained in:
parent
451a80d636
commit
d43c92e2a1
1 changed files with 28 additions and 5 deletions
33
ina260.c
33
ina260.c
|
@ -16,7 +16,6 @@
|
||||||
#define INA260_REG_MANUFACTURER 0xFE
|
#define INA260_REG_MANUFACTURER 0xFE
|
||||||
#define INA260_REG_DIE 0xFF
|
#define INA260_REG_DIE 0xFF
|
||||||
|
|
||||||
#define INA260_LSB_POWER 10 // 10mW
|
|
||||||
#define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0)
|
#define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0)
|
||||||
|
|
||||||
// ina260 average modes list
|
// ina260 average modes list
|
||||||
|
@ -156,12 +155,26 @@ static ssize_t attr_metric_show(struct kobject *_kobj,
|
||||||
char *buf)
|
char *buf)
|
||||||
{
|
{
|
||||||
int rvalue;
|
int rvalue;
|
||||||
|
int op1, op2;
|
||||||
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
|
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
|
||||||
|
|
||||||
if(INA260_IS_ATTR(power)){
|
if(INA260_IS_ATTR(power)){
|
||||||
if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue))
|
if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue))
|
||||||
return -1;
|
return -1;
|
||||||
return sprintf(buf, "%d.%d\n", INA260_LSB_POWER*rvalue/1000,(INA260_LSB_POWER*rvalue) % 1000);
|
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;
|
return -1;
|
||||||
|
@ -359,13 +372,22 @@ static const struct attribute_group registers_group = {
|
||||||
// ----- Metrics -----
|
// ----- Metrics -----
|
||||||
static struct kobj_attribute metric_power_attribute =
|
static struct kobj_attribute metric_power_attribute =
|
||||||
__ATTR(power, 0664, attr_metric_show, NULL);
|
__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[] = {
|
static struct attribute *metrics_attrs[] = {
|
||||||
&metric_power_attribute.attr,
|
&metric_power_attribute.attr,
|
||||||
|
&metric_voltage_attribute.attr,
|
||||||
|
&metric_current_attribute.attr,
|
||||||
NULL,
|
NULL,
|
||||||
};
|
};
|
||||||
static const struct attribute_group metrics_group = {
|
static const struct attribute_group metrics_group = {
|
||||||
.attrs = metrics_attrs,
|
.attrs = metrics_attrs
|
||||||
.name = "metrics"
|
|
||||||
};
|
};
|
||||||
// ----- Fields -----
|
// ----- Fields -----
|
||||||
static struct kobj_attribute reset_field_attribute =
|
static struct kobj_attribute reset_field_attribute =
|
||||||
|
@ -427,6 +449,7 @@ static struct attribute *fields_attrs[] = {
|
||||||
};
|
};
|
||||||
static const struct attribute_group fields_group = {
|
static const struct attribute_group fields_group = {
|
||||||
.attrs = fields_attrs,
|
.attrs = fields_attrs,
|
||||||
|
.name = "fields"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue