mirror of
https://gitlab.com/manzerbredes/ina260-sysfs-driver.git
synced 2025-04-19 04:09:45 +00:00
Minor changes
This commit is contained in:
parent
15858006f9
commit
e2e40a7305
2 changed files with 14 additions and 51 deletions
4
Makefile
4
Makefile
|
@ -13,7 +13,7 @@ run: ina260.c
|
|||
- rmmod ina260
|
||||
make clean
|
||||
make
|
||||
insmod ina260
|
||||
insmod ina260.ko
|
||||
echo ina260 $(TEST_DEV_ADDR) > /sys/bus/i2c/devices/i2c-2/new_device
|
||||
|
||||
run-full: ina2602.c
|
||||
|
@ -21,7 +21,7 @@ run-full: ina2602.c
|
|||
- rmmod ina260_full
|
||||
make clean
|
||||
make
|
||||
insmod ina260_full
|
||||
insmod ina260_full.ko
|
||||
echo ina260 $(TEST_DEV_ADDR) > /sys/bus/i2c/devices/i2c-2/new_device
|
||||
|
||||
clean:
|
||||
|
|
61
ina260.c
61
ina260.c
|
@ -28,8 +28,9 @@ static struct regmap_config ina260_regmap_config = {
|
|||
#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)) \
|
||||
unsigned int rvalue; \
|
||||
struct client_data *cdata=dev_get_drvdata(dev); \
|
||||
if(regmap_read(cdata->regmap, (_reg), &rvalue)) \
|
||||
return -1; \
|
||||
return sprintf(buf, "%x\n", rvalue); \
|
||||
}
|
||||
|
@ -37,15 +38,15 @@ static ssize_t _attr##_show(struct device *dev, struct device_attribute *attr, c
|
|||
#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); \
|
||||
int uvalue; \
|
||||
struct client_data *cdata=dev_get_drvdata(dev); \
|
||||
if(kstrtoint(buf, 0,&uvalue)) \
|
||||
return -EINVAL; \
|
||||
if(ina260_write_register(cdata, (_reg), uvalue)) \
|
||||
if(regmap_write(cdata->regmap, (_reg), uvalue)) \
|
||||
return -1; \
|
||||
return count; \
|
||||
}
|
||||
|
||||
|
||||
#define HWMON_CHANNEL_INFO(stype, ...) \
|
||||
(&(struct hwmon_channel_info) { \
|
||||
.type = hwmon_##stype, \
|
||||
|
@ -54,25 +55,6 @@ static ssize_t _attr##_store(struct device *dev, struct device_attribute *attr,
|
|||
} \
|
||||
})
|
||||
|
||||
// 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;
|
||||
|
@ -94,21 +76,8 @@ MODULE_DEVICE_TABLE(i2c,ina260_ids);
|
|||
* @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,®,1)<0)
|
||||
return 1;
|
||||
cdata->reg = reg;
|
||||
if(i2c_master_recv(cdata->client,bytes,2)<0)
|
||||
return 1;
|
||||
}*/
|
||||
static int ina260_read_register(struct client_data* cdata, unsigned char reg, unsigned int *value){
|
||||
return regmap_read(cdata->regmap, reg, value);
|
||||
// *value=(bytes[0]<<8) | bytes[1];
|
||||
//return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,13 +89,7 @@ static int ina260_read_register(struct client_data* cdata, unsigned char reg, in
|
|||
* @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;
|
||||
return regmap_write(cdata->regmap, reg, value);
|
||||
}
|
||||
|
||||
|
||||
|
@ -136,20 +99,20 @@ static int ina260_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
|
|||
int rvalue;
|
||||
struct client_data *cdata=dev_get_drvdata(dev);
|
||||
if(type == hwmon_power){
|
||||
if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue))
|
||||
if(regmap_read(cdata->regmap, INA260_REG_POWER, &rvalue))
|
||||
return -1;
|
||||
*val=10*rvalue*1000;
|
||||
} else if (type == hwmon_curr){
|
||||
if(ina260_read_register(cdata, INA260_REG_CURRENT,&rvalue))
|
||||
if(regmap_read(cdata->regmap, 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))
|
||||
if(regmap_read(cdata->regmap, INA260_REG_VOLTAGE, &rvalue))
|
||||
return -1;
|
||||
|
||||
*val=((rvalue*25/100) + rvalue)*100+(rvalue*25%100);
|
||||
*val/=100;
|
||||
*val/=100; // Skip remainder here
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue