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
c66f086e89
commit
2223403c81
1 changed files with 57 additions and 53 deletions
110
ina260.c
110
ina260.c
|
@ -62,43 +62,43 @@ const char *buf, size_t count) \
|
|||
* @brief Embedded user data
|
||||
*/
|
||||
struct client_data {
|
||||
struct i2c_client *client;
|
||||
struct regmap *regmap;
|
||||
struct i2c_client *client;
|
||||
struct regmap *regmap;
|
||||
};
|
||||
|
||||
static int ina260_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
|
||||
u32 attr, int channel, long *val)
|
||||
u32 attr, int channel, long *val)
|
||||
{
|
||||
int rvalue, reg, err;
|
||||
struct client_data *cdata = dev_get_drvdata(dev);
|
||||
switch (type) {
|
||||
case hwmon_power:
|
||||
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:
|
||||
case hwmon_curr:
|
||||
reg = INA260_REG_CURRENT;
|
||||
break;
|
||||
case hwmon_in:
|
||||
case hwmon_in:
|
||||
reg = INA260_REG_VOLTAGE;
|
||||
break;
|
||||
default:
|
||||
default:
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
err = regmap_read(cdata->regmap, reg, &rvalue);
|
||||
if (err < 0) {
|
||||
}
|
||||
err = regmap_read(cdata->regmap, reg, &rvalue);
|
||||
if (err < 0)
|
||||
return err;
|
||||
} else if (type == hwmon_power) {
|
||||
else if (type == hwmon_power)
|
||||
*val = rvalue * 10000;
|
||||
} else {
|
||||
else
|
||||
*val = div_u64(rvalue * 25, 100) + rvalue;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ina260_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
|
||||
u32 attr, int channel, long val)
|
||||
{
|
||||
return -EOPNOTSUPP;
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
INA260_REG_SHOW(configuration, INA260_REG_CONFIGURATION)
|
||||
|
@ -118,19 +118,20 @@ 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)
|
||||
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(in, HWMON_I_INPUT),
|
||||
HWMON_CHANNEL_INFO(power, HWMON_P_INPUT),
|
||||
HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT),
|
||||
HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT),
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct hwmon_ops ina260_hwmon_ops = {
|
||||
.is_visible = ina260_hwmon_is_visible,
|
||||
.is_visible = ina260_hwmon_is_visible,
|
||||
.read = ina260_hwmon_read,
|
||||
.write = ina260_hwmon_write,
|
||||
};
|
||||
|
@ -139,6 +140,7 @@ 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(curr);
|
||||
|
@ -149,60 +151,62 @@ 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_curr.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,
|
||||
&dev_attr_configuration.attr,
|
||||
&dev_attr_curr.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"
|
||||
.name = "registers"
|
||||
};
|
||||
|
||||
const struct attribute_group *extra_groups[] = {
|
||||
®isters_group,
|
||||
NULL
|
||||
®isters_group,
|
||||
NULL
|
||||
};
|
||||
|
||||
static int ina260_probe_new(struct i2c_client *client)
|
||||
{
|
||||
struct client_data *p;
|
||||
struct device *hwmon_dev;
|
||||
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;
|
||||
// Initialize client data:
|
||||
printk(KERN_INFO "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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
struct client_data *p = i2c_get_clientdata(client);
|
||||
|
||||
kfree(p);
|
||||
hwmon_device_unregister(&client->dev);
|
||||
printk(KERN_INFO "Removing ina260 [bus=%d address=0x%02x]\n", client->adapter->nr, client->addr);
|
||||
}
|
||||
|
||||
static const struct i2c_device_id ina260_ids[] = {
|
||||
{ "ina260", 0 },
|
||||
{ }
|
||||
{ "ina260", 0 },
|
||||
{ }
|
||||
};
|
||||
MODULE_DEVICE_TABLE(i2c, ina260_ids);
|
||||
|
||||
static struct i2c_driver ina260_driver = {
|
||||
.class = I2C_CLASS_HWMON,
|
||||
.class = I2C_CLASS_HWMON,
|
||||
.driver = {
|
||||
.name = "ina260"
|
||||
},
|
||||
|
@ -213,13 +217,13 @@ static struct i2c_driver ina260_driver = {
|
|||
|
||||
static int __init ina260_init(void)
|
||||
{
|
||||
i2c_add_driver(&ina260_driver);
|
||||
return 0;
|
||||
i2c_add_driver(&ina260_driver);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit ina260_exit(void)
|
||||
{
|
||||
i2c_del_driver(&ina260_driver);
|
||||
i2c_del_driver(&ina260_driver);
|
||||
}
|
||||
|
||||
module_init(ina260_init);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue