* [PATCH 1/2] hwmon: (ltc2947) Use the energy64 attribute type to report the energy
@ 2025-10-12 21:16 Guenter Roeck
2025-10-12 21:16 ` [PATCH 2/2] hwmon: (ltc4282) " Guenter Roeck
2025-10-13 11:33 ` [PATCH 1/2] hwmon: (ltc2947) " Nuno Sá
0 siblings, 2 replies; 4+ messages in thread
From: Guenter Roeck @ 2025-10-12 21:16 UTC (permalink / raw)
To: Hardware Monitoring; +Cc: Guenter Roeck
Use the energy64 attribute type instead of a locally defined sysfs
attribute to report the accumulated energy.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/ltc2947-core.c | 60 ++++++++++++++++--------------------
1 file changed, 26 insertions(+), 34 deletions(-)
diff --git a/drivers/hwmon/ltc2947-core.c b/drivers/hwmon/ltc2947-core.c
index 244839167e51..90f70f732b41 100644
--- a/drivers/hwmon/ltc2947-core.c
+++ b/drivers/hwmon/ltc2947-core.c
@@ -9,8 +9,8 @@
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
#include <linux/module.h>
+#include <linux/math64.h>
#include <linux/mod_devicetable.h>
#include <linux/property.h>
#include <linux/regmap.h>
@@ -319,24 +319,6 @@ static int ltc2947_alarm_read(struct ltc2947_data *st, const u8 reg,
return ret;
}
-static ssize_t ltc2947_show_value(struct device *dev,
- struct device_attribute *da, char *buf)
-{
- struct ltc2947_data *st = dev_get_drvdata(dev);
- struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
- int ret;
- s64 val = 0;
-
- ret = ltc2947_val_read(st, attr->index, LTC2947_PAGE0, 6, &val);
- if (ret)
- return ret;
-
- /* value in microJoule. st->lsb_energy was multiplied by 10E9 */
- val = div_s64(val * st->lsb_energy, 1000);
-
- return sprintf(buf, "%lld\n", val);
-}
-
static int ltc2947_read_temp(struct device *dev, const u32 attr, long *val,
const int channel)
{
@@ -588,6 +570,23 @@ static int ltc2947_read_in(struct device *dev, const u32 attr, long *val,
return 0;
}
+static int ltc2947_read_energy(struct device *dev, s64 *val, const int channel)
+{
+ int reg = channel ? LTC2947_REG_ENERGY2 : LTC2947_REG_ENERGY1;
+ struct ltc2947_data *st = dev_get_drvdata(dev);
+ s64 __val = 0;
+ int ret;
+
+ ret = ltc2947_val_read(st, reg, LTC2947_PAGE0, 6, &__val);
+ if (ret)
+ return ret;
+
+ /* value in microJoule. st->lsb_energy was multiplied by 10E9 */
+ *val = DIV_S64_ROUND_CLOSEST(__val * st->lsb_energy, 1000);
+
+ return 0;
+}
+
static int ltc2947_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
@@ -600,6 +599,8 @@ static int ltc2947_read(struct device *dev, enum hwmon_sensor_types type,
return ltc2947_read_power(dev, attr, val);
case hwmon_temp:
return ltc2947_read_temp(dev, attr, val, channel);
+ case hwmon_energy64:
+ return ltc2947_read_energy(dev, (s64 *)val, channel);
default:
return -ENOTSUPP;
}
@@ -897,6 +898,8 @@ static umode_t ltc2947_is_visible(const void *data,
return ltc2947_power_is_visible(attr);
case hwmon_temp:
return ltc2947_temp_is_visible(attr);
+ case hwmon_energy64:
+ return 0444;
default:
return 0;
}
@@ -929,6 +932,9 @@ static const struct hwmon_channel_info * const ltc2947_info[] = {
HWMON_T_LABEL,
HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM | HWMON_T_MAX |
HWMON_T_MIN | HWMON_T_LABEL),
+ HWMON_CHANNEL_INFO(energy64,
+ HWMON_E_INPUT,
+ HWMON_E_INPUT),
NULL
};
@@ -944,19 +950,6 @@ static const struct hwmon_chip_info ltc2947_chip_info = {
.info = ltc2947_info,
};
-/* energy attributes are 6bytes wide so we need u64 */
-static SENSOR_DEVICE_ATTR(energy1_input, 0444, ltc2947_show_value, NULL,
- LTC2947_REG_ENERGY1);
-static SENSOR_DEVICE_ATTR(energy2_input, 0444, ltc2947_show_value, NULL,
- LTC2947_REG_ENERGY2);
-
-static struct attribute *ltc2947_attrs[] = {
- &sensor_dev_attr_energy1_input.dev_attr.attr,
- &sensor_dev_attr_energy2_input.dev_attr.attr,
- NULL,
-};
-ATTRIBUTE_GROUPS(ltc2947);
-
static int ltc2947_setup(struct ltc2947_data *st)
{
int ret;
@@ -1114,8 +1107,7 @@ int ltc2947_core_probe(struct regmap *map, const char *name)
return ret;
hwmon = devm_hwmon_device_register_with_info(dev, name, st,
- <c2947_chip_info,
- ltc2947_groups);
+ <c2947_chip_info, NULL);
return PTR_ERR_OR_ZERO(hwmon);
}
EXPORT_SYMBOL_GPL(ltc2947_core_probe);
--
2.45.2
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] hwmon: (ltc4282) Use the energy64 attribute type to report the energy
2025-10-12 21:16 [PATCH 1/2] hwmon: (ltc2947) Use the energy64 attribute type to report the energy Guenter Roeck
@ 2025-10-12 21:16 ` Guenter Roeck
2025-10-13 11:33 ` Nuno Sá
2025-10-13 11:33 ` [PATCH 1/2] hwmon: (ltc2947) " Nuno Sá
1 sibling, 1 reply; 4+ messages in thread
From: Guenter Roeck @ 2025-10-12 21:16 UTC (permalink / raw)
To: Hardware Monitoring; +Cc: Guenter Roeck
Use the energy64 attribute type instead of a locally defined sysfs
attribute to report the accumulated energy.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/ltc4282.c | 44 ++++++++++++-----------------------------
1 file changed, 13 insertions(+), 31 deletions(-)
diff --git a/drivers/hwmon/ltc4282.c b/drivers/hwmon/ltc4282.c
index 1d664a2d7b3c..44102879694a 100644
--- a/drivers/hwmon/ltc4282.c
+++ b/drivers/hwmon/ltc4282.c
@@ -12,7 +12,6 @@
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
#include <linux/i2c.h>
#include <linux/math.h>
#include <linux/minmax.h>
@@ -541,7 +540,7 @@ static int ltc4282_read_power_byte(const struct ltc4282_state *st, u32 reg,
return 0;
}
-static int ltc4282_read_energy(const struct ltc4282_state *st, u64 *val)
+static int ltc4282_read_energy(const struct ltc4282_state *st, s64 *val)
{
u64 temp, energy;
__be64 raw;
@@ -617,6 +616,12 @@ static int ltc4282_read(struct device *dev, enum hwmon_sensor_types type,
*val = st->energy_en;
}
return 0;
+ case hwmon_energy64:
+ scoped_guard(mutex, &st->lock) {
+ if (st->energy_en)
+ return ltc4282_read_energy(st, (s64 *)val);
+ }
+ return -ENODATA;
default:
return -EOPNOTSUPP;
}
@@ -1078,6 +1083,9 @@ static umode_t ltc4282_is_visible(const void *data,
case hwmon_energy:
/* hwmon_energy_enable */
return 0644;
+ case hwmon_energy64:
+ /* hwmon_energy_input */
+ return 0444;
default:
return 0;
}
@@ -1106,24 +1114,6 @@ static int ltc4282_read_labels(struct device *dev,
}
}
-static ssize_t ltc4282_energy_show(struct device *dev,
- struct device_attribute *da, char *buf)
-{
- struct ltc4282_state *st = dev_get_drvdata(dev);
- u64 energy;
- int ret;
-
- guard(mutex)(&st->lock);
- if (!st->energy_en)
- return -ENODATA;
-
- ret = ltc4282_read_energy(st, &energy);
- if (ret < 0)
- return ret;
-
- return sysfs_emit(buf, "%llu\n", energy);
-}
-
static const struct clk_ops ltc4282_ops = {
.recalc_rate = ltc4282_recalc_rate,
.determine_rate = ltc4282_determine_rate,
@@ -1588,6 +1578,8 @@ static const struct hwmon_channel_info * const ltc4282_info[] = {
HWMON_P_RESET_HISTORY | HWMON_P_LABEL),
HWMON_CHANNEL_INFO(energy,
HWMON_E_ENABLE),
+ HWMON_CHANNEL_INFO(energy64,
+ HWMON_E_INPUT),
NULL
};
@@ -1603,15 +1595,6 @@ static const struct hwmon_chip_info ltc4282_chip_info = {
.info = ltc4282_info,
};
-/* energy attributes are 6bytes wide so we need u64 */
-static SENSOR_DEVICE_ATTR_RO(energy1_input, ltc4282_energy, 0);
-
-static struct attribute *ltc4282_attrs[] = {
- &sensor_dev_attr_energy1_input.dev_attr.attr,
- NULL
-};
-ATTRIBUTE_GROUPS(ltc4282);
-
static int ltc4282_show_fault_log(void *arg, u64 *val, u32 mask)
{
struct ltc4282_state *st = arg;
@@ -1718,8 +1701,7 @@ static int ltc4282_probe(struct i2c_client *i2c)
mutex_init(&st->lock);
hwmon = devm_hwmon_device_register_with_info(dev, "ltc4282", st,
- <c4282_chip_info,
- ltc4282_groups);
+ <c4282_chip_info, NULL);
if (IS_ERR(hwmon))
return PTR_ERR(hwmon);
--
2.45.2
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH 2/2] hwmon: (ltc4282) Use the energy64 attribute type to report the energy
2025-10-12 21:16 ` [PATCH 2/2] hwmon: (ltc4282) " Guenter Roeck
@ 2025-10-13 11:33 ` Nuno Sá
0 siblings, 0 replies; 4+ messages in thread
From: Nuno Sá @ 2025-10-13 11:33 UTC (permalink / raw)
To: Guenter Roeck, Hardware Monitoring
On Sun, 2025-10-12 at 14:16 -0700, Guenter Roeck wrote:
> Use the energy64 attribute type instead of a locally defined sysfs
> attribute to report the accumulated energy.
>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
> drivers/hwmon/ltc4282.c | 44 ++++++++++++-----------------------------
> 1 file changed, 13 insertions(+), 31 deletions(-)
>
> diff --git a/drivers/hwmon/ltc4282.c b/drivers/hwmon/ltc4282.c
> index 1d664a2d7b3c..44102879694a 100644
> --- a/drivers/hwmon/ltc4282.c
> +++ b/drivers/hwmon/ltc4282.c
> @@ -12,7 +12,6 @@
> #include <linux/delay.h>
> #include <linux/device.h>
> #include <linux/hwmon.h>
> -#include <linux/hwmon-sysfs.h>
> #include <linux/i2c.h>
> #include <linux/math.h>
> #include <linux/minmax.h>
> @@ -541,7 +540,7 @@ static int ltc4282_read_power_byte(const struct
> ltc4282_state *st, u32 reg,
> return 0;
> }
>
> -static int ltc4282_read_energy(const struct ltc4282_state *st, u64 *val)
> +static int ltc4282_read_energy(const struct ltc4282_state *st, s64 *val)
> {
> u64 temp, energy;
> __be64 raw;
> @@ -617,6 +616,12 @@ static int ltc4282_read(struct device *dev, enum
> hwmon_sensor_types type,
> *val = st->energy_en;
> }
> return 0;
> + case hwmon_energy64:
> + scoped_guard(mutex, &st->lock) {
> + if (st->energy_en)
> + return ltc4282_read_energy(st, (s64 *)val);
> + }
> + return -ENODATA;
> default:
> return -EOPNOTSUPP;
> }
> @@ -1078,6 +1083,9 @@ static umode_t ltc4282_is_visible(const void *data,
> case hwmon_energy:
> /* hwmon_energy_enable */
> return 0644;
> + case hwmon_energy64:
> + /* hwmon_energy_input */
> + return 0444;
> default:
> return 0;
> }
> @@ -1106,24 +1114,6 @@ static int ltc4282_read_labels(struct device *dev,
> }
> }
>
> -static ssize_t ltc4282_energy_show(struct device *dev,
> - struct device_attribute *da, char *buf)
> -{
> - struct ltc4282_state *st = dev_get_drvdata(dev);
> - u64 energy;
> - int ret;
> -
> - guard(mutex)(&st->lock);
> - if (!st->energy_en)
> - return -ENODATA;
> -
> - ret = ltc4282_read_energy(st, &energy);
> - if (ret < 0)
> - return ret;
> -
> - return sysfs_emit(buf, "%llu\n", energy);
> -}
> -
> static const struct clk_ops ltc4282_ops = {
> .recalc_rate = ltc4282_recalc_rate,
> .determine_rate = ltc4282_determine_rate,
> @@ -1588,6 +1578,8 @@ static const struct hwmon_channel_info * const
> ltc4282_info[] = {
> HWMON_P_RESET_HISTORY | HWMON_P_LABEL),
> HWMON_CHANNEL_INFO(energy,
> HWMON_E_ENABLE),
> + HWMON_CHANNEL_INFO(energy64,
> + HWMON_E_INPUT),
> NULL
> };
>
> @@ -1603,15 +1595,6 @@ static const struct hwmon_chip_info ltc4282_chip_info =
> {
> .info = ltc4282_info,
> };
>
> -/* energy attributes are 6bytes wide so we need u64 */
> -static SENSOR_DEVICE_ATTR_RO(energy1_input, ltc4282_energy, 0);
> -
> -static struct attribute *ltc4282_attrs[] = {
> - &sensor_dev_attr_energy1_input.dev_attr.attr,
> - NULL
> -};
> -ATTRIBUTE_GROUPS(ltc4282);
> -
> static int ltc4282_show_fault_log(void *arg, u64 *val, u32 mask)
> {
> struct ltc4282_state *st = arg;
> @@ -1718,8 +1701,7 @@ static int ltc4282_probe(struct i2c_client *i2c)
>
> mutex_init(&st->lock);
> hwmon = devm_hwmon_device_register_with_info(dev, "ltc4282", st,
> - <c4282_chip_info,
> - ltc4282_groups);
> + <c4282_chip_info,
> NULL);
> if (IS_ERR(hwmon))
> return PTR_ERR(hwmon);
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] hwmon: (ltc2947) Use the energy64 attribute type to report the energy
2025-10-12 21:16 [PATCH 1/2] hwmon: (ltc2947) Use the energy64 attribute type to report the energy Guenter Roeck
2025-10-12 21:16 ` [PATCH 2/2] hwmon: (ltc4282) " Guenter Roeck
@ 2025-10-13 11:33 ` Nuno Sá
1 sibling, 0 replies; 4+ messages in thread
From: Nuno Sá @ 2025-10-13 11:33 UTC (permalink / raw)
To: Guenter Roeck, Hardware Monitoring
On Sun, 2025-10-12 at 14:16 -0700, Guenter Roeck wrote:
> Use the energy64 attribute type instead of a locally defined sysfs
> attribute to report the accumulated energy.
>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
> drivers/hwmon/ltc2947-core.c | 60 ++++++++++++++++--------------------
> 1 file changed, 26 insertions(+), 34 deletions(-)
>
> diff --git a/drivers/hwmon/ltc2947-core.c b/drivers/hwmon/ltc2947-core.c
> index 244839167e51..90f70f732b41 100644
> --- a/drivers/hwmon/ltc2947-core.c
> +++ b/drivers/hwmon/ltc2947-core.c
> @@ -9,8 +9,8 @@
> #include <linux/clk.h>
> #include <linux/device.h>
> #include <linux/hwmon.h>
> -#include <linux/hwmon-sysfs.h>
> #include <linux/module.h>
> +#include <linux/math64.h>
> #include <linux/mod_devicetable.h>
> #include <linux/property.h>
> #include <linux/regmap.h>
> @@ -319,24 +319,6 @@ static int ltc2947_alarm_read(struct ltc2947_data *st,
> const u8 reg,
> return ret;
> }
>
> -static ssize_t ltc2947_show_value(struct device *dev,
> - struct device_attribute *da, char *buf)
> -{
> - struct ltc2947_data *st = dev_get_drvdata(dev);
> - struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
> - int ret;
> - s64 val = 0;
> -
> - ret = ltc2947_val_read(st, attr->index, LTC2947_PAGE0, 6, &val);
> - if (ret)
> - return ret;
> -
> - /* value in microJoule. st->lsb_energy was multiplied by 10E9 */
> - val = div_s64(val * st->lsb_energy, 1000);
> -
> - return sprintf(buf, "%lld\n", val);
> -}
> -
> static int ltc2947_read_temp(struct device *dev, const u32 attr, long *val,
> const int channel)
> {
> @@ -588,6 +570,23 @@ static int ltc2947_read_in(struct device *dev, const u32
> attr, long *val,
> return 0;
> }
>
> +static int ltc2947_read_energy(struct device *dev, s64 *val, const int
> channel)
> +{
> + int reg = channel ? LTC2947_REG_ENERGY2 : LTC2947_REG_ENERGY1;
> + struct ltc2947_data *st = dev_get_drvdata(dev);
> + s64 __val = 0;
> + int ret;
> +
> + ret = ltc2947_val_read(st, reg, LTC2947_PAGE0, 6, &__val);
> + if (ret)
> + return ret;
> +
> + /* value in microJoule. st->lsb_energy was multiplied by 10E9 */
> + *val = DIV_S64_ROUND_CLOSEST(__val * st->lsb_energy, 1000);
> +
> + return 0;
> +}
> +
> static int ltc2947_read(struct device *dev, enum hwmon_sensor_types type,
> u32 attr, int channel, long *val)
> {
> @@ -600,6 +599,8 @@ static int ltc2947_read(struct device *dev, enum
> hwmon_sensor_types type,
> return ltc2947_read_power(dev, attr, val);
> case hwmon_temp:
> return ltc2947_read_temp(dev, attr, val, channel);
> + case hwmon_energy64:
> + return ltc2947_read_energy(dev, (s64 *)val, channel);
> default:
> return -ENOTSUPP;
> }
> @@ -897,6 +898,8 @@ static umode_t ltc2947_is_visible(const void *data,
> return ltc2947_power_is_visible(attr);
> case hwmon_temp:
> return ltc2947_temp_is_visible(attr);
> + case hwmon_energy64:
> + return 0444;
> default:
> return 0;
> }
> @@ -929,6 +932,9 @@ static const struct hwmon_channel_info * const
> ltc2947_info[] = {
> HWMON_T_LABEL,
> HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
> HWMON_T_MAX |
> HWMON_T_MIN | HWMON_T_LABEL),
> + HWMON_CHANNEL_INFO(energy64,
> + HWMON_E_INPUT,
> + HWMON_E_INPUT),
> NULL
> };
>
> @@ -944,19 +950,6 @@ static const struct hwmon_chip_info ltc2947_chip_info = {
> .info = ltc2947_info,
> };
>
> -/* energy attributes are 6bytes wide so we need u64 */
> -static SENSOR_DEVICE_ATTR(energy1_input, 0444, ltc2947_show_value, NULL,
> - LTC2947_REG_ENERGY1);
> -static SENSOR_DEVICE_ATTR(energy2_input, 0444, ltc2947_show_value, NULL,
> - LTC2947_REG_ENERGY2);
> -
> -static struct attribute *ltc2947_attrs[] = {
> - &sensor_dev_attr_energy1_input.dev_attr.attr,
> - &sensor_dev_attr_energy2_input.dev_attr.attr,
> - NULL,
> -};
> -ATTRIBUTE_GROUPS(ltc2947);
> -
> static int ltc2947_setup(struct ltc2947_data *st)
> {
> int ret;
> @@ -1114,8 +1107,7 @@ int ltc2947_core_probe(struct regmap *map, const char
> *name)
> return ret;
>
> hwmon = devm_hwmon_device_register_with_info(dev, name, st,
> - <c2947_chip_info,
> - ltc2947_groups);
> + <c2947_chip_info,
> NULL);
> return PTR_ERR_OR_ZERO(hwmon);
> }
> EXPORT_SYMBOL_GPL(ltc2947_core_probe);
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-10-13 11:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-12 21:16 [PATCH 1/2] hwmon: (ltc2947) Use the energy64 attribute type to report the energy Guenter Roeck
2025-10-12 21:16 ` [PATCH 2/2] hwmon: (ltc4282) " Guenter Roeck
2025-10-13 11:33 ` Nuno Sá
2025-10-13 11:33 ` [PATCH 1/2] hwmon: (ltc2947) " Nuno Sá
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox