ina260-sysfs-driver/ina260.c

261 lines
6.3 KiB
C
Raw Normal View History

2023-08-14 12:30:53 +02:00
// SPDX-License-Identifier: GPL-2.0-only
/*
* Driver for Texas Instruments INA260 power monitor chip
* Datasheet: https://www.ti.com/lit/gpn/INA260
*
* Copyright (C) 2023 GUEGAN Loic <loic.guegan@mailbox.org>
*/
2023-07-13 10:56:14 +02:00
#include "linux/module.h"
#include "linux/uaccess.h"
2023-07-12 16:07:20 +02:00
#include "linux/i2c.h"
#include "linux/kobject.h"
2023-07-13 10:56:14 +02:00
#include "linux/slab.h"
#include "linux/kernel.h"
2023-07-12 16:07:20 +02:00
#include <linux/sysfs.h>
2023-08-14 10:39:36 +02:00
#include <linux/hwmon.h>
#include <linux/regmap.h>
2023-07-12 16:07:20 +02:00
2023-08-14 12:30:53 +02:00
// INA260 registers
2023-08-14 17:10:40 +02:00
#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
2023-08-14 10:39:36 +02:00
static struct regmap_config ina260_regmap_config = {
2023-08-14 18:53:14 +02:00
.max_register = INA260_REG_DIE,
2023-08-14 10:39:36 +02:00
.reg_bits = 8,
.val_bits = 16,
};
2023-08-14 18:53:14 +02:00
#define INA260_REG_SHOW(_attr, _reg) \
2023-08-14 10:39:36 +02:00
static ssize_t _attr##_show(struct device *dev, struct device_attribute *attr, char *buf) \
{ \
2023-08-14 18:53:14 +02:00
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); \
2023-08-14 10:39:36 +02:00
}
2023-08-14 18:53:14 +02:00
#define INA260_REG_STORE(_attr, _reg) \
static ssize_t _attr##_store(struct device *dev, struct device_attribute *attr, \
const char *buf, size_t count) \
2023-08-14 10:39:36 +02:00
{ \
2023-08-14 18:53:14 +02:00
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; \
2023-08-14 10:39:36 +02:00
}
2023-08-14 12:13:56 +02:00
/**
* @brief Embedded user data
*/
2023-07-12 16:07:20 +02:00
struct client_data {
2023-08-14 19:17:48 +02:00
struct i2c_client *client;
struct regmap *regmap;
2023-07-12 16:07:20 +02:00
};
2023-07-13 10:56:14 +02:00
2023-08-14 10:39:36 +02:00
static int ina260_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
2023-08-14 20:58:17 +02:00
u32 attr, int channel, long *val)
2023-08-10 17:38:01 +02:00
{
2023-08-15 11:21:19 +02:00
int rvalue, reg, err, rem;
2023-08-14 19:17:48 +02:00
struct client_data *cdata = dev_get_drvdata(dev);
switch (type) {
case hwmon_power:
2023-08-14 18:53:14 +02:00
reg = INA260_REG_POWER;
break;
2023-08-14 19:17:48 +02:00
case hwmon_curr:
2023-08-14 18:53:14 +02:00
reg = INA260_REG_CURRENT;
break;
2023-08-14 19:17:48 +02:00
case hwmon_in:
2023-08-14 18:53:14 +02:00
reg = INA260_REG_VOLTAGE;
break;
2023-08-14 19:17:48 +02:00
default:
2023-08-14 18:53:14 +02:00
return -EOPNOTSUPP;
2023-08-14 19:17:48 +02:00
}
err = regmap_read(cdata->regmap, reg, &rvalue);
if (err < 0)
2023-08-14 18:53:14 +02:00
return err;
2023-08-14 19:17:48 +02:00
else if (type == hwmon_power)
2023-08-14 18:53:14 +02:00
*val = rvalue * 10000;
2023-08-14 19:17:48 +02:00
else
2023-08-15 11:21:19 +02:00
*val = div_u64_rem(rvalue * 25, 100, &rem) + rvalue + div_u64(rem, 10);
2023-08-14 18:53:14 +02:00
return 0;
2023-07-12 16:14:26 +02:00
}
2023-08-14 10:39:36 +02:00
static int ina260_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
2023-08-14 20:58:17 +02:00
u32 attr, int channel, long val)
2023-07-12 16:07:20 +02:00
{
2023-08-14 19:17:48 +02:00
return -EOPNOTSUPP;
2023-07-12 16:07:20 +02:00
}
2023-08-14 18:53:14 +02:00
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)
2023-08-14 10:39:36 +02:00
static umode_t ina260_hwmon_is_visible(const void *drvdata,
2023-08-14 20:58:17 +02:00
enum hwmon_sensor_types type, u32 attr, int channel)
2023-08-14 18:53:14 +02:00
{
2023-08-15 11:21:19 +02:00
switch (type) {
case hwmon_in:
switch (attr) {
case hwmon_in_input:
return 0444;
default:
return 0;
}
case hwmon_curr:
switch (attr) {
case hwmon_curr_input:
return 0444;
default:
return 0;
}
case hwmon_power:
switch (attr) {
case hwmon_power_input:
return 0444;
default:
return 0;
}
default:
return 0;
}
2023-08-14 18:53:14 +02:00
}
2023-08-14 19:17:48 +02:00
2023-08-14 18:53:14 +02:00
static const struct hwmon_channel_info *ina260_hwmon_info[] = {
2023-08-14 19:17:48 +02:00
HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
2023-08-14 18:53:14 +02:00
HWMON_CHANNEL_INFO(power, HWMON_P_INPUT),
2023-08-14 19:17:48 +02:00
HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT),
2023-08-14 10:39:36 +02:00
NULL
};
2023-08-14 19:17:48 +02:00
2023-08-14 10:39:36 +02:00
static const struct hwmon_ops ina260_hwmon_ops = {
2023-08-14 19:17:48 +02:00
.is_visible = ina260_hwmon_is_visible,
2023-08-14 10:39:36 +02:00
.read = ina260_hwmon_read,
.write = ina260_hwmon_write,
};
2023-07-12 16:07:20 +02:00
2023-08-14 10:39:36 +02:00
static const struct hwmon_chip_info ina260_chip_info = {
.ops = &ina260_hwmon_ops,
.info = ina260_hwmon_info,
2023-07-12 16:07:20 +02:00
};
2023-08-14 19:17:48 +02:00
2023-08-14 10:39:36 +02:00
// ----- Registers -----
static DEVICE_ATTR_RW(configuration);
2023-08-14 12:13:56 +02:00
static DEVICE_ATTR_RW(curr);
2023-08-14 10:39:36 +02:00
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);
2023-07-12 16:07:20 +02:00
static struct attribute *registers_attrs[] = {
2023-08-14 19:17:48 +02:00
&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,
2023-07-12 16:07:20 +02:00
};
2023-08-14 20:58:17 +02:00
2023-07-12 16:07:20 +02:00
static const struct attribute_group registers_group = {
.attrs = registers_attrs,
2023-08-14 19:17:48 +02:00
.name = "registers"
2023-07-12 16:07:20 +02:00
};
2023-08-14 10:39:36 +02:00
const struct attribute_group *extra_groups[] = {
2023-08-14 19:17:48 +02:00
&registers_group,
NULL
2023-08-14 10:39:36 +02:00
};
2023-08-14 18:53:14 +02:00
static int ina260_probe_new(struct i2c_client *client)
{
2023-08-14 19:17:48 +02:00
struct client_data *p;
struct device *hwmon_dev;
// Initialize client data:
2023-08-15 11:34:10 +02:00
dev_dbg(&client->dev, "Adding ina260 [bus=%d address=0x%02x]\n",
2023-08-14 20:58:17 +02:00
client->adapter->nr, client->addr);
2023-08-15 11:34:10 +02:00
p = kzalloc(sizeof(*p), GFP_KERNEL);
2023-08-14 19:17:48 +02:00
p->client = client;
2023-08-14 10:39:36 +02:00
p->regmap = devm_regmap_init_i2c(client, &ina260_regmap_config);
2023-08-14 19:17:48 +02:00
hwmon_dev = hwmon_device_register_with_info(&client->dev, client->name, p,
2023-08-14 20:58:17 +02:00
&ina260_chip_info, extra_groups);
2023-08-14 10:39:36 +02:00
if (IS_ERR(hwmon_dev))
return PTR_ERR(hwmon_dev);
2023-08-14 19:17:48 +02:00
return 0;
2023-07-12 16:07:20 +02:00
}
2023-08-14 18:53:14 +02:00
static void ina260_remove(struct i2c_client *client)
{
2023-08-14 19:17:48 +02:00
struct client_data *p = i2c_get_clientdata(client);
kfree(p);
hwmon_device_unregister(&client->dev);
2023-08-15 11:34:10 +02:00
dev_dbg(&client->dev, "Removing ina260 [bus=%d address=0x%02x]\n",
2023-08-14 20:58:17 +02:00
client->adapter->nr, client->addr);
2023-07-12 16:07:20 +02:00
}
2023-08-14 12:13:56 +02:00
static const struct i2c_device_id ina260_ids[] = {
2023-08-14 19:17:48 +02:00
{ "ina260", 0 },
{ }
2023-08-14 12:13:56 +02:00
};
2023-08-14 18:53:14 +02:00
MODULE_DEVICE_TABLE(i2c, ina260_ids);
2023-08-14 12:13:56 +02:00
2023-07-12 16:07:20 +02:00
static struct i2c_driver ina260_driver = {
2023-08-14 19:17:48 +02:00
.class = I2C_CLASS_HWMON,
2023-07-12 16:07:20 +02:00
.driver = {
.name = "ina260"
},
.probe_new = ina260_probe_new,
.remove = ina260_remove,
.id_table = ina260_ids
};
2023-08-14 18:53:14 +02:00
static int __init ina260_init(void)
{
2023-08-14 19:17:48 +02:00
i2c_add_driver(&ina260_driver);
return 0;
2023-07-12 16:07:20 +02:00
}
2023-08-14 18:53:14 +02:00
static void __exit ina260_exit(void)
{
2023-08-14 19:17:48 +02:00
i2c_del_driver(&ina260_driver);
2023-07-12 16:07:20 +02:00
}
module_init(ina260_init);
module_exit(ina260_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Loïc Guegan");
MODULE_DESCRIPTION("INA260 Texas Instruments");
2023-07-12 17:51:15 +02:00
MODULE_VERSION("1.0");