Minor changes

This commit is contained in:
Loic Guegan 2023-08-14 10:39:36 +02:00
parent 5277de9f99
commit 15858006f9
6 changed files with 684 additions and 790 deletions

3
.gitignore vendored
View file

@ -4,5 +4,6 @@
# Include the following
!Makefile
!.gitignore
!*2.c
!ina260.c
!ina260_full.c
!README.md

View file

@ -1,39 +1,30 @@
# Linux Makefile Location:
#LML="/usr/src/linux-headers-$(shell uname -r)/"
LML="/lib/modules/$(shell uname -r)/build/" # Change if required on your system
obj-m += ina260.o ina260_full.o
obj-m += ina2602.o
TEST_DEV_ADDR=0x41
all: hwmon
ina260: ina260.c
make -C $(LML) M=$(PWD) modules
hwmon: ina2602.c
all: ina260.c ina260_full.c
make -C $(LML) M=$(PWD) modules
run: ina260.c
-echo 0x41 > /sys/bus/i2c/devices/i2c-2/delete_device
-rmmod ina260
- echo $(TEST_DEV_ADDR) > /sys/bus/i2c/devices/i2c-2/delete_device
- rmmod ina260
make clean
make
insmod ina260.ko
echo ina260 0x41 > /sys/bus/i2c/devices/i2c-2/new_device
insmod ina260
echo ina260 $(TEST_DEV_ADDR) > /sys/bus/i2c/devices/i2c-2/new_device
run2: ina2602.c
-echo 0x41 > /sys/bus/i2c/devices/i2c-2/delete_device
-rmmod ina2602
run-full: ina2602.c
- echo $(TEST_DEV_ADDR) > /sys/bus/i2c/devices/i2c-2/delete_device
- rmmod ina260_full
make clean
make
insmod ina2602.ko
echo ina260 0x41 > /sys/bus/i2c/devices/i2c-2/new_device
read: read.c
gcc $^ -o read
insmod ina260_full
echo ina260 $(TEST_DEV_ADDR) > /sys/bus/i2c/devices/i2c-2/new_device
clean:
rm -f ina260*.o ina260.ko ina260.mod* Module.symvers modules.order .ina260* .Module* .modules*
rm -f inahwmon*.o inahwmon.ko inahwmon.mod* Module.symvers modules.order .inahwmon* .Module* .modules*
rm -f .ina260* *.ko *.mod.* *.o *.symvers *.order
.PHONY: clean run

490
ina260.c
View file

@ -5,6 +5,8 @@
#include "linux/slab.h"
#include "linux/kernel.h"
#include <linux/sysfs.h>
#include <linux/hwmon.h>
#include <linux/regmap.h>
// ina260 chip registers
#define INA260_REG_CONFIGURATION 0x00
@ -16,7 +18,41 @@
#define INA260_REG_MANUFACTURER 0xFE
#define INA260_REG_DIE 0xFF
#define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0)
#undef current
static struct regmap_config ina260_regmap_config = {
.reg_bits = 8,
.val_bits = 16,
};
#define INA260_REG_SHOW(_attr,_reg) \
static ssize_t _attr##_show(struct device *dev, struct device_attribute *attr, char *buf) \
{ \
int rvalue; struct client_data *cdata=dev_get_drvdata(dev); \
if(ina260_read_register(cdata,(_reg),&rvalue)) \
return -1; \
return sprintf(buf, "%x\n", rvalue); \
}
#define INA260_REG_STORE(_attr,_reg) \
static ssize_t _attr##_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
{ \
int uvalue; struct client_data *cdata=dev_get_drvdata(dev); \
if(kstrtoint(buf, 0,&uvalue)) \
return -EINVAL; \
if(ina260_write_register(cdata, (_reg), uvalue)) \
return -1; \
return count; \
}
#define HWMON_CHANNEL_INFO(stype, ...) \
(&(struct hwmon_channel_info) { \
.type = hwmon_##stype, \
.config = (u32 []) { \
__VA_ARGS__, 0 \
} \
})
// ina260 average modes list
static int ina260_avgs[8]={
@ -36,14 +72,12 @@ static char ina260_ishcts_vbusct[8][9]={
"140 µs", "204 µs","332 µs", "588 µs","1.1 ms", "2.116 ms","4.156 ms","8.244 ms"
};
// Driver root
static struct kobject* ina260_kobj;
// Data attached to i2c clients
struct client_data {
struct kobject kobj;
struct i2c_client *client;
unsigned char reg; // Slave selected register
struct regmap *regmap;
};
static const struct i2c_device_id ina260_ids[] = {
@ -62,7 +96,7 @@ MODULE_DEVICE_TABLE(i2c,ina260_ids);
*/
static int ina260_read_register(struct client_data* cdata, unsigned char reg, int *value){
unsigned char bytes[2];
if(cdata->reg == reg){
/*if(cdata->reg == reg){
if(i2c_master_recv(cdata->client,bytes,2)<0)
return 1;
} else {
@ -71,9 +105,10 @@ static int ina260_read_register(struct client_data* cdata, unsigned char reg, in
cdata->reg = reg;
if(i2c_master_recv(cdata->client,bytes,2)<0)
return 1;
}
*value=(bytes[0]<<8) | bytes[1];
return 0;
}*/
return regmap_read(cdata->regmap, reg, value);
// *value=(bytes[0]<<8) | bytes[1];
//return 0;
}
/**
@ -94,369 +129,102 @@ static int ina260_write_register(struct client_data* cdata, unsigned char reg, i
return 0;
}
static ssize_t attr_show(struct kobject *_kobj,
struct kobj_attribute *attr,
char *buf)
static int ina260_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
int rvalue;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
unsigned char reg=INA260_REG_POWER;
if(!INA260_IS_ATTR(power)){
if (INA260_IS_ATTR(current)){
reg=INA260_REG_CURRENT;
} else if(INA260_IS_ATTR(bus_voltage)) {
reg=INA260_REG_VOLTAGE;
} else if(INA260_IS_ATTR(mask_enable)) {
reg=INA260_REG_MASKENABLE;
} else if(INA260_IS_ATTR(alert_limit)) {
reg=INA260_REG_ALERTLIMIT;
} else if(INA260_IS_ATTR(configuration)) {
reg=INA260_REG_CONFIGURATION;
} else if(INA260_IS_ATTR(manufacturer_id)) {
reg=INA260_REG_MANUFACTURER;
} else {
reg=INA260_REG_DIE;
}
}
if(ina260_read_register(cdata,reg,&rvalue))
return -1;
return sprintf(buf, "%x\n", rvalue);
}
static ssize_t attr_store(struct kobject *_kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
int uvalue;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
unsigned char reg=INA260_REG_CONFIGURATION;
if(kstrtoint(buf, 0,&uvalue))
return -EINVAL;
if (INA260_IS_ATTR(mask_enable)){
reg=INA260_REG_MASKENABLE;
} else if (INA260_IS_ATTR(alert_limit)){
reg=INA260_REG_ALERTLIMIT;
}
if(ina260_write_register(cdata, reg, uvalue))
return -1;
return count;
}
static ssize_t attr_metric_show(struct kobject *_kobj,
struct kobj_attribute *attr,
char *buf)
{
int rvalue;
int op1, op2;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
if(INA260_IS_ATTR(power)){
struct client_data *cdata=dev_get_drvdata(dev);
if(type == hwmon_power){
if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue))
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)){
*val=10*rvalue*1000;
} else if (type == hwmon_curr){
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);
*val=((rvalue*25/100) + rvalue)*100+(rvalue*25%100);
*val/=100;
} else if (type == hwmon_in){
if(ina260_read_register(cdata, INA260_REG_VOLTAGE,&rvalue))
return -1;
*val=((rvalue*25/100) + rvalue)*100+(rvalue*25%100);
*val/=100;
}
return -1;
return 0;
}
static ssize_t attr_field_show(struct kobject *_kobj,
struct kobj_attribute *attr,
char *buf)
static int ina260_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
{
int rvalue;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
if(INA260_IS_ATTR(avg)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%d\n", ina260_avgs[(rvalue>>9)&0x7]);
} else if (INA260_IS_ATTR(mode)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%s\n", ina260_modes[rvalue&0x7]);
} else if (INA260_IS_ATTR(ishct)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%s\n", ina260_ishcts_vbusct[(rvalue>>3)&0x7]);
} else if (INA260_IS_ATTR(vbusct)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%s\n", ina260_ishcts_vbusct[(rvalue>>6)&0x7]);
} else if (INA260_IS_ATTR(aff)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>4)&0x1);
} else if (INA260_IS_ATTR(cvrf)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>3)&0x1);
} else if (INA260_IS_ATTR(ocl)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>15)&0x1);
}else if (INA260_IS_ATTR(ucl)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>14)&0x1);
}else if (INA260_IS_ATTR(bol)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>13)&0x1);
}else if (INA260_IS_ATTR(bul)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>12)&0x1);
}else if (INA260_IS_ATTR(pol)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>11)&0x1);
}else if (INA260_IS_ATTR(cnvr)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>10)&0x1);
}else if (INA260_IS_ATTR(ovf)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>2)&0x1);
}else if (INA260_IS_ATTR(apol)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>1)&0x1);
}else if (INA260_IS_ATTR(len)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", rvalue&0x1);
}else if (INA260_IS_ATTR(did)){
if(ina260_read_register(cdata, INA260_REG_DIE,&rvalue))
return -1;
return sprintf(buf, "0x%x\n", (rvalue>>4));
}else if (INA260_IS_ATTR(rid)){
if(ina260_read_register(cdata, INA260_REG_DIE,&rvalue))
return -1;
return sprintf(buf, "0x%x\n", rvalue&0x7);
}else if (INA260_IS_ATTR(reset)){
return sprintf(buf, "0\n");
}
return 0;
}
static int ina260_write_field3(struct client_data *cdata, unsigned char reg, unsigned char n, int uvalue){
int rvalue;
if(!(uvalue>=0 && uvalue <8))
return -EINVAL;
// Fetch register value:
if(ina260_read_register(cdata,reg,&rvalue))
return 1;
// Write bits:
rvalue &= ~(0x7 << n); // clear bits
rvalue |= (uvalue << n);
// Write register value:
if(ina260_write_register(cdata,reg,rvalue))
return 1;
return 0;
}
INA260_REG_SHOW(configuration,INA260_REG_CONFIGURATION)
INA260_REG_SHOW(current,INA260_REG_CURRENT)
INA260_REG_SHOW(bus_voltage,INA260_REG_VOLTAGE)
INA260_REG_SHOW(power,INA260_REG_POWER)
INA260_REG_SHOW(mask_enable,INA260_REG_MASKENABLE)
INA260_REG_SHOW(alert_limit,INA260_REG_ALERTLIMIT)
INA260_REG_SHOW(manufacturer_id,INA260_REG_MANUFACTURER)
INA260_REG_SHOW(die_id,INA260_REG_DIE)
static int ina260_write_field1(struct client_data *cdata, unsigned char reg, unsigned char n, int uvalue){
int rvalue;
if(!(uvalue==0 || uvalue == 1))
return -EINVAL;
// Fetch register value:
if(ina260_read_register(cdata,reg,&rvalue))
return 1;
// Set bit:
rvalue &= ~(1<< n); // clear bit
rvalue |= (uvalue << n);
// Write register value
if(ina260_write_register(cdata,reg,rvalue))
return 1;
return 0;
}
static ssize_t attr_field_store(struct kobject *_kobj,
struct kobj_attribute *attr,
const char __user *buf, size_t count)
{
int uvalue, ret=0;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
// Extract user supplied value:
if(kstrtoint(buf, 0,&uvalue))
return -EINVAL;
// Store:
if(INA260_IS_ATTR(reset) && uvalue!=0){
ret=ina260_write_register(cdata, INA260_REG_CONFIGURATION, 0xFFFF); // Much quicker this way
} else if(INA260_IS_ATTR(avg)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,9,uvalue);
} else if(INA260_IS_ATTR(mode)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,0,uvalue);
} else if(INA260_IS_ATTR(ishct)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,3,uvalue);
} else if(INA260_IS_ATTR(vbusct)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,6,uvalue);
} else if(INA260_IS_ATTR(ocl)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,15,uvalue);
} else if(INA260_IS_ATTR(ucl)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,14,uvalue);
} else if(INA260_IS_ATTR(bol)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,13,uvalue);
} else if(INA260_IS_ATTR(bul)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,12,uvalue);
} else if(INA260_IS_ATTR(pol)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,11,uvalue);
} else if(INA260_IS_ATTR(cnvr)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,10,uvalue);
} else if(INA260_IS_ATTR(apol)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,1,uvalue);
} else if(INA260_IS_ATTR(len)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,0,uvalue);
}
return ret ? ret: count;
}
INA260_REG_STORE(configuration,INA260_REG_CONFIGURATION)
INA260_REG_STORE(current,INA260_REG_CURRENT)
INA260_REG_STORE(bus_voltage,INA260_REG_VOLTAGE)
INA260_REG_STORE(power,INA260_REG_POWER)
INA260_REG_STORE(mask_enable,INA260_REG_MASKENABLE)
INA260_REG_STORE(alert_limit,INA260_REG_ALERTLIMIT)
// ----- Registers -----
static struct kobj_attribute configuration_attribute =
__ATTR(configuration, 0664, attr_show, attr_store);
static struct kobj_attribute current_attribute = {
.attr = {.name = "current", // current is a defined macro so need to do it manually
.mode = VERIFY_OCTAL_PERMISSIONS(0444) },
.show = attr_show,
.store = attr_store,
static umode_t ina260_hwmon_is_visible(const void *drvdata,
enum hwmon_sensor_types type,
u32 attr, int channel){
return 0444;
}
static const struct hwmon_channel_info * ina260_hwmon_info[] = {
HWMON_CHANNEL_INFO(in,HWMON_I_INPUT),
HWMON_CHANNEL_INFO(power,HWMON_P_INPUT),
HWMON_CHANNEL_INFO(curr,HWMON_C_INPUT),
NULL
};
static struct kobj_attribute bus_voltage_attribute =
__ATTR(bus_voltage, 0444, attr_show, attr_store);
static struct kobj_attribute power_attribute =
__ATTR(power, 0444, attr_show, attr_store);
static struct kobj_attribute mask_enable_attribute =
__ATTR(mask_enable, 0664, attr_show, attr_store);
static struct kobj_attribute alert_limit_attribute =
__ATTR(alert_limit, 0664, attr_show, attr_store);
static struct kobj_attribute manufacturer_id_attribute =
__ATTR(manufacturer_id, 0664, attr_show, attr_store);
static struct kobj_attribute die_id_attribute=
__ATTR(die_id, 0664, attr_show, attr_store);
static const struct hwmon_ops ina260_hwmon_ops = {
.is_visible = ina260_hwmon_is_visible,
.read = ina260_hwmon_read,
.write = ina260_hwmon_write,
};
static const struct hwmon_chip_info ina260_chip_info = {
.ops = &ina260_hwmon_ops,
.info = ina260_hwmon_info,
};
// ----- Registers -----
static DEVICE_ATTR_RW(configuration);
static DEVICE_ATTR_RW(current);
static DEVICE_ATTR_RW(bus_voltage);
static DEVICE_ATTR_RW(power);
static DEVICE_ATTR_RW(mask_enable);
static DEVICE_ATTR_RW(alert_limit);
static DEVICE_ATTR_RO(manufacturer_id);
static DEVICE_ATTR_RO(die_id);
static struct attribute *registers_attrs[] = {
&configuration_attribute.attr,
&current_attribute.attr,
&bus_voltage_attribute.attr,
&power_attribute.attr,
&mask_enable_attribute.attr,
&alert_limit_attribute.attr,
&manufacturer_id_attribute.attr,
&die_id_attribute.attr,
&dev_attr_configuration.attr,
&dev_attr_current.attr,
&dev_attr_bus_voltage.attr,
&dev_attr_power.attr,
&dev_attr_mask_enable.attr,
&dev_attr_alert_limit.attr,
&dev_attr_manufacturer_id.attr,
&dev_attr_die_id.attr,
NULL,
};
static const struct attribute_group registers_group = {
.attrs = registers_attrs,
.name = "registers"
};
// ----- Metrics -----
static struct kobj_attribute metric_power_attribute =
__ATTR(power, 0444, attr_metric_show, NULL);
static struct kobj_attribute metric_voltage_attribute =
__ATTR(voltage, 0444, 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
};
// ----- Fields -----
static struct kobj_attribute reset_field_attribute =
__ATTR(reset, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute avg_field_attribute =
__ATTR(avg, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute mode_field_attribute =
__ATTR(mode, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ishct_field_attribute =
__ATTR(ishct, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute vbusct_field_attribute =
__ATTR(vbusct, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute aff_field_attribute =
__ATTR(aff, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute cvrf_field_attribute =
__ATTR(cvrf, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute ocl_field_attribute =
__ATTR(ocl, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ucl_field_attribute =
__ATTR(ucl, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute bol_field_attribute =
__ATTR(bol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute bul_field_attribute =
__ATTR(bul, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute pol_field_attribute =
__ATTR(pol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute cnvr_field_attribute =
__ATTR(cnvr, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ovf_field_attribute =
__ATTR(ovf, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute apol_field_attribute =
__ATTR(apol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute len_field_attribute =
__ATTR(len, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute did_field_attribute =
__ATTR(did, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute rid_field_attribute =
__ATTR(rid, 0444, attr_field_show, attr_field_store);
static struct attribute *fields_attrs[] = {
&reset_field_attribute.attr,
&avg_field_attribute.attr,
&mode_field_attribute.attr,
&ishct_field_attribute.attr,
&vbusct_field_attribute.attr,
&aff_field_attribute.attr,
&cvrf_field_attribute.attr,
&ocl_field_attribute.attr,
&ucl_field_attribute.attr,
&bol_field_attribute.attr,
&bul_field_attribute.attr,
&pol_field_attribute.attr,
&cnvr_field_attribute.attr,
&ovf_field_attribute.attr,
&apol_field_attribute.attr,
&len_field_attribute.attr,
&did_field_attribute.attr,
&rid_field_attribute.attr,
NULL,
};
static const struct attribute_group fields_group = {
.attrs = fields_attrs,
.name = "fields"
};
static struct kobj_type ina260_ktype = {
.sysfs_ops = &kobj_sysfs_ops,
};
/**
* @brief Compare on ina260 register to a supplied value
*
@ -474,8 +242,15 @@ static int ina260_probe_register(struct i2c_client *client, unsigned char reg, i
return ((bytes[0]<<8) | bytes[1])!=value;
}
const struct attribute_group *extra_groups[] = {
&registers_group,
NULL
};
static int ina260_probe_new(struct i2c_client *client){
struct client_data *p;
struct device *hwmon_dev;
// Attempt device discovery:
if(ina260_probe_register(client,INA260_REG_MANUFACTURER,0x5449) ||
ina260_probe_register(client,INA260_REG_DIE,0x2270)){
@ -485,36 +260,27 @@ static int ina260_probe_new(struct i2c_client *client){
// Initialize client data:
printk("ina260 detected bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
p=kzalloc(sizeof(struct client_data),GFP_KERNEL);
kobject_init(&p->kobj,&ina260_ktype);
// Setup sysfs root kobject:
if(kobject_add(&p->kobj,ina260_kobj,client->dev.kobj.name)){
kobject_put(&p->kobj);
kfree(p);
return 1;
}
p->client=client;
p->reg=INA260_REG_DIE; // Maintain cache coherence
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);
return 1;
}
p->regmap = devm_regmap_init_i2c(client, &ina260_regmap_config);
hwmon_dev=hwmon_device_register_with_info(&client->dev,client->name,p,
&ina260_chip_info,extra_groups);
if (IS_ERR(hwmon_dev))
return PTR_ERR(hwmon_dev);
return 0;
}
static int ina260_remove(struct i2c_client *client){
struct client_data *p=i2c_get_clientdata(client);
kobject_put(&p->kobj);
kfree(p);
hwmon_device_unregister(&client->dev);
printk("ina260 removed bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
return 0;
}
static struct i2c_driver ina260_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "ina260"
},
@ -524,14 +290,12 @@ static struct i2c_driver ina260_driver = {
};
static int __init ina260_init(void){
ina260_kobj = kobject_create_and_add("ina260",kernel_kobj);
i2c_add_driver(&ina260_driver);
return 0;
}
static void __exit ina260_exit(void){
i2c_del_driver(&ina260_driver);
kobject_put(ina260_kobj);
}
module_init(ina260_init);

297
ina2602.c
View file

@ -1,297 +0,0 @@
#include "linux/module.h"
#include "linux/uaccess.h"
#include "linux/i2c.h"
#include "linux/kobject.h"
#include "linux/slab.h"
#include "linux/kernel.h"
#include <linux/sysfs.h>
#include <linux/hwmon.h>
// ina260 chip registers
#define INA260_REG_CONFIGURATION 0x00
#define INA260_REG_CURRENT 0x01
#define INA260_REG_VOLTAGE 0x02
#define INA260_REG_POWER 0x03
#define INA260_REG_MASKENABLE 0x06
#define INA260_REG_ALERTLIMIT 0x07
#define INA260_REG_MANUFACTURER 0xFE
#define INA260_REG_DIE 0xFF
#undef current
#define INA260_REG_SHOW(_attr,_reg) \
static ssize_t _attr##_show(struct device *dev, struct device_attribute *attr, char *buf) \
{ \
int rvalue; struct client_data *cdata=dev_get_drvdata(dev); \
if(ina260_read_register(cdata,(_reg),&rvalue)) \
return -1; \
return sprintf(buf, "%x\n", rvalue); \
}
#define INA260_REG_STORE(_attr,_reg) \
static ssize_t _attr##_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
{ \
int uvalue; struct client_data *cdata=dev_get_drvdata(dev); \
if(kstrtoint(buf, 0,&uvalue)) \
return -EINVAL; \
if(ina260_write_register(cdata, (_reg), uvalue)) \
return -1; \
return count; \
}
#define HWMON_CHANNEL_INFO(stype, ...) \
(&(struct hwmon_channel_info) { \
.type = hwmon_##stype, \
.config = (u32 []) { \
__VA_ARGS__, 0 \
} \
})
// ina260 average modes list
static int ina260_avgs[8]={
1,4,16,64,128,256,512,1024
};
// ina260 operating modes list
static char ina260_modes[8][42]={
"Power-Down (or Shutdown)","Shunt Current, Triggered",
"Bus Voltage, Triggered","Shunt Current and Bus Voltage, Triggered",
"Power-Down (or Shutdown)","Shunt Current, Continuous","Bus Voltage, Continuous",
"Shunt Current and Bus Voltage, Continuous"
};
// ina260 conversion times list
static char ina260_ishcts_vbusct[8][9]={
"140 µs", "204 µs","332 µs", "588 µs","1.1 ms", "2.116 ms","4.156 ms","8.244 ms"
};
// Data attached to i2c clients
struct client_data {
struct i2c_client *client;
unsigned char reg; // Slave selected register
};
static const struct i2c_device_id ina260_ids[] = {
{ "ina260", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c,ina260_ids);
/**
* @brief Read from ina260 registers
*
* @param cdata client data to use to communicate
* @param reg register to read
* @param value register content output
* @return int 0 on success, 1 on communication errors
*/
static int ina260_read_register(struct client_data* cdata, unsigned char reg, int *value){
unsigned char bytes[2];
if(cdata->reg == reg){
if(i2c_master_recv(cdata->client,bytes,2)<0)
return 1;
} else {
if(i2c_master_send(cdata->client,&reg,1)<0)
return 1;
cdata->reg = reg;
if(i2c_master_recv(cdata->client,bytes,2)<0)
return 1;
}
*value=(bytes[0]<<8) | bytes[1];
return 0;
}
/**
* @brief Write to ina260 registers
*
* @param cdata client data to use to communicate
* @param reg register to write to
* @param value value to write in @a reg
* @return int 0 on success, 1 on communication errors
*/
static int ina260_write_register(struct client_data* cdata, unsigned char reg, int value){
unsigned char data[3];
data[0]=reg;
data[1]=(value>>8) & 0xFF; // MSB
data[2]=value & 0xFF; // LSB
if(i2c_master_send(cdata->client,data,3)<0)
return 1;
return 0;
}
static int ina260_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
int rvalue;
struct client_data *cdata=dev_get_drvdata(dev);
if(type == hwmon_power){
if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue))
return -1;
*val=10*rvalue*1000;
} else if (type == hwmon_curr){
if(ina260_read_register(cdata, INA260_REG_CURRENT,&rvalue))
return -1;
*val=((rvalue*25/100) + rvalue)*100+(rvalue*25%100);
*val/=100;
} else if (type == hwmon_in){
if(ina260_read_register(cdata, INA260_REG_VOLTAGE,&rvalue))
return -1;
*val=((rvalue*25/100) + rvalue)*100+(rvalue*25%100);
*val/=100;
}
return 0;
}
static int ina260_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
{
return 0;
}
INA260_REG_SHOW(configuration,INA260_REG_CONFIGURATION)
INA260_REG_SHOW(current,INA260_REG_CURRENT)
INA260_REG_SHOW(bus_voltage,INA260_REG_VOLTAGE)
INA260_REG_SHOW(power,INA260_REG_POWER)
INA260_REG_SHOW(mask_enable,INA260_REG_MASKENABLE)
INA260_REG_SHOW(alert_limit,INA260_REG_ALERTLIMIT)
INA260_REG_SHOW(manufacturer_id,INA260_REG_MANUFACTURER)
INA260_REG_SHOW(die_id,INA260_REG_DIE)
INA260_REG_STORE(configuration,INA260_REG_CONFIGURATION)
INA260_REG_STORE(current,INA260_REG_CURRENT)
INA260_REG_STORE(bus_voltage,INA260_REG_VOLTAGE)
INA260_REG_STORE(power,INA260_REG_POWER)
INA260_REG_STORE(mask_enable,INA260_REG_MASKENABLE)
INA260_REG_STORE(alert_limit,INA260_REG_ALERTLIMIT)
static umode_t ina260_hwmon_is_visible(const void *drvdata,
enum hwmon_sensor_types type,
u32 attr, int channel){
return 0444;
}
static const struct hwmon_channel_info * ina260_hwmon_info[] = {
HWMON_CHANNEL_INFO(in,HWMON_I_INPUT),
HWMON_CHANNEL_INFO(power,HWMON_P_INPUT),
HWMON_CHANNEL_INFO(curr,HWMON_C_INPUT),
NULL
};
static const struct hwmon_ops ina260_hwmon_ops = {
.is_visible = ina260_hwmon_is_visible,
.read = ina260_hwmon_read,
.write = ina260_hwmon_write,
};
static const struct hwmon_chip_info ina260_chip_info = {
.ops = &ina260_hwmon_ops,
.info = ina260_hwmon_info,
};
// ----- Registers -----
static DEVICE_ATTR_RW(configuration);
static DEVICE_ATTR_RW(current);
static DEVICE_ATTR_RW(bus_voltage);
static DEVICE_ATTR_RW(power);
static DEVICE_ATTR_RW(mask_enable);
static DEVICE_ATTR_RW(alert_limit);
static DEVICE_ATTR_RO(manufacturer_id);
static DEVICE_ATTR_RO(die_id);
static struct attribute *registers_attrs[] = {
&dev_attr_configuration.attr,
&dev_attr_current.attr,
&dev_attr_bus_voltage.attr,
&dev_attr_power.attr,
&dev_attr_mask_enable.attr,
&dev_attr_alert_limit.attr,
&dev_attr_manufacturer_id.attr,
&dev_attr_die_id.attr,
NULL,
};
static const struct attribute_group registers_group = {
.attrs = registers_attrs,
.name = "registers"
};
/**
* @brief Compare on ina260 register to a supplied value
*
* @param client i2c client to use for communications
* @param reg register to use for comparison
* @param value register expected value
* @return int 0 if register contains value and 1 otherwise
*/
static int ina260_probe_register(struct i2c_client *client, unsigned char reg, int value){
unsigned char bytes[2];
if(i2c_master_send(client,&reg,1)<0)
return 1;
if(i2c_master_recv(client,bytes,2)<0)
return 1;
return ((bytes[0]<<8) | bytes[1])!=value;
}
const struct attribute_group *extra_groups[] = {
&registers_group,
NULL
};
static int ina260_probe_new(struct i2c_client *client){
struct client_data *p;
struct device *hwmon_dev;
// Attempt device discovery:
if(ina260_probe_register(client,INA260_REG_MANUFACTURER,0x5449) ||
ina260_probe_register(client,INA260_REG_DIE,0x2270)){
printk("ina260 probe fails bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
return 1;
}
// Initialize client data:
printk("ina260 detected bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
p=kzalloc(sizeof(struct client_data),GFP_KERNEL);
p->client=client;
p->reg=INA260_REG_DIE; // Maintain cache coherence
hwmon_dev=hwmon_device_register_with_info(&client->dev,client->name,p,
&ina260_chip_info,extra_groups);
if (IS_ERR(hwmon_dev))
return PTR_ERR(hwmon_dev);
return 0;
}
static int ina260_remove(struct i2c_client *client){
struct client_data *p=i2c_get_clientdata(client);
kfree(p);
hwmon_device_unregister(&client->dev);
printk("ina260 removed bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
return 0;
}
static struct i2c_driver ina260_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "ina260"
},
.probe_new = ina260_probe_new,
.remove = ina260_remove,
.id_table = ina260_ids
};
static int __init ina260_init(void){
i2c_add_driver(&ina260_driver);
return 0;
}
static void __exit ina260_exit(void){
i2c_del_driver(&ina260_driver);
}
module_init(ina260_init);
module_exit(ina260_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Loïc Guegan");
MODULE_DESCRIPTION("INA260 Texas Instruments");
MODULE_VERSION("1.0");

542
ina260_full.c Executable file
View file

@ -0,0 +1,542 @@
#include "linux/module.h"
#include "linux/uaccess.h"
#include "linux/i2c.h"
#include "linux/kobject.h"
#include "linux/slab.h"
#include "linux/kernel.h"
#include <linux/sysfs.h>
// ina260 chip registers
#define INA260_REG_CONFIGURATION 0x00
#define INA260_REG_CURRENT 0x01
#define INA260_REG_VOLTAGE 0x02
#define INA260_REG_POWER 0x03
#define INA260_REG_MASKENABLE 0x06
#define INA260_REG_ALERTLIMIT 0x07
#define INA260_REG_MANUFACTURER 0xFE
#define INA260_REG_DIE 0xFF
#define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0)
// ina260 average modes list
static int ina260_avgs[8]={
1,4,16,64,128,256,512,1024
};
// ina260 operating modes list
static char ina260_modes[8][42]={
"Power-Down (or Shutdown)","Shunt Current, Triggered",
"Bus Voltage, Triggered","Shunt Current and Bus Voltage, Triggered",
"Power-Down (or Shutdown)","Shunt Current, Continuous","Bus Voltage, Continuous",
"Shunt Current and Bus Voltage, Continuous"
};
// ina260 conversion times list
static char ina260_ishcts_vbusct[8][9]={
"140 µs", "204 µs","332 µs", "588 µs","1.1 ms", "2.116 ms","4.156 ms","8.244 ms"
};
// Driver root
static struct kobject* ina260_kobj;
// Data attached to i2c clients
struct client_data {
struct kobject kobj;
struct i2c_client *client;
unsigned char reg; // Slave selected register
};
static const struct i2c_device_id ina260_ids[] = {
{ "ina260", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c,ina260_ids);
/**
* @brief Read from ina260 registers
*
* @param cdata client data to use to communicate
* @param reg register to read
* @param value register content output
* @return int 0 on success, 1 on communication errors
*/
static int ina260_read_register(struct client_data* cdata, unsigned char reg, int *value){
unsigned char bytes[2];
if(cdata->reg == reg){
if(i2c_master_recv(cdata->client,bytes,2)<0)
return 1;
} else {
if(i2c_master_send(cdata->client,&reg,1)<0)
return 1;
cdata->reg = reg;
if(i2c_master_recv(cdata->client,bytes,2)<0)
return 1;
}
*value=(bytes[0]<<8) | bytes[1];
return 0;
}
/**
* @brief Write to ina260 registers
*
* @param cdata client data to use to communicate
* @param reg register to write to
* @param value value to write in @a reg
* @return int 0 on success, 1 on communication errors
*/
static int ina260_write_register(struct client_data* cdata, unsigned char reg, int value){
unsigned char data[3];
data[0]=reg;
data[1]=(value>>8) & 0xFF; // MSB
data[2]=value & 0xFF; // LSB
if(i2c_master_send(cdata->client,data,3)<0)
return 1;
return 0;
}
static ssize_t attr_show(struct kobject *_kobj,
struct kobj_attribute *attr,
char *buf)
{
int rvalue;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
unsigned char reg=INA260_REG_POWER;
if(!INA260_IS_ATTR(power)){
if (INA260_IS_ATTR(current)){
reg=INA260_REG_CURRENT;
} else if(INA260_IS_ATTR(bus_voltage)) {
reg=INA260_REG_VOLTAGE;
} else if(INA260_IS_ATTR(mask_enable)) {
reg=INA260_REG_MASKENABLE;
} else if(INA260_IS_ATTR(alert_limit)) {
reg=INA260_REG_ALERTLIMIT;
} else if(INA260_IS_ATTR(configuration)) {
reg=INA260_REG_CONFIGURATION;
} else if(INA260_IS_ATTR(manufacturer_id)) {
reg=INA260_REG_MANUFACTURER;
} else {
reg=INA260_REG_DIE;
}
}
if(ina260_read_register(cdata,reg,&rvalue))
return -1;
return sprintf(buf, "%x\n", rvalue);
}
static ssize_t attr_store(struct kobject *_kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
int uvalue;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
unsigned char reg=INA260_REG_CONFIGURATION;
if(kstrtoint(buf, 0,&uvalue))
return -EINVAL;
if (INA260_IS_ATTR(mask_enable)){
reg=INA260_REG_MASKENABLE;
} else if (INA260_IS_ATTR(alert_limit)){
reg=INA260_REG_ALERTLIMIT;
}
if(ina260_write_register(cdata, reg, uvalue))
return -1;
return count;
}
static ssize_t attr_metric_show(struct kobject *_kobj,
struct kobj_attribute *attr,
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;
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;
}
static ssize_t attr_field_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(avg)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%d\n", ina260_avgs[(rvalue>>9)&0x7]);
} else if (INA260_IS_ATTR(mode)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%s\n", ina260_modes[rvalue&0x7]);
} else if (INA260_IS_ATTR(ishct)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%s\n", ina260_ishcts_vbusct[(rvalue>>3)&0x7]);
} else if (INA260_IS_ATTR(vbusct)){
if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&rvalue))
return -1;
return sprintf(buf, "%s\n", ina260_ishcts_vbusct[(rvalue>>6)&0x7]);
} else if (INA260_IS_ATTR(aff)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>4)&0x1);
} else if (INA260_IS_ATTR(cvrf)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>3)&0x1);
} else if (INA260_IS_ATTR(ocl)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>15)&0x1);
}else if (INA260_IS_ATTR(ucl)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>14)&0x1);
}else if (INA260_IS_ATTR(bol)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>13)&0x1);
}else if (INA260_IS_ATTR(bul)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>12)&0x1);
}else if (INA260_IS_ATTR(pol)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>11)&0x1);
}else if (INA260_IS_ATTR(cnvr)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>10)&0x1);
}else if (INA260_IS_ATTR(ovf)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>2)&0x1);
}else if (INA260_IS_ATTR(apol)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", (rvalue>>1)&0x1);
}else if (INA260_IS_ATTR(len)){
if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&rvalue))
return -1;
return sprintf(buf, "%d\n", rvalue&0x1);
}else if (INA260_IS_ATTR(did)){
if(ina260_read_register(cdata, INA260_REG_DIE,&rvalue))
return -1;
return sprintf(buf, "0x%x\n", (rvalue>>4));
}else if (INA260_IS_ATTR(rid)){
if(ina260_read_register(cdata, INA260_REG_DIE,&rvalue))
return -1;
return sprintf(buf, "0x%x\n", rvalue&0x7);
}else if (INA260_IS_ATTR(reset)){
return sprintf(buf, "0\n");
}
return 0;
}
static int ina260_write_field3(struct client_data *cdata, unsigned char reg, unsigned char n, int uvalue){
int rvalue;
if(!(uvalue>=0 && uvalue <8))
return -EINVAL;
// Fetch register value:
if(ina260_read_register(cdata,reg,&rvalue))
return 1;
// Write bits:
rvalue &= ~(0x7 << n); // clear bits
rvalue |= (uvalue << n);
// Write register value:
if(ina260_write_register(cdata,reg,rvalue))
return 1;
return 0;
}
static int ina260_write_field1(struct client_data *cdata, unsigned char reg, unsigned char n, int uvalue){
int rvalue;
if(!(uvalue==0 || uvalue == 1))
return -EINVAL;
// Fetch register value:
if(ina260_read_register(cdata,reg,&rvalue))
return 1;
// Set bit:
rvalue &= ~(1<< n); // clear bit
rvalue |= (uvalue << n);
// Write register value
if(ina260_write_register(cdata,reg,rvalue))
return 1;
return 0;
}
static ssize_t attr_field_store(struct kobject *_kobj,
struct kobj_attribute *attr,
const char __user *buf, size_t count)
{
int uvalue, ret=0;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
// Extract user supplied value:
if(kstrtoint(buf, 0,&uvalue))
return -EINVAL;
// Store:
if(INA260_IS_ATTR(reset) && uvalue!=0){
ret=ina260_write_register(cdata, INA260_REG_CONFIGURATION, 0xFFFF); // Much quicker this way
} else if(INA260_IS_ATTR(avg)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,9,uvalue);
} else if(INA260_IS_ATTR(mode)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,0,uvalue);
} else if(INA260_IS_ATTR(ishct)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,3,uvalue);
} else if(INA260_IS_ATTR(vbusct)){
ret=ina260_write_field3(cdata,INA260_REG_CONFIGURATION,6,uvalue);
} else if(INA260_IS_ATTR(ocl)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,15,uvalue);
} else if(INA260_IS_ATTR(ucl)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,14,uvalue);
} else if(INA260_IS_ATTR(bol)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,13,uvalue);
} else if(INA260_IS_ATTR(bul)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,12,uvalue);
} else if(INA260_IS_ATTR(pol)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,11,uvalue);
} else if(INA260_IS_ATTR(cnvr)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,10,uvalue);
} else if(INA260_IS_ATTR(apol)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,1,uvalue);
} else if(INA260_IS_ATTR(len)){
ret=ina260_write_field1(cdata,INA260_REG_MASKENABLE,0,uvalue);
}
return ret ? ret: count;
}
// ----- Registers -----
static struct kobj_attribute configuration_attribute =
__ATTR(configuration, 0664, attr_show, attr_store);
static struct kobj_attribute current_attribute = {
.attr = {.name = "current", // current is a defined macro so need to do it manually
.mode = VERIFY_OCTAL_PERMISSIONS(0444) },
.show = attr_show,
.store = attr_store,
};
static struct kobj_attribute bus_voltage_attribute =
__ATTR(bus_voltage, 0444, attr_show, attr_store);
static struct kobj_attribute power_attribute =
__ATTR(power, 0444, attr_show, attr_store);
static struct kobj_attribute mask_enable_attribute =
__ATTR(mask_enable, 0664, attr_show, attr_store);
static struct kobj_attribute alert_limit_attribute =
__ATTR(alert_limit, 0664, attr_show, attr_store);
static struct kobj_attribute manufacturer_id_attribute =
__ATTR(manufacturer_id, 0664, attr_show, attr_store);
static struct kobj_attribute die_id_attribute=
__ATTR(die_id, 0664, attr_show, attr_store);
static struct attribute *registers_attrs[] = {
&configuration_attribute.attr,
&current_attribute.attr,
&bus_voltage_attribute.attr,
&power_attribute.attr,
&mask_enable_attribute.attr,
&alert_limit_attribute.attr,
&manufacturer_id_attribute.attr,
&die_id_attribute.attr,
NULL,
};
static const struct attribute_group registers_group = {
.attrs = registers_attrs,
.name = "registers"
};
// ----- Metrics -----
static struct kobj_attribute metric_power_attribute =
__ATTR(power, 0444, attr_metric_show, NULL);
static struct kobj_attribute metric_voltage_attribute =
__ATTR(voltage, 0444, 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
};
// ----- Fields -----
static struct kobj_attribute reset_field_attribute =
__ATTR(reset, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute avg_field_attribute =
__ATTR(avg, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute mode_field_attribute =
__ATTR(mode, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ishct_field_attribute =
__ATTR(ishct, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute vbusct_field_attribute =
__ATTR(vbusct, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute aff_field_attribute =
__ATTR(aff, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute cvrf_field_attribute =
__ATTR(cvrf, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute ocl_field_attribute =
__ATTR(ocl, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ucl_field_attribute =
__ATTR(ucl, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute bol_field_attribute =
__ATTR(bol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute bul_field_attribute =
__ATTR(bul, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute pol_field_attribute =
__ATTR(pol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute cnvr_field_attribute =
__ATTR(cnvr, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ovf_field_attribute =
__ATTR(ovf, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute apol_field_attribute =
__ATTR(apol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute len_field_attribute =
__ATTR(len, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute did_field_attribute =
__ATTR(did, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute rid_field_attribute =
__ATTR(rid, 0444, attr_field_show, attr_field_store);
static struct attribute *fields_attrs[] = {
&reset_field_attribute.attr,
&avg_field_attribute.attr,
&mode_field_attribute.attr,
&ishct_field_attribute.attr,
&vbusct_field_attribute.attr,
&aff_field_attribute.attr,
&cvrf_field_attribute.attr,
&ocl_field_attribute.attr,
&ucl_field_attribute.attr,
&bol_field_attribute.attr,
&bul_field_attribute.attr,
&pol_field_attribute.attr,
&cnvr_field_attribute.attr,
&ovf_field_attribute.attr,
&apol_field_attribute.attr,
&len_field_attribute.attr,
&did_field_attribute.attr,
&rid_field_attribute.attr,
NULL,
};
static const struct attribute_group fields_group = {
.attrs = fields_attrs,
.name = "fields"
};
static struct kobj_type ina260_ktype = {
.sysfs_ops = &kobj_sysfs_ops,
};
/**
* @brief Compare on ina260 register to a supplied value
*
* @param client i2c client to use for communications
* @param reg register to use for comparison
* @param value register expected value
* @return int 0 if register contains value and 1 otherwise
*/
static int ina260_probe_register(struct i2c_client *client, unsigned char reg, int value){
unsigned char bytes[2];
if(i2c_master_send(client,&reg,1)<0)
return 1;
if(i2c_master_recv(client,bytes,2)<0)
return 1;
return ((bytes[0]<<8) | bytes[1])!=value;
}
static int ina260_probe_new(struct i2c_client *client){
struct client_data *p;
// Attempt device discovery:
if(ina260_probe_register(client,INA260_REG_MANUFACTURER,0x5449) ||
ina260_probe_register(client,INA260_REG_DIE,0x2270)){
printk("ina260 probe fails bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
return 1;
}
// Initialize client data:
printk("ina260 detected bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
p=kzalloc(sizeof(struct client_data),GFP_KERNEL);
kobject_init(&p->kobj,&ina260_ktype);
// Setup sysfs root kobject:
if(kobject_add(&p->kobj,ina260_kobj,client->dev.kobj.name)){
kobject_put(&p->kobj);
kfree(p);
return 1;
}
p->client=client;
p->reg=INA260_REG_DIE; // Maintain cache coherence
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);
return 1;
}
return 0;
}
static int ina260_remove(struct i2c_client *client){
struct client_data *p=i2c_get_clientdata(client);
kobject_put(&p->kobj);
kfree(p);
printk("ina260 removed bus=%d address=0x%02x\n",client->adapter->nr,client->addr);
return 0;
}
static struct i2c_driver ina260_driver = {
.driver = {
.name = "ina260"
},
.probe_new = ina260_probe_new,
.remove = ina260_remove,
.id_table = ina260_ids
};
static int __init ina260_init(void){
ina260_kobj = kobject_create_and_add("ina260",kernel_kobj);
i2c_add_driver(&ina260_driver);
return 0;
}
static void __exit ina260_exit(void){
i2c_del_driver(&ina260_driver);
kobject_put(ina260_kobj);
}
module_init(ina260_init);
module_exit(ina260_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Loïc Guegan");
MODULE_DESCRIPTION("INA260 Texas Instruments");
MODULE_VERSION("1.0");

View file

@ -1,107 +0,0 @@
#include "linux/module.h"
#include "linux/kernel.h"
#include <linux/hwmon.h>
#include "linux/i2c.h"
static int ina260_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
*val=5;
return 0;
}
static int ina260_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
{
return 0;
}
static umode_t ina260_is_visible(const void *drvdata,
enum hwmon_sensor_types type,
u32 attr, int channel){
return 0444;
}
#define HWMON_CHANNEL_INFO(stype, ...) \
(&(struct hwmon_channel_info) { \
.type = hwmon_##stype, \
.config = (u32 []) { \
__VA_ARGS__, 0 \
} \
})
static const struct hwmon_channel_info * ina260_info[] = {
HWMON_CHANNEL_INFO(power,HWMON_P_INPUT),
NULL
};
static const struct hwmon_ops ina260_hwmon_ops = {
.is_visible = ina260_is_visible,
.read = ina260_read,
.write = ina260_write,
};
static const struct hwmon_chip_info ina260_chip_info = {
.ops = &ina260_hwmon_ops,
.info = ina260_info,
};
/*const struct attribute_group *extra_groups[] = {
NULL
};*/
static int ina260_probe_new(struct i2c_client *client){
struct device *hwmon_dev;
hwmon_dev=hwmon_device_register_with_info(&client->dev,client->name,NULL,
&ina260_chip_info,NULL);
if (IS_ERR(hwmon_dev))
return PTR_ERR(hwmon_dev);
return 0;
}
static int ina260_remove(struct i2c_client *client){
printk("Unregister client...\n");
hwmon_device_unregister(&client->dev);
return 0;
}
static const struct i2c_device_id ina260_ids[] = {
{ "ina260", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c,ina260_ids);
static struct i2c_driver ina260_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "ina260HWMON"
},
.probe_new = ina260_probe_new,
.remove = ina260_remove,
.id_table = ina260_ids
};
static int __init ina260_init(void){
printk("Init\n");
i2c_add_driver(&ina260_driver);
return 0;
}
static void __exit ina260_exit(void){
printk("Quit\n");
i2c_del_driver(&ina260_driver);
}
module_init(ina260_init);
module_exit(ina260_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Loïc Guegan");
MODULE_DESCRIPTION("INA260 Texas Instruments");
MODULE_VERSION("1.0");