summaryrefslogtreecommitdiff
path: root/ina260.c
diff options
context:
space:
mode:
Diffstat (limited to 'ina260.c')
-rw-r--r--ina260.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/ina260.c b/ina260.c
index 97a7821..6bdaa6d 100644
--- a/ina260.c
+++ b/ina260.c
@@ -16,6 +16,7 @@
#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
@@ -150,6 +151,22 @@ static ssize_t attr_store(struct kobject *_kobj,
return count;
}
+static ssize_t attr_metric_show(struct kobject *_kobj,
+ struct kobj_attribute *attr,
+ char *buf)
+{
+ int rvalue;
+ 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;
+}
+
static ssize_t attr_field_show(struct kobject *_kobj,
struct kobj_attribute *attr,
char *buf)
@@ -339,6 +356,17 @@ static const struct attribute_group registers_group = {
.attrs = registers_attrs,
.name = "registers"
};
+// ----- Metrics -----
+static struct kobj_attribute metric_power_attribute =
+ __ATTR(power, 0664, attr_metric_show, NULL);
+static struct attribute *metrics_attrs[] = {
+ &metric_power_attribute.attr,
+ NULL,
+};
+static const struct attribute_group metrics_group = {
+ .attrs = metrics_attrs,
+ .name = "metrics"
+};
// ----- Fields -----
static struct kobj_attribute reset_field_attribute =
__ATTR(reset, 0664, attr_field_show, attr_field_store);
@@ -446,6 +474,7 @@ static int ina260_probe_new(struct i2c_client *client){
i2c_set_clientdata(client,p);
// Setup sysfs groups:
if(sysfs_create_group(&p->kobj,&registers_group)||
+ sysfs_create_group(&p->kobj,&metrics_group)||
sysfs_create_group(&p->kobj,&fields_group)){
kobject_put(&p->kobj);
kfree(p);