Skip to contentSkip to conversation

hwmon: (ltc2947) Use the energy64 attribute type to report the energy

PATCH In review Guenter Roeck 2 participants 4 messages Oct 12, 2025 – Oct 13, 2025

Subsystems (3)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/2 hwmon: (ltc2947) Use the energy64 attribute type to report the energy

Use the energy64 attribute type instead of a locally defined sysfs attribute to report the accumulated energy.

---
 drivers/hwmon/ltc2947-core.c | 60 ++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 34 deletions(-)
drivers/hwmon/ltc2947-core.c +26 -34
index 244839167e51..90f70f732b41 100644
@@ -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,
-						     &ltc2947_chip_info,
-						     ltc2947_groups);
+						     &ltc2947_chip_info, NULL);
 	return PTR_ERR_OR_ZERO(hwmon);
 }
 EXPORT_SYMBOL_GPL(ltc2947_core_probe);
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH 2/2 hwmon: (ltc4282) Use the energy64 attribute type to report the energy

Use the energy64 attribute type instead of a locally defined sysfs attribute to report the accumulated energy.

---
 drivers/hwmon/ltc4282.c | 44 ++++++++++++-----------------------------
 1 file changed, 13 insertions(+), 31 deletions(-)
drivers/hwmon/ltc4282.c +13 -31
index 1d664a2d7b3c..44102879694a 100644
@@ -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,
-						     &ltc4282_chip_info,
-						     ltc4282_groups);
+						     &ltc4282_chip_info, NULL);
 	if (IS_ERR(hwmon))
 		return PTR_ERR(hwmon);
 
Signature · 1 line
2.45.2
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] hwmon: (ltc4282) Use the energy64 attribute type to report the energy

Quoted 5 lines · Guenter Roeck

Use the energy64 attribute type instead of a locally defined sysfs attribute to report the accumulated energy.

---

Quoted 112 lines

 drivers/hwmon/ltc4282.c | 44 ++++++++++++-----------------------------

 1 file changed, 13 insertions(+), 31 deletions(-)
drivers/hwmon/ltc4282.c +0 -1
index 1d664a2d7b3c..44102879694a 100644
@@ -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;
  }

drivers/hwmon/ltc4282.c +1 -1
-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;

drivers/hwmon/ltc4282.c +6 -0
@@ -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;
  }

drivers/hwmon/ltc4282.c +0 -0
@@ -1078,6 +1083,9 @@ static umode_t ltc4282_is_visible(const void *data,

  case hwmon_energy:
  /* hwmon_energy_enable */
  return 0644;

drivers/hwmon/ltc4282.c +3 -0
+	case hwmon_energy64:
+		/* hwmon_energy_input */
+		return 0444;

  default:
  return 0;
  }

drivers/hwmon/ltc4282.c +0 -0
@@ -1106,24 +1114,6 @@ static int ltc4282_read_labels(struct device *dev,
  	}
  }

drivers/hwmon/ltc4282.c +0 -18
-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,

drivers/hwmon/ltc4282.c +2 -0
@@ -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
 };

drivers/hwmon/ltc4282.c +0 -0
@@ -1603,15 +1595,6 @@ static const struct hwmon_chip_info ltc4282_chip_info =
 {
  	.info = ltc4282_info,
  };

drivers/hwmon/ltc4282.c +0 -9
-/* 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;

drivers/hwmon/ltc4282.c +0 -0
@@ -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,

drivers/hwmon/ltc4282.c +1 -2
-						     &ltc4282_chip_info,
-						     ltc4282_groups);
+						     &ltc4282_chip_info,

NULL);
  if (IS_ERR(hwmon))
  return PTR_ERR(hwmon);

Reviewed-by on v1 1/2
#3 lore raw
In reply to Guenter Roeck
Quoted 5 lines · Guenter Roeck

Use the energy64 attribute type instead of a locally defined sysfs attribute to report the accumulated energy.

---

Quoted 132 lines

 drivers/hwmon/ltc2947-core.c | 60 ++++++++++++++++--------------------

 1 file changed, 26 insertions(+), 34 deletions(-)
drivers/hwmon/ltc2947-core.c +1 -1
index 244839167e51..90f70f732b41 100644
@@ -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;
  }

drivers/hwmon/ltc2947-core.c +0 -18
-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)
 {

drivers/hwmon/ltc2947-core.c +0 -0
@@ -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)

drivers/hwmon/ltc2947-core.c +16 -0
+{
+	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)
 {

drivers/hwmon/ltc2947-core.c +0 -0
@@ -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);

drivers/hwmon/ltc2947-core.c +2 -0
+	case hwmon_energy64:
+		return ltc2947_read_energy(dev, (s64 *)val, channel);

  default:
  return -ENOTSUPP;
  }

drivers/hwmon/ltc2947-core.c +0 -0
@@ -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);

drivers/hwmon/ltc2947-core.c +2 -0
+	case hwmon_energy64:
+		return 0444;

  default:
  return 0;
  }

drivers/hwmon/ltc2947-core.c +0 -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),

drivers/hwmon/ltc2947-core.c +3 -0
+	HWMON_CHANNEL_INFO(energy64,
+			   HWMON_E_INPUT,
+			   HWMON_E_INPUT),

  NULL
 };

drivers/hwmon/ltc2947-core.c +0 -0
@@ -944,19 +950,6 @@ static const struct hwmon_chip_info ltc2947_chip_info = {
  	.info = ltc2947_info,
  };

drivers/hwmon/ltc2947-core.c +0 -13
-/* 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;

drivers/hwmon/ltc2947-core.c +0 -0
@@ -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,

drivers/hwmon/ltc2947-core.c +1 -2
-						     &ltc2947_chip_info,
-						     ltc2947_groups);
+						     &ltc2947_chip_info,

NULL);
  return PTR_ERR_OR_ZERO(hwmon);
 }
 EXPORT_SYMBOL_GPL(ltc2947_core_probe);