Minor changes

This commit is contained in:
Loic Guegan 2023-08-14 18:53:14 +02:00
parent 45e16466b1
commit c66f086e89

154
ina260.c
View file

@ -27,34 +27,35 @@
#define INA260_REG_DIE 0xFF
static struct regmap_config ina260_regmap_config = {
.max_register = INA260_REG_DIE,
.max_register = INA260_REG_DIE,
.reg_bits = 8,
.val_bits = 16,
};
#define INA260_REG_SHOW(_attr,_reg) \
#define INA260_REG_SHOW(_attr, _reg) \
static ssize_t _attr##_show(struct device *dev, struct device_attribute *attr, char *buf) \
{ \
unsigned int rvalue; \
int err; \
struct client_data *cdata=dev_get_drvdata(dev); \
err = regmap_read(cdata->regmap, (_reg), &rvalue); \
if(err>0) \
return err; \
return sprintf(buf, "0x%x\n", rvalue); \
unsigned int rvalue; \
int err; \
struct client_data *cdata = dev_get_drvdata(dev); \
err = regmap_read(cdata->regmap, (_reg), &rvalue); \
if (err > 0) \
return err; \
return sprintf(buf, "0x%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) \
#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, err; \
struct client_data *cdata=dev_get_drvdata(dev); \
if(kstrtoint(buf, 0,&uvalue)) \
return -EINVAL; \
err = regmap_write(cdata->regmap, (_reg), uvalue); \
if(err>0) \
return err; \
return count; \
int uvalue, err; \
struct client_data *cdata = dev_get_drvdata(dev); \
if (kstrtoint(buf, 0, &uvalue)) \
return -EINVAL; \
err = regmap_write(cdata->regmap, (_reg), uvalue); \
if (err > 0) \
return err; \
return count; \
}
/**
@ -69,29 +70,29 @@ static int ina260_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
int rvalue, reg, err;
struct client_data *cdata=dev_get_drvdata(dev);
switch(type){
case hwmon_power:
reg=INA260_REG_POWER;
break;
case hwmon_curr:
reg=INA260_REG_CURRENT;
break;
case hwmon_in:
reg=INA260_REG_VOLTAGE;
break;
default:
return -EOPNOTSUPP;
struct client_data *cdata = dev_get_drvdata(dev);
switch (type) {
case hwmon_power:
reg = INA260_REG_POWER;
break;
case hwmon_curr:
reg = INA260_REG_CURRENT;
break;
case hwmon_in:
reg = INA260_REG_VOLTAGE;
break;
default:
return -EOPNOTSUPP;
}
err=regmap_read(cdata->regmap, reg, &rvalue);
if(err<0){
return err;
} else if(type == hwmon_power) {
*val=rvalue*10000;
} else{
*val=div_u64(rvalue*25,100)+rvalue;
err = regmap_read(cdata->regmap, reg, &rvalue);
if (err < 0) {
return err;
} else if (type == hwmon_power) {
*val = rvalue * 10000;
} else {
*val = div_u64(rvalue * 25, 100) + rvalue;
}
return 0;
return 0;
}
static int ina260_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
@ -100,31 +101,32 @@ static int ina260_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
return -EOPNOTSUPP;
}
INA260_REG_SHOW(configuration,INA260_REG_CONFIGURATION)
INA260_REG_SHOW(curr,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_SHOW(configuration, INA260_REG_CONFIGURATION)
INA260_REG_SHOW(curr, 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(curr,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)
INA260_REG_STORE(configuration, INA260_REG_CONFIGURATION)
INA260_REG_STORE(curr, 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),
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 = {
@ -155,7 +157,7 @@ static struct attribute *registers_attrs[] = {
&dev_attr_alert_limit.attr,
&dev_attr_manufacturer_id.attr,
&dev_attr_die_id.attr,
NULL,
NULL,
};
static const struct attribute_group registers_group = {
.attrs = registers_attrs,
@ -167,35 +169,37 @@ const struct attribute_group *extra_groups[] = {
NULL
};
static int ina260_probe_new(struct i2c_client *client){
static int ina260_probe_new(struct i2c_client *client)
{
struct client_data *p;
struct device *hwmon_dev;
// Initialize client data:
printk("Adding ina260 [bus=%d address=0x%02x]\n",client->adapter->nr,client->addr);
p=kzalloc(sizeof(struct client_data),GFP_KERNEL);
p->client=client;
printk("Adding ina260 [bus=%d address=0x%02x]\n", client->adapter->nr, client->addr);
p = kzalloc(sizeof(struct client_data), GFP_KERNEL);
p->client = client;
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);
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 void ina260_remove(struct i2c_client *client){
struct client_data *p=i2c_get_clientdata(client);
static void ina260_remove(struct i2c_client *client)
{
struct client_data *p = i2c_get_clientdata(client);
kfree(p);
hwmon_device_unregister(&client->dev);
printk("Removing ina260 [bus=%d address=0x%02x]\n",client->adapter->nr,client->addr);
printk("Removing ina260 [bus=%d address=0x%02x]\n", client->adapter->nr, client->addr);
}
static const struct i2c_device_id ina260_ids[] = {
{ "ina260", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c,ina260_ids);
MODULE_DEVICE_TABLE(i2c, ina260_ids);
static struct i2c_driver ina260_driver = {
.class = I2C_CLASS_HWMON,
@ -207,12 +211,14 @@ static struct i2c_driver ina260_driver = {
.id_table = ina260_ids
};
static int __init ina260_init(void){
static int __init ina260_init(void)
{
i2c_add_driver(&ina260_driver);
return 0;
}
static void __exit ina260_exit(void){
static void __exit ina260_exit(void)
{
i2c_del_driver(&ina260_driver);
}