hwmon: Add KEBA battery monitoring controller support
PATCH 1/1 hwmon: Add KEBA battery monitoring controller support
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
--- drivers/hwmon/Kconfig | 12 ++++ drivers/hwmon/Makefile | 1 + drivers/hwmon/kbatt.c | 159 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 drivers/hwmon/kbatt.c
drivers/hwmon/Kconfig +12 -0
@@ -335,6 +335,18 @@ config SENSORS_K10TEMP This driver can also be built as a module. If so, the module will be called k10temp. +config SENSORS_KBATT + tristate "KEBA battery controller support" + depends on HAS_IOMEM + depends on KEBA_CP500 || COMPILE_TEST + select AUXILIARY_BUS + help + This driver supports the battery monitoring controller found in + KEBA system FPGA devices. + + This driver can also be built as a module. If so, the module + will be called kbatt. + config SENSORS_FAM15H_POWER tristate "AMD Family 15h processor power" depends on X86 && PCI && CPU_SUP_AMD
drivers/hwmon/Makefile +1 -0
@@ -108,6 +108,7 @@ obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_JC42) += jc42.o obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o +obj-$(CONFIG_SENSORS_KBATT) += kbatt.o obj-$(CONFIG_SENSORS_LAN966X) += lan966x-hwmon.o obj-$(CONFIG_SENSORS_LENOVO_EC) += lenovo-ec-sensors.o obj-$(CONFIG_SENSORS_LINEAGE) += lineage-pem.o
drivers/hwmon/kbatt.c new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
2.39.5
Quoted 5 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
A hint that this will be instantiated from drivers/misc/keba/cp500.c would be nice.
Quoted 5 lines
Signed-off-by Gerhard Engleder <[email protected]>--- drivers/hwmon/Kconfig | 12 ++++ drivers/hwmon/Makefile | 1 + drivers/hwmon/kbatt.c | 159 +++++++++++++++++++++++++++++++++++++++++
hwmon drivers commonly have an entry in Documentation/hwmon/.
Quoted 8 lines
3 files changed, 172 insertions(+) create mode 100644 drivers/hwmon/kbatt.cdrivers/hwmon/Kconfig +0
-0
drivers/hwmon/Kconfig config SENSORS_K10TEMP · line 341
Quoted 7 lines
drivers/hwmon/Kconfig +4
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMP This driver can also be built as a module. If so, the module will be called k10temp. +config SENSORS_KBATT + tristate "KEBA battery controller support" + depends on HAS_IOMEM + depends on KEBA_CP500 || COMPILE_TEST
KEBA_CP500 already has a COMPILE_TEST variant. Duplicating it here looks unnecessary.
Then the HAS_IOMEM and AUXILIARY_BUS references can go away.
Quoted 29 lines
drivers/hwmon/Kconfig +8
-0+ select AUXILIARY_BUS + help + This driver supports the battery monitoring controller found in + KEBA system FPGA devices. + + This driver can also be built as a module. If so, the module + will be called kbatt. + config SENSORS_FAM15H_POWER tristate "AMD Family 15h processor power" depends on X86 && PCI && CPU_SUP_AMDdrivers/hwmon/Makefile +1
-0@@ -108,6 +108,7 @@ obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_JC42) += jc42.o obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o +obj-$(CONFIG_SENSORS_KBATT) += kbatt.o obj-$(CONFIG_SENSORS_LAN966X) += lan966x-hwmon.o obj-$(CONFIG_SENSORS_LENOVO_EC) += lenovo-ec-sensors.o obj-$(CONFIG_SENSORS_LINEAGE) += lineage-pem.odrivers/hwmon/kbatt.c new file +0
-0
drivers/hwmon/kbatt.c line 3
Quoted 3 lines
drivers/hwmon/kbatt.c new file +3
-0@@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) KEBA Industrial Automation Gmbh 2025
typo in "GmbH".
Normal copyright format would be:
Copyright (C) 2025 KEBA Industrial Automation GmbH
drivers/hwmon/kbatt.c line 12
Quoted 9 lines
drivers/hwmon/kbatt.c new file +9
-0+* +* Driver for KEBA battery monitoring controller FPGA IP core +*/ + +#include <linux/hwmon.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/misc/keba.h>
#include <linux/auxiliary_bus.h>
#include <linux/device.h>
#include <linux/mutex.h>
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+ +#define KBATT "kbatt" + +#define KBATT_CONTROL_REG 0x4
drivers/hwmon/kbatt.c line 26
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+#define KBATT_CONTROL_BAT_TEST 0x01 + +#define KBATT_STATUS_REG 0x8 +#define KBATT_STATUS_BAT_OK 0x01 + +#define KBATT_MAX_UPD_INTERVAL (5 * HZ) +#define KBATT_SETTLE_TIME_MS 100 + +struct kbatt { +struct keba_batt_auxdev *auxdev;
Not needed.
drivers/hwmon/kbatt.c line 30
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+/* update lock */ +struct mutex lock; +void __iomem *base; +struct device *hwmon_dev;
Not needed.
drivers/hwmon/kbatt.c line 34
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+ +bool valid; +unsigned long last_updated;/* in jiffies */ +long alarm;
bool
drivers/hwmon/kbatt.c line 42
Quoted 8 lines
drivers/hwmon/kbatt.c new file +8
-0+}; + +static long kbatt_alarm (struct kbatt *kbatt) +{ +mutex_lock (&kbatt->lock); + +if (time_after (jiffies, kbatt->last_updated +KBATT_MAX_UPD_INTERVAL ) || + !kbatt->valid) {
kbatt->valid can be removed and instead check for
!kbatt->last_updated || time_after().
drivers/hwmon/kbatt.c line 48
Quoted 6 lines
drivers/hwmon/kbatt.c new file +6
-0+/* switch load on */ +iowrite8 (KBATT_CONTROL_BAT_TEST , + kbatt->base +KBATT_CONTROL_REG ); + +/* wait some time to let things settle */ +msleep (KBATT_SETTLE_TIME_MS );
Could use the recommended fsleep():Documentation/timers/delay_sleep_functions.rst
Quoted 17 lines
drivers/hwmon/kbatt.c new file +17
-0+ +/* check battery state */ +if (ioread8 (kbatt->base +KBATT_STATUS_REG ) & +KBATT_STATUS_BAT_OK ) + kbatt->alarm =0 ; +else + kbatt->alarm =1 ; + +/* switch load off */ +iowrite8 (0 , kbatt->base +KBATT_CONTROL_REG ); + + kbatt->last_updated = jiffies; + kbatt->valid = true; + } + +mutex_unlock (&kbatt->lock); +
drivers/hwmon/kbatt.c line 75
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+return kbatt->alarm; +} + +static int kbatt_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct kbatt *kbatt =dev_get_drvdata (dev); + +if ((channel !=1 ) || (attr != hwmon_in_alarm)) +return -EOPNOTSUPP ;
This condition is never true.
Quoted 8 lines
drivers/hwmon/kbatt.c new file +8
-0+ + *val =kbatt_alarm (kbatt); + +return 0 ; +} + +static umode_t kbatt_is_visible (const void *data,enum hwmon_sensor_types type, +u32 attr,int channel)
drivers/hwmon/kbatt.c line 93
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+{ +if ((channel ==1 ) && (attr == hwmon_in_alarm)) +return 0444 ; + +return 0 ; +} + +static const struct hwmon_channel_info *kbatt_info[] = { + HWMON_CHANNEL_INFO(in, + /* 0: dummy, skipped in is_visible */
Why?
Quoted 13 lines
drivers/hwmon/kbatt.c new file +13
-0+ HWMON_I_ALARM, +/* 1: input alarm channel */ + HWMON_I_ALARM), +NULL +}; + +static const struct hwmon_ops kbatt_hwmon_ops = { + .is_visible = kbatt_is_visible, + .read = kbatt_read, +}; + +static const struct hwmon_chip_info kbatt_chip_info = { + .ops = &kbatt_hwmon_ops,
drivers/hwmon/kbatt.c line 116
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+ .info = kbatt_info, +}; + +static int kbatt_probe (struct auxiliary_device *auxdev, +const struct auxiliary_device_id *id) +{ +struct device *dev = &auxdev->dev; +struct kbatt *kbatt; + + kbatt =devm_kzalloc (dev,sizeof (struct kbatt ),GFP_KERNEL );
sizeof(*kbatt)
drivers/hwmon/kbatt.c line 120
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+if (!kbatt) +return -ENOMEM ; + kbatt->auxdev = container_of(auxdev, struct keba_batt_auxdev, auxdev); +mutex_init (&kbatt->lock);
devm_mutex_init(), check the return value.
drivers/hwmon/kbatt.c line 121
Quoted 1 line
drivers/hwmon/kbatt.c new file +1
-0+auxiliary_set_drvdata (auxdev, kbatt);
Is this needed?
drivers/hwmon/kbatt.c line 134
Quoted 13 lines
drivers/hwmon/kbatt.c new file +13
-0+ + kbatt->base =devm_ioremap_resource (dev, &kbatt->auxdev->io); +if (IS_ERR (kbatt->base)) +return PTR_ERR (kbatt->base); + + kbatt->hwmon_dev =devm_hwmon_device_register_with_info (dev,KBATT , + kbatt, + &kbatt_chip_info, +NULL ); +if (IS_ERR (kbatt->hwmon_dev)) +return PTR_ERR (kbatt->hwmon_dev); + +return 0 ;
Would be slightly shorter with 'return PTR_ERR_OR_ZERO()', but both variant are fine.
drivers/hwmon/kbatt.c line 139
Quoted 5 lines
drivers/hwmon/kbatt.c new file +5
-0+} + +static void kbatt_remove (struct auxiliary_device *auxdev) +{ +auxiliary_set_drvdata (auxdev,NULL );
Unnecessary.
Quoted 24 lines
drivers/hwmon/kbatt.c new file +20
-0+} + +static const struct auxiliary_device_id kbatt_devtype_aux[] = { + { .name ="keba.batt" }, + {} +}; +MODULE_DEVICE_TABLE (auxiliary, kbatt_devtype_aux); + +static struct auxiliary_driver kbatt_driver_aux = { + .name =KBATT , + .id_table = kbatt_devtype_aux, + .probe = kbatt_probe, + .remove = kbatt_remove, +}; +module_auxiliary_driver (kbatt_driver_aux); + +MODULE_AUTHOR ("Petar Bojanic <[email protected]>" ); +MODULE_AUTHOR ("Gerhard Engleder <[email protected]>" ); +MODULE_DESCRIPTION ("KEBA battery monitoring controller driver" ); +MODULE_LICENSE ("GPL" );Signature · 1 line
2.39.5
Quoted 9 lines · Thomas Weißschuh
Quoted 5 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
A hint that this will be instantiated from
drivers/misc/keba/cp500.cwould be nice.
I will add that hint.
Quoted 8 lines
Quoted 6 lines
Signed-off-by Gerhard Engleder <[email protected]>--- drivers/hwmon/Kconfig | 12 ++++ drivers/hwmon/Makefile | 1 + drivers/hwmon/kbatt.c | 159 +++++++++++++++++++++++++++++++++++++++++hwmon drivers commonly have an entry in
Documentation/hwmon/.
Will be added.
Quoted 11 lines
Quoted 11 lines
3 files changed, 172 insertions(+)create mode 100644
drivers/hwmon/kbatt.cdrivers/hwmon/Kconfig +0
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMP This driver can also be built as a module. If so, the module will be called k10temp.
Quoted 8 lines
drivers/hwmon/Kconfig config SENSORS_K10TEMP · line 341
Quoted 4 lines
drivers/hwmon/Kconfig +4
-0+config SENSORS_KBATT + tristate "KEBA battery controller support" + depends on HAS_IOMEM + depends on KEBA_CP500 || COMPILE_TEST
KEBA_CP500already has aCOMPILE_TESTvariant. Duplicating it here looks unnecessary.
Then theHAS_IOMEMandAUXILIARY_BUSreferences can go away.
With COMPILE_TEST here the driver can be compile tested individually. Is this property not worth it? But I can change it if needed.
Quoted 26 lines
Quoted 26 lines
drivers/hwmon/Kconfig +8
-0+ select AUXILIARY_BUS + help + This driver supports the battery monitoring controller found in + KEBA system FPGA devices. + + This driver can also be built as a module. If so, the module + will be called kbatt. + config SENSORS_FAM15H_POWER tristate "AMD Family 15h processor power" depends on X86 && PCI && CPU_SUP_AMDdrivers/hwmon/Makefile +1
-0@@ -108,6 +108,7 @@ obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_JC42) += jc42.o obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o +obj-$(CONFIG_SENSORS_KBATT) += kbatt.o obj-$(CONFIG_SENSORS_LAN966X) += lan966x-hwmon.o obj-$(CONFIG_SENSORS_LENOVO_EC) += lenovo-ec-sensors.o obj-$(CONFIG_SENSORS_LINEAGE) += lineage-pem.odrivers/hwmon/kbatt.c new file +0
-0
Quoted 8 lines
drivers/hwmon/kbatt.c line 3
Quoted 6 lines
drivers/hwmon/kbatt.c new file +3
-0@@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) KEBA Industrial Automation Gmbh 2025 typo in "GmbH".
I will fix that.
Quoted 2 lines
Normal copyright format would be:
Copyright (C) 2025 KEBA Industrial Automation GmbH
I will check the copyright format.
Quoted 4 lines
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+* +* Driver for KEBA battery monitoring controller FPGA IP core +*/ +
Quoted 8 lines
drivers/hwmon/kbatt.c line 12
Quoted 5 lines
drivers/hwmon/kbatt.c new file +5
-0+#include <linux/hwmon.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/misc/keba.h> #include <
linux/auxiliary_bus.h>
#include <linux/device.h>
Do I really have to include them explicitly? They are included indirectly with linux/misc/keba.h.
Quoted 18 lines
#include <
linux/mutex.h>Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+ +#define KBATT "kbatt" + +#define KBATT_CONTROL_REG 0x4drivers/hwmon/kbatt.c line 26
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+#define KBATT_CONTROL_BAT_TEST 0x01 + +#define KBATT_STATUS_REG 0x8 +#define KBATT_STATUS_BAT_OK 0x01 + +#define KBATT_MAX_UPD_INTERVAL (5 * HZ) +#define KBATT_SETTLE_TIME_MS 100 + +struct kbatt { +struct keba_batt_auxdev *auxdev;Not needed.
Will be fixed.
Quoted 6 lines
drivers/hwmon/kbatt.c line 30
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+/* update lock */ +struct mutex lock; +void __iomem *base; +struct device *hwmon_dev;Not needed.
Will be fixed.
Quoted 6 lines
drivers/hwmon/kbatt.c line 34
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+ +bool valid; +unsigned long last_updated;/* in jiffies */ +long alarm;bool
I choose long to match the hwmon API, hwmon_ops->read. Does it need to be bool nevertheless?
Quoted 11 lines
drivers/hwmon/kbatt.c line 42
Quoted 8 lines
drivers/hwmon/kbatt.c new file +8
-0+}; + +static long kbatt_alarm (struct kbatt *kbatt) +{ +mutex_lock (&kbatt->lock); + +if (time_after (jiffies, kbatt->last_updated +KBATT_MAX_UPD_INTERVAL ) || + !kbatt->valid) {
kbatt->validcan be removed and instead check for
!kbatt->last_updated ||time_after().
You are right. I will rework.
Quoted 9 lines
drivers/hwmon/kbatt.c line 48
Quoted 6 lines
drivers/hwmon/kbatt.c new file +6
-0+/* switch load on */ +iowrite8 (KBATT_CONTROL_BAT_TEST , + kbatt->base +KBATT_CONTROL_REG ); + +/* wait some time to let things settle */ +msleep (KBATT_SETTLE_TIME_MS );Could use the recommended
fsleep():Documentation/timers/delay_sleep_functions.rst
Thank you for the hint! According to the documentation, I would still choose the second option "Use *sleep() whenever possible", because I want to prevent unecessary hrtimer work and interrupts.
Quoted 21 lines
Quoted 21 lines
drivers/hwmon/kbatt.c new file +21
-0+ +/* check battery state */ +if (ioread8 (kbatt->base +KBATT_STATUS_REG ) & +KBATT_STATUS_BAT_OK ) + kbatt->alarm =0 ; +else + kbatt->alarm =1 ; + +/* switch load off */ +iowrite8 (0 , kbatt->base +KBATT_CONTROL_REG ); + + kbatt->last_updated = jiffies; + kbatt->valid = true; + } + +mutex_unlock (&kbatt->lock); + +return kbatt->alarm; +} + +static int kbatt_read(struct device *dev, enum hwmon_sensor_types type,
Quoted 8 lines
drivers/hwmon/kbatt.c line 75
Quoted 6 lines
drivers/hwmon/kbatt.c new file +6
-0+ u32 attr, int channel, long *val) +{ + struct kbatt *kbatt = dev_get_drvdata(dev); + +if ((channel !=1 ) || (attr != hwmon_in_alarm)) +return -EOPNOTSUPP ;This condition is never true.
I will remove that check.
Quoted 12 lines
Quoted 12 lines
drivers/hwmon/kbatt.c new file +12
-0+ + *val =kbatt_alarm (kbatt); + +return 0 ; +} + +static umode_t kbatt_is_visible (const void *data,enum hwmon_sensor_types type, +u32 attr,int channel) +{ +if ((channel ==1 ) && (attr == hwmon_in_alarm)) +return 0444 ; +
Quoted 8 lines
drivers/hwmon/kbatt.c line 93
Quoted 6 lines
drivers/hwmon/kbatt.c new file +6
-0+return 0 ; +} + +static const struct hwmon_channel_info *kbatt_info[] = { + HWMON_CHANNEL_INFO(in, + /* 0: dummy, skipped in is_visible */Why?
For compatibility reasons, as the out of tree version of the driver did start with index 1 and there is software which rely on that fact. But I'm unsure if this is a valid argument for mainline code. Guenter Roeck also commented that, so will discuss this in the other thread.
Quoted 17 lines
Quoted 17 lines
drivers/hwmon/kbatt.c new file +17
-0+HWMON_I_ALARM , +/* 1: input alarm channel */ + HWMON_I_ALARM), + NULL +}; + +static const struct hwmon_ops kbatt_hwmon_ops = { + .is_visible = kbatt_is_visible, + .read = kbatt_read, +}; + +static const struct hwmon_chip_info kbatt_chip_info = { + .ops = &kbatt_hwmon_ops, + .info = kbatt_info, +}; + +static int kbatt_probe(struct auxiliary_device *auxdev,
Quoted 8 lines
drivers/hwmon/kbatt.c line 116
Quoted 6 lines
drivers/hwmon/kbatt.c new file +6
-0+ const struct auxiliary_device_id *id) +{ +struct device *dev = &auxdev->dev; +struct kbatt *kbatt; + + kbatt =devm_kzalloc (dev,sizeof (struct kbatt ),GFP_KERNEL );
sizeof(*kbatt)
I will change that.
Quoted 6 lines
drivers/hwmon/kbatt.c line 120
Quoted 4 lines
drivers/hwmon/kbatt.c new file +4
-0+if (!kbatt) +return -ENOMEM ; + kbatt->auxdev = container_of(auxdev, struct keba_batt_auxdev, auxdev); +mutex_init (&kbatt->lock);
devm_mutex_init(), check the return value.
That is new to me. Seams to help with mutex debugging. I will use it.
Quoted 3 lines
drivers/hwmon/kbatt.c line 121
Quoted 1 line
drivers/hwmon/kbatt.c new file +1
-0+auxiliary_set_drvdata (auxdev, kbatt);Is this needed?
dev_get_drvdata() is used within kbatt_read().
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/kbatt.c new file +8
-0+ + kbatt->base =devm_ioremap_resource (dev, &kbatt->auxdev->io); +if (IS_ERR (kbatt->base)) +return PTR_ERR (kbatt->base); + + kbatt->hwmon_dev = devm_hwmon_device_register_with_info(dev, KBATT, + kbatt, + &kbatt_chip_info,
Quoted 8 lines
drivers/hwmon/kbatt.c line 134
Quoted 5 lines
drivers/hwmon/kbatt.c new file +5
-0+ NULL); +if (IS_ERR (kbatt->hwmon_dev)) +return PTR_ERR (kbatt->hwmon_dev); + +return 0 ;Would be slightly shorter with 'return
PTR_ERR_OR_ZERO()', but both variant are fine.
I will use it.
Quoted 7 lines
drivers/hwmon/kbatt.c line 139
Quoted 5 lines
drivers/hwmon/kbatt.c new file +5
-0+} + +static void kbatt_remove (struct auxiliary_device *auxdev) +{ +auxiliary_set_drvdata (auxdev,NULL );Unnecessary.
Then I can completely eliminate kbatt_remove(). I will remove it.
Quoted 24 lines
Quoted 24 lines
drivers/hwmon/kbatt.c new file +20
-0+} + +static const struct auxiliary_device_id kbatt_devtype_aux[] = { + { .name ="keba.batt" }, + {} +}; +MODULE_DEVICE_TABLE (auxiliary, kbatt_devtype_aux); + +static struct auxiliary_driver kbatt_driver_aux = { + .name =KBATT , + .id_table = kbatt_devtype_aux, + .probe = kbatt_probe, + .remove = kbatt_remove, +}; +module_auxiliary_driver (kbatt_driver_aux); + +MODULE_AUTHOR ("Petar Bojanic <[email protected]>" ); +MODULE_AUTHOR ("Gerhard Engleder <[email protected]>" ); +MODULE_DESCRIPTION ("KEBA battery monitoring controller driver" ); +MODULE_LICENSE ("GPL" );Signature · 1 line
2.39.5
Quoted 2 lines · Gerhard Engleder
Quoted 1 line · Thomas Weißschuh
On 2025-03-08 22:23:46+0100, Gerhard Engleder wrote:
<snip>
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/hwmon/Kconfig +0
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMP This driver can also be built as a module. If so, the module will be called k10temp.Quoted 8 lines
drivers/hwmon/Kconfig config SENSORS_K10TEMP · line 341
Quoted 4 lines
drivers/hwmon/Kconfig +4
-0+config SENSORS_KBATT + tristate "KEBA battery controller support" + depends on HAS_IOMEM + depends on KEBA_CP500 || COMPILE_TEST
KEBA_CP500already has aCOMPILE_TESTvariant. Duplicating it here looks unnecessary.
Then theHAS_IOMEMandAUXILIARY_BUSreferences can go away.
Quoted 2 lines
With
COMPILE_TESThere the driver can be compile tested individually. Is this property not worth it? But I can change it if needed.
COMPILE_TEST is meant to break dependencies on concrete platforms. KEBA_CP500 itself is not a platform dependency.
The platform dependencies of KERBA_CP500 are already broken through COMPILE_TEST.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/hwmon/Kconfig +8
-0+ select AUXILIARY_BUS + help + This driver supports the battery monitoring controller found in + KEBA system FPGA devices. + + This driver can also be built as a module. If so, the module + will be called kbatt. + config SENSORS_FAM15H_POWER tristate "AMD Family 15h processor power" depends on X86 && PCI && CPU_SUP_AMD
<snip>
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
+ *
+ * Driver for KEBA battery monitoring controller FPGA IP core
+ */
+Quoted 8 lines
drivers/hwmon/Kconfig
Quoted 5 lines
drivers/hwmon/Kconfig +5
-0+#include <linux/hwmon.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/misc/keba.h>#include <
linux/auxiliary_bus.h>
#include <linux/device.h>
Quoted 2 lines
Do I really have to include them explicitly? They are included indirectly with
linux/misc/keba.h.
You are using symbols from those headers in your own source code, so there is a direct dependency on them.
Quoted 1 line
Quoted 1 line
#include <
linux/mutex.h>
<snip>
Quoted 9 lines
Quoted 6 lines
drivers/hwmon/Kconfig
Quoted 4 lines
drivers/hwmon/Kconfig +4
-0+ + bool valid; + unsigned long last_updated; /* in jiffies */ + long alarm;bool
I choose long to match the hwmon API,
hwmon_ops->read. Does it need to be bool nevertheless?
hwmon_ops->read needs to deal with different kinds of attributes, most of which need proper number support. Alarm is only a bool, so the code specific to it can be simpler.
Guenter also mentioned it.
<snip>
Quoted 10 lines
Quoted 9 lines
drivers/hwmon/Kconfig
Quoted 6 lines
drivers/hwmon/Kconfig +6
-0+ /* switch load on */ + iowrite8(KBATT_CONTROL_BAT_TEST, + kbatt->base + KBATT_CONTROL_REG); + + /* wait some time to let things settle */ + msleep(KBATT_SETTLE_TIME_MS);Could use the recommended
fsleep():Documentation/timers/delay_sleep_functions.rst
Quoted 3 lines
Thank you for the hint! According to the documentation, I would still choose the second option "Use
*sleep()whenever possible", because I want to prevent unecessary hrtimer work and interrupts.
I read the docs as fsleep() being preferable.
The timer core should do the right thing to avoid unnecessary work.
<snip>
Quoted 10 lines
Quoted 5 lines
drivers/hwmon/Kconfig
Quoted 3 lines
drivers/hwmon/Kconfig +3
-0+static const struct hwmon_channel_info *kbatt_info[] = { + HWMON_CHANNEL_INFO(in, + /* 0: dummy, skipped in is_visible */Why?
For compatibility reasons, as the out of tree version of the driver did start with index 1 and there is software which rely on that fact. But I'm unsure if this is a valid argument for mainline code. Guenter Roeck also commented that, so will discuss this in the other thread.
Ack, lets' discuss with Guenter.
However I don't think it's going to fly.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/hwmon/Kconfig +5
-0+ HWMON_I_ALARM, + /* 1: input alarm channel */ + HWMON_I_ALARM), + NULL +};
<snip>
Quoted 5 lines
Quoted 3 lines
drivers/hwmon/Kconfig
Quoted 1 line
drivers/hwmon/Kconfig +1
-0+ auxiliary_set_drvdata(auxdev, kbatt);Is this needed?
dev_get_drvdata()is used withinkbatt_read().
The dev_get_drvdata() in kbatt_read(), is used on the hwmon device, not the aux device. The drvdata for that hwmon device is set in devm_hwmon_device_register_with_info() below.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/hwmon/Kconfig +9
-0+ + kbatt->base = devm_ioremap_resource(dev, &kbatt->auxdev->io); + if (IS_ERR(kbatt->base)) + return PTR_ERR(kbatt->base); + + kbatt->hwmon_dev = devm_hwmon_device_register_with_info(dev, KBATT, + kbatt, + &kbatt_chip_info, + NULL);
<snip>
Quoted 22 lines · Thomas Weißschuh
Quoted 2 lines · Gerhard Engleder
Quoted 1 line · Thomas Weißschuh
On 2025-03-08 22:23:46+0100, Gerhard Engleder wrote:
<snip>
Quoted 16 lines
Quoted 15 lines
drivers/hwmon/Kconfig config SENSORS_K10TEMP · line 340
Quoted 11 lines
drivers/hwmon/Kconfig +4
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMP This driver can also be built as a module. If so, the module will be called k10temp. +config SENSORS_KBATT + tristate "KEBA battery controller support" + depends on HAS_IOMEM + depends on KEBA_CP500 || COMPILE_TEST
KEBA_CP500already has aCOMPILE_TESTvariant. Duplicating it here looks unnecessary.
Then theHAS_IOMEMandAUXILIARY_BUSreferences can go away.
Quoted 7 lines
Quoted 2 lines
With
COMPILE_TESThere the driver can be compile tested individually. Is this property not worth it? But I can change it if needed.
COMPILE_TESTis meant to break dependencies on concrete platforms.KEBA_CP500itself is not a platform dependency.
The platform dependencies ofKERBA_CP500are already broken throughCOMPILE_TEST.
Ok, I will change it.
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
drivers/hwmon/Kconfig config SENSORS_K10TEMP · line 349
Quoted 9 lines
drivers/hwmon/Kconfig +9
-0+ * + * Driver for KEBA battery monitoring controller FPGA IP core + */ + +#include <linux/hwmon.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/misc/keba.h>#include <
linux/auxiliary_bus.h>
#include <linux/device.h>
Quoted 5 lines
Quoted 2 lines
Do I really have to include them explicitly? They are included indirectly with
linux/misc/keba.h.You are using symbols from those headers in your own source code, so there is a direct dependency on them.
I will include them.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/hwmon/Kconfig config SENSORS_K10TEMP
Quoted 4 lines
drivers/hwmon/Kconfig +4
-0+ + bool valid; + unsigned long last_updated; /* in jiffies */ + long alarm;bool
Quoted 8 lines
Quoted 2 lines
I choose long to match the hwmon API,
hwmon_ops->read. Does it need to be bool nevertheless?
hwmon_ops->readneeds to deal with different kinds of attributes, most of which need proper number support. Alarm is only a bool, so the code specific to it can be simpler.Guenter also mentioned it.
I will switch to bool.
Quoted 12 lines
<snip>
Quoted 10 lines
Quoted 9 lines
drivers/hwmon/Kconfig
Quoted 6 lines
drivers/hwmon/Kconfig +6
-0+ /* switch load on */ + iowrite8(KBATT_CONTROL_BAT_TEST, + kbatt->base + KBATT_CONTROL_REG); + + /* wait some time to let things settle */ + msleep(KBATT_SETTLE_TIME_MS);Could use the recommended
fsleep():Documentation/timers/delay_sleep_functions.rst
Quoted 6 lines
Quoted 3 lines
Thank you for the hint! According to the documentation, I would still choose the second option "Use
*sleep()whenever possible", because I want to prevent unecessary hrtimer work and interrupts.I read the docs as
fsleep()being preferable.
The timer core should do the right thing to avoid unnecessary work.
I read "Use fsleep() whenever _unsure_" and I'm sure that msleep() is sufficient and I don't need hrtimer. But in this case fsleep() will end up in msleep() anyway. I will switch to fsleep().
(...)
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
drivers/hwmon/Kconfig
Quoted 1 line
drivers/hwmon/Kconfig +1
-0+ auxiliary_set_drvdata(auxdev, kbatt);Is this needed?
dev_get_drvdata()is used withinkbatt_read().The
dev_get_drvdata()inkbatt_read(), is used on the hwmon device, not the aux device. The drvdata for that hwmon device is set indevm_hwmon_device_register_with_info()below.
I will remove it.
Thank you for your detailed review! Will make the code much simpler.
Gerhard
Guenter Roeck
maintainerQuoted 26 lines · Gerhard Engleder
Quoted 25 lines · Thomas Weißschuh
Quoted 2 lines · Gerhard Engleder
Quoted 1 line · Thomas Weißschuh
On 2025-03-08 22:23:46+0100, Gerhard Engleder wrote:
<snip>
Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
drivers/hwmon/Kconfig +0
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMPThis driver can also be built as a module. If so, the module will be called k10temp.
Quoted 8 lines
Quoted 4 lines
+config
SENSORS_KBATT
+ tristate "KEBA battery controller support"
+ depends onHAS_IOMEM
+ depends onKEBA_CP500||COMPILE_TEST
KEBA_CP500already has aCOMPILE_TESTvariant. Duplicating it here looks unnecessary.
Then theHAS_IOMEMandAUXILIARY_BUSreferences can go away.With
COMPILE_TESThere the driver can be compile tested individually. Is this property not worth it? But I can change it if needed.
Quoted 7 lines
Quoted 4 lines
COMPILE_TESTis meant to break dependencies on concrete platforms.KEBA_CP500itself is not a platform dependency.
The platform dependencies ofKERBA_CP500are already broken throughCOMPILE_TEST.Ok, I will change it.
FWIW, all those COMPILE_TEST dependencies are pointless:
drivers/i2c/busses/Kconfig: depends on KEBA_CP500 || COMPILE_TEST drivers/misc/keba/Kconfig: depends on KEBA_CP500 || COMPILE_TEST drivers/spi/Kconfig: depends on KEBA_CP500 || COMPILE_TEST
On top of that, both SPI_KSPI2 and I2C_KEBA select AUXILIARY_BUS which is equally pointless because KEBA_CP500 already does that. I2C_KEBA depends on HAS_IOMEM but I2C itself already depends on it.
It is also ... odd ... that KEBA_CP500 depends on I2C. So it isn't possible to enable any of its sub-devices without also enabling I2C. It is not immediately obvious why this would be necessary.
Guenter
Quoted 32 lines · Guenter Roeck
Quoted 31 lines · Gerhard Engleder
Quoted 29 lines · Thomas Weißschuh
Quoted 2 lines · Gerhard Engleder
Quoted 1 line · Thomas Weißschuh
On 2025-03-08 22:23:46+0100, Gerhard Engleder wrote:
<snip>
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/hwmon/Kconfig +0
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMPThis driver can also be built as a module. If so, the module will be called k10temp.
Quoted 8 lines
Quoted 4 lines
+config
SENSORS_KBATT
+ tristate "KEBA battery controller support"
+ depends onHAS_IOMEM
+ depends onKEBA_CP500||COMPILE_TEST
KEBA_CP500already has aCOMPILE_TESTvariant. Duplicating it here looks unnecessary.
Then theHAS_IOMEMandAUXILIARY_BUSreferences can go away.Quoted 2 lines
With
COMPILE_TESThere the driver can be compile tested individually. Is this property not worth it? But I can change it if needed.
COMPILE_TESTis meant to break dependencies on concrete platforms.KEBA_CP500itself is not a platform dependency.
The platform dependencies ofKERBA_CP500are already broken throughCOMPILE_TEST.
Quoted 8 lines
Quoted 2 lines
Ok, I will change it.
FWIW, all those
COMPILE_TESTdependencies are pointless:drivers/i2c/busses/Kconfig: depends on
KEBA_CP500||COMPILE_TESTdrivers/misc/keba/Kconfig: depends onKEBA_CP500||COMPILE_TESTdrivers/spi/Kconfig: depends onKEBA_CP500||COMPILE_TEST
Ok, I won't add them anymore.
Quoted 3 lines
On top of that, both
SPI_KSPI2andI2C_KEBAselectAUXILIARY_BUSwhich is equally pointless becauseKEBA_CP500already does that.I2C_KEBAdepends onHAS_IOMEMbut I2C itself already depends on it.
I'm sorry, I didn't know that Kconfig must be strictly minimized.
Quoted 3 lines
It is also ... odd ... that
KEBA_CP500depends on I2C. So it isn't possible to enable any of its sub-devices without also enabling I2C. It is not immediately obvious why this would be necessary.
The cp500 driver uses functions of the I2C subsystem to find a defined EEPROM.
Gerhard
Guenter Roeck
maintainerQuoted 6 lines · Gerhard Engleder
Quoted 5 lines
FWIW, all those
COMPILE_TESTdependencies are pointless:drivers/i2c/busses/Kconfig: depends on
KEBA_CP500||COMPILE_TESTdrivers/misc/keba/Kconfig: depends onKEBA_CP500||COMPILE_TESTdrivers/spi/Kconfig: depends onKEBA_CP500||COMPILE_TEST
Quoted 8 lines
Ok, I won't add them anymore.
Quoted 3 lines
On top of that, both
SPI_KSPI2andI2C_KEBAselectAUXILIARY_BUSwhich is equally pointless becauseKEBA_CP500already does that.I2C_KEBAdepends onHAS_IOMEMbut I2C itself already depends on it.I'm sorry, I didn't know that
Kconfigmust be strictly minimized.
It doesn't, it just has no value if it isn't, and it is to some degree misleading. Following your logic, the unnecessary COMPILE_TEST don't matter either. Of course, if there is some problem with those COMPILE_TEST dependencies in the future, and someone will drop them, there will be a bit of a surprise when the drivers are built anyway. _That_ is why one would normally avoid redundant dependencies.
Quoted 7 lines
Quoted 3 lines
It is also ... odd ... that
KEBA_CP500depends on I2C. So it isn't possible to enable any of its sub-devices without also enabling I2C. It is not immediately obvious why this would be necessary.The cp500 driver uses functions of the I2C subsystem to find a defined EEPROM.
Ok.
Thanks,
Guenter
Guenter Roeck
maintainerQuoted 6 lines · Thomas Weißschuh
Quoted 6 lines
Quoted 5 lines
drivers/hwmon/kbatt.c line 93
Quoted 3 lines
drivers/hwmon/kbatt.c new file +3
-0+static const struct hwmon_channel_info *kbatt_info[] = { + HWMON_CHANNEL_INFO(in, + /* 0: dummy, skipped in is_visible */Why?
Quoted 7 lines
Quoted 4 lines
For compatibility reasons, as the out of tree version of the driver did start with index 1 and there is software which rely on that fact. But I'm unsure if this is a valid argument for mainline code. Guenter Roeck also commented that, so will discuss this in the other thread.
Ack, lets' discuss with Guenter.
However I don't think it's going to fly.
This kind of argument is often used by those who want to implement non-standard code. Implement it out-of-tree first and then say "sorry, we have to do it, the out-of-tree code does it and our userspace depends on it". That is completely unacceptable. If that is what you want, and you are not willing to adjust your userspace code, keep your code out of tree.
On top of that, I don't even know what the attribute means. An alarm attribute is supposed to indicate that a value is out of range. The implementation suggests that this is is not the case. What is "battery ok" ? Voltage out of range ? Battery failed ? The term itself suggests that it may reflect a failure. It might be a "fault" attribute, and even that would not be a good match. I'll need to see the actual description to determine what if anything is acceptable. It will most definitely not be in1_alarm.
Guenter
Quoted 13 lines · Guenter Roeck
Quoted 11 lines · Thomas Weißschuh
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
+static const struct
hwmon_channel_info*kbatt_info[] = { + HWMON_CHANNEL_INFO(in,
+ /* 0: dummy, skipped inis_visible*/Why?
For compatibility reasons, as the out of tree version of the driver did start with index 1 and there is software which rely on that fact. But I'm unsure if this is a valid argument for mainline code. Guenter Roeck also commented that, so will discuss this in the other thread.
Quoted 8 lines
Quoted 2 lines
Ack, lets' discuss with Guenter.
However I don't think it's going to fly.This kind of argument is often used by those who want to implement non- standard code. Implement it out-of-tree first and then say "sorry, we have to do it, the out-of-tree code does it and our userspace depends on it". That is completely unacceptable. If that is what you want, and you are not willing to adjust your userspace code, keep your code out of tree.
I'm sorry that I created the impression that I don't want to change driver code and user space code. This is not the case, I'm will remove that dummy and start with 0.
Quoted 7 lines
On top of that, I don't even know what the attribute means. An alarm attribute is supposed to indicate that a value is out of range. The implementation suggests that this is is not the case. What is "battery ok" ? Voltage out of range ? Battery failed ? The term itself suggests that it may reflect a failure. It might be a "fault" attribute, and even that would not be a good match. I'll need to see the actual description to determine what if anything is acceptable. It will most definitely not be
in1_alarm.
I will try to provide a clear picture in the other thread.
Gerhard
Guenter Roeck
maintainerQuoted 7 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
Signed-off-by Gerhard Engleder <[email protected]>
Looking into the keba code, that is kind of piece by piece approach. I see a reference to fans in drivers/misc/keba/cp500.c, so I guess I'll see a fan controller driver at some point in the future. I do not support the idea of having multiple drivers for the hardware monitoring functionality of a single device.
Either case, the attribute needs to be documented.
Some more technical comments inline.
Guenter
Quoted 72 lines
--- drivers/hwmon/Kconfig | 12 ++++ drivers/hwmon/Makefile | 1 + drivers/hwmon/kbatt.c | 159 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+)create mode 100644
drivers/hwmon/kbatt.cdrivers/hwmon/Kconfig +12
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMP This driver can also be built as a module. If so, the module will be called k10temp. +config SENSORS_KBATT + tristate "KEBA battery controller support" + depends on HAS_IOMEM + depends on KEBA_CP500 || COMPILE_TEST + select AUXILIARY_BUS + help + This driver supports the battery monitoring controller found in + KEBA system FPGA devices. + + This driver can also be built as a module. If so, the module + will be called kbatt. + config SENSORS_FAM15H_POWER tristate "AMD Family 15h processor power" depends on X86 && PCI && CPU_SUP_AMDdrivers/hwmon/Makefile +1
-0@@ -108,6 +108,7 @@ obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_JC42) += jc42.o obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o +obj-$(CONFIG_SENSORS_KBATT) += kbatt.o obj-$(CONFIG_SENSORS_LAN966X) += lan966x-hwmon.o obj-$(CONFIG_SENSORS_LENOVO_EC) += lenovo-ec-sensors.o obj-$(CONFIG_SENSORS_LINEAGE) += lineage-pem.odrivers/hwmon/kbatt.c new file +24
-0@@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) KEBA Industrial Automation Gmbh 2025 +* +* Driver for KEBA battery monitoring controller FPGA IP core +*/ + +#include <linux/hwmon.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/misc/keba.h> + +#define KBATT "kbatt" + +#define KBATT_CONTROL_REG 0x4 +#define KBATT_CONTROL_BAT_TEST 0x01 + +#define KBATT_STATUS_REG 0x8 +#define KBATT_STATUS_BAT_OK 0x01 + +#define KBATT_MAX_UPD_INTERVAL (5 * HZ) +#define KBATT_SETTLE_TIME_MS 100 +
drivers/hwmon/kbatt.c line 34
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+struct kbatt { +struct keba_batt_auxdev *auxdev; +/* update lock */ +struct mutex lock; +void __iomem *base; +struct device *hwmon_dev; + +bool valid; +unsigned long last_updated;/* in jiffies */ +long alarm;
bool
Quoted 30 lines
drivers/hwmon/kbatt.c new file +30
-0+}; + +static long kbatt_alarm (struct kbatt *kbatt) +{ +mutex_lock (&kbatt->lock); + +if (time_after (jiffies, kbatt->last_updated +KBATT_MAX_UPD_INTERVAL ) || + !kbatt->valid) { +/* switch load on */ +iowrite8 (KBATT_CONTROL_BAT_TEST , + kbatt->base +KBATT_CONTROL_REG ); + +/* wait some time to let things settle */ +msleep (KBATT_SETTLE_TIME_MS ); + +/* check battery state */ +if (ioread8 (kbatt->base +KBATT_STATUS_REG ) & +KBATT_STATUS_BAT_OK ) + kbatt->alarm =0 ; +else + kbatt->alarm =1 ; + +/* switch load off */ +iowrite8 (0 , kbatt->base +KBATT_CONTROL_REG ); + + kbatt->last_updated = jiffies; + kbatt->valid = true; + } + +mutex_unlock (&kbatt->lock);
drivers/hwmon/kbatt.c line 74
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+ +return kbatt->alarm; +} + +static int kbatt_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct kbatt *kbatt =dev_get_drvdata (dev); + +if ((channel !=1 ) || (attr != hwmon_in_alarm))
Various unnecessary ( ) here and below.
Quoted 12 lines
drivers/hwmon/kbatt.c new file +12
-0+return -EOPNOTSUPP ; + + *val =kbatt_alarm (kbatt); + +return 0 ; +} + +static umode_t kbatt_is_visible (const void *data,enum hwmon_sensor_types type, +u32 attr,int channel) +{ +if ((channel ==1 ) && (attr == hwmon_in_alarm)) +return 0444 ;
drivers/hwmon/kbatt.c line 96
Quoted 10 lines
drivers/hwmon/kbatt.c new file +10
-0+ +return 0 ; +} + +static const struct hwmon_channel_info *kbatt_info[] = { +HWMON_CHANNEL_INFO (in, +/* 0: dummy, skipped in is_visible */ +HWMON_I_ALARM , +/* 1: input alarm channel */ +HWMON_I_ALARM ),
Not acceptable. The first voltage channel is channel 0, not 1.
Quoted 63 lines
drivers/hwmon/kbatt.c new file +63
-0+NULL +}; + +static const struct hwmon_ops kbatt_hwmon_ops = { + .is_visible = kbatt_is_visible, + .read = kbatt_read, +}; + +static const struct hwmon_chip_info kbatt_chip_info = { + .ops = &kbatt_hwmon_ops, + .info = kbatt_info, +}; + +static int kbatt_probe (struct auxiliary_device *auxdev, +const struct auxiliary_device_id *id) +{ +struct device *dev = &auxdev->dev; +struct kbatt *kbatt; + + kbatt =devm_kzalloc (dev,sizeof (struct kbatt ),GFP_KERNEL ); +if (!kbatt) +return -ENOMEM ; + kbatt->auxdev = container_of(auxdev, struct keba_batt_auxdev, auxdev); +mutex_init (&kbatt->lock); +auxiliary_set_drvdata (auxdev, kbatt); + + kbatt->base =devm_ioremap_resource (dev, &kbatt->auxdev->io); +if (IS_ERR (kbatt->base)) +return PTR_ERR (kbatt->base); + + kbatt->hwmon_dev =devm_hwmon_device_register_with_info (dev,KBATT , + kbatt, + &kbatt_chip_info, +NULL ); +if (IS_ERR (kbatt->hwmon_dev)) +return PTR_ERR (kbatt->hwmon_dev); + +return 0 ; +} + +static void kbatt_remove (struct auxiliary_device *auxdev) +{ +auxiliary_set_drvdata (auxdev,NULL ); +} + +static const struct auxiliary_device_id kbatt_devtype_aux[] = { + { .name ="keba.batt" }, + {} +}; +MODULE_DEVICE_TABLE (auxiliary, kbatt_devtype_aux); + +static struct auxiliary_driver kbatt_driver_aux = { + .name =KBATT , + .id_table = kbatt_devtype_aux, + .probe = kbatt_probe, + .remove = kbatt_remove, +}; +module_auxiliary_driver (kbatt_driver_aux); + +MODULE_AUTHOR ("Petar Bojanic <[email protected]>" ); +MODULE_AUTHOR ("Gerhard Engleder <[email protected]>" ); +MODULE_DESCRIPTION ("KEBA battery monitoring controller driver" ); +MODULE_LICENSE ("GPL" );
Quoted 7 lines · Guenter Roeck
Quoted 6 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
Quoted 7 lines
Quoted 1 line
Signed-off-by Gerhard Engleder <[email protected]>Looking into the keba code, that is kind of piece by piece approach. I see a reference to fans in
drivers/misc/keba/cp500.c, so I guess I'll see a fan controller driver at some point in the future. I do not support the idea of having multiple drivers for the hardware monitoring functionality of a single device.
Yes, the fan controller will follow. The cp500 driver supports multiple devices. All of them have the battery controller, but only some of them have the fan controller. Fanless devices don't have a fan controller in the FPGA. There are also devices with two fan controllers.
The battery controller and the fan controller are separate IP cores with its own 4k address range (also I2C, SPI, ...). So I see them as separate devices. There is also no guarantee if the address range of both controllers is next to each other or not.
Does that help you to see the battery controller and the fan controller as separate devices?
Quoted 1 line
Either case, the attribute needs to be documented.
You mean the documentation Documentation/hwmon/, which Thomas Weißschuh also mentioned? I will add it.
Quoted 79 lines
Some more technical comments inline.
Guenter
Quoted 75 lines
---
drivers/hwmon/Kconfig | 12 ++++
drivers/hwmon/Makefile| 1 +
drivers/hwmon/kbatt.c | 159 +++++++++++++++++++++++++++++++++++++++++3 files changed, 172 insertions(+)create mode 100644
drivers/hwmon/kbatt.cdrivers/hwmon/Kconfig +0
-0@@ -335,6 +335,18 @@ config SENSORS_K10TEMPThis driver can also be built as a module. If so, the module will be called k10temp.
drivers/hwmon/Kconfig +12
-0+config SENSORS_KBATT + tristate "KEBA battery controller support" + depends on HAS_IOMEM + depends on KEBA_CP500 || COMPILE_TEST + select AUXILIARY_BUS + help + This driver supports the battery monitoring controller found in + KEBA system FPGA devices. + + This driver can also be built as a module. If so, the module + will be called kbatt. +config
SENSORS_FAM15H_POWER
tristate "AMD Family 15h processor power"
depends on X86 && PCI &&CPU_SUP_AMDdrivers/hwmon/Makefile +0
-0@@ -108,6 +108,7 @@ obj-$(CONFIG_SENSORS_IT87) += it87.oobj-$(CONFIG_SENSORS_JC42) += jc42.o obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o +obj-$(CONFIG_SENSORS_KBATT) += kbatt.o obj-$(CONFIG_SENSORS_LAN966X) += lan966x-hwmon.o obj-$(CONFIG_SENSORS_LENOVO_EC) += lenovo-ec-sensors.o obj-$(CONFIG_SENSORS_LINEAGE) += lineage-pem.o
drivers/hwmon/kbatt.c new file +28
-0@@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) KEBA Industrial Automation Gmbh 2025 +* +* Driver for KEBA battery monitoring controller FPGA IP core +*/ + +#include <linux/hwmon.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/misc/keba.h> + +#define KBATT "kbatt" + +#define KBATT_CONTROL_REG 0x4 +#define KBATT_CONTROL_BAT_TEST 0x01 + +#define KBATT_STATUS_REG 0x8 +#define KBATT_STATUS_BAT_OK 0x01 + +#define KBATT_MAX_UPD_INTERVAL (5 * HZ) +#define KBATT_SETTLE_TIME_MS 100 + +struct kbatt { + struct keba_batt_auxdev *auxdev; + /* update lock */ + struct mutex lock;
Quoted 8 lines
drivers/hwmon/kbatt.c line 34
Quoted 6 lines
drivers/hwmon/kbatt.c new file +6
-0+ void __iomem *base; + struct device *hwmon_dev; + + bool valid; + unsigned long last_updated; /* in jiffies */ + long alarm;bool
I will change it to bool.
Quoted 35 lines
Quoted 35 lines
drivers/hwmon/kbatt.c new file +7
-0+}; + +static long kbatt_alarm (struct kbatt *kbatt) +{ + mutex_lock(&kbatt->lock); + + if (time_after(jiffies, kbatt->last_updated +
KBATT_MAX_UPD_INTERVAL) ||
+ !kbatt->valid) {
+ /* switch load on */
+ iowrite8(KBATT_CONTROL_BAT_TEST,
+kbatt->base+KBATT_CONTROL_REG);drivers/hwmon/kbatt.c new file +23
-0+ + /* wait some time to let things settle */ + msleep(KBATT_SETTLE_TIME_MS); + + /* check battery state */ + if (ioread8(kbatt->base + KBATT_STATUS_REG) & + KBATT_STATUS_BAT_OK) + kbatt->alarm = 0; + else + kbatt->alarm = 1; + + /* switch load off */ + iowrite8(0, kbatt->base + KBATT_CONTROL_REG); + + kbatt->last_updated = jiffies; + kbatt->valid = true; + } + + mutex_unlock(&kbatt->lock); + + return kbatt->alarm; +} +
Quoted 8 lines
drivers/hwmon/kbatt.c line 74
Quoted 6 lines
drivers/hwmon/kbatt.c new file +6
-0+static int kbatt_read (struct device *dev,enum hwmon_sensor_types type, + u32 attr, int channel, long *val) +{ + struct kbatt *kbatt = dev_get_drvdata(dev); + + if ((channel != 1) || (attr != hwmon_in_alarm))Various unnecessary ( ) here and below.
I will clean it up here and check the whole code for that.
Quoted 17 lines
Quoted 17 lines
+ return
-EOPNOTSUPP;drivers/hwmon/kbatt.c new file +7
-0+ + *val = kbatt_alarm(kbatt); + + return 0; +} + +static umode_t kbatt_is_visible(const void *data, enum
hwmon_sensor_typestype,
+ u32 attr, int channel)drivers/hwmon/kbatt.c new file +7
-0+{ + if ((channel == 1) && (attr == hwmon_in_alarm)) + return 0444; + + return 0; +} +
Quoted 8 lines
Quoted 6 lines
+static const struct
hwmon_channel_info*kbatt_info[] = { + HWMON_CHANNEL_INFO(in,
+ /* 0: dummy, skipped inis_visible*/ +HWMON_I_ALARM,
+ /* 1: input alarm channel */
+HWMON_I_ALARM),Not acceptable. The first voltage channel is channel 0, not 1.
I did that for compatibility reasons, as the out of tree version of the driver did start with index 1 and there is software which rely on that fact. I saw a similar dummy in ina3221.c, so I thought this is ok.
Usually out of tree code is no argument. So if it must start with 0, then I will change it.
Thank you for the review!
Gerhard
Guenter Roeck
maintainerQuoted 21 lines · Gerhard Engleder
Quoted 7 lines · Guenter Roeck
Quoted 6 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
Quoted 7 lines
Quoted 1 line
Signed-off-by Gerhard Engleder <[email protected]>Looking into the keba code, that is kind of piece by piece approach. I see a reference to fans in
drivers/misc/keba/cp500.c, so I guess I'll see a fan controller driver at some point in the future. I do not support the idea of having multiple drivers for the hardware monitoring functionality of a single device.Yes, the fan controller will follow. The cp500 driver supports multiple devices. All of them have the battery controller, but only some of them have the fan controller. Fanless devices don't have a fan controller in the FPGA. There are also devices with two fan controllers.
Quoted 8 lines
The battery controller and the fan controller are separate IP cores with its own 4k address range (also I2C, SPI, ...). So I see them as separate devices. There is also no guarantee if the address range of both controllers is next to each other or not.
Does that help you to see the battery controller and the fan controller as separate devices?
Barely. At this point I am not convinced that this should be a hwmon driver in the first place.
Quoted 5 lines
Quoted 1 line
Either case, the attribute needs to be documented.
You mean the documentation
Documentation/hwmon/, which Thomas Weißschuh also mentioned? I will add it.
Yes.
...
Quoted 5 lines
Quoted 1 line
Not acceptable. The first voltage channel is channel 0, not 1.
I did that for compatibility reasons, as the out of tree version of the driver did start with index 1 and there is software which rely on that fact. I saw a similar dummy in
ina3221.c, so I thought this is ok.
Isn't this a nice thing in the Linux kernel: You can find almost everything in there to use as precedence.
The ina3221 driver slipped this in when it was submitted and, yes, I didn't notice. When it was converted to the with_info API, it was too late to change it. That doesn't make it better, and it is most definitely not an argument to make for a new driver doing the same.
Guenter
Quoted 27 lines · Guenter Roeck
Quoted 26 lines · Gerhard Engleder
Quoted 7 lines · Guenter Roeck
Quoted 6 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
Quoted 7 lines
Quoted 1 line
Signed-off-by Gerhard Engleder <[email protected]>Looking into the keba code, that is kind of piece by piece approach. I see a reference to fans in
drivers/misc/keba/cp500.c, so I guess I'll see a fan controller driver at some point in the future. I do not support the idea of having multiple drivers for the hardware monitoring functionality of a single device.Yes, the fan controller will follow. The cp500 driver supports multiple devices. All of them have the battery controller, but only some of them have the fan controller. Fanless devices don't have a fan controller in the FPGA. There are also devices with two fan controllers.
The battery controller and the fan controller are separate IP cores with its own 4k address range (also I2C, SPI, ...). So I see them as separate devices. There is also no guarantee if the address range of both controllers is next to each other or not.
Quoted 6 lines
Quoted 3 lines
Does that help you to see the battery controller and the fan controller as separate devices?
Barely. At this point I am not convinced that this should be a hwmon driver in the first place.
Here a more detailed description, which I would add to Documentation/hwmon/ in the proper form:
The PLC devices use a coin cell battery for the RTC to keep the current time. The goal is to provide information about the coin cell state to user space. Actually the user space shall be informed that the coin cell battery is nearly empty and needs to be replaced.
The coin cell must be tested actively to get to know if its nearly empty or not. Therefore, a load is put on the coin cell and the resulting voltage is evaluated. This evaluation is done by some hard wired analog logic, which compares the voltage to a defined limit. If the voltage is above the limit, then the coin cell is assumed to be ok. If the voltage is below the limit, then the coin cell is nearly empty (or broken, or removed, ...) and shall be replaced by a new one. The battery controller allows to start the test of the coin cell and to get the result if the voltage is above or below the limit. The actual voltage is not available. Only the information if the voltage is below a limit is available.
That's why I thought a voltage alarm is a good fit. But I'm not an expert and I'm curious about your opinion.
(...)
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
Not acceptable. The first voltage channel is channel 0, not 1.
I did that for compatibility reasons, as the out of tree version of the driver did start with index 1 and there is software which rely on that fact. I saw a similar dummy in
ina3221.c, so I thought this is ok.
Quoted 7 lines
Isn't this a nice thing in the Linux kernel: You can find almost everything in there to use as precedence.
The ina3221 driver slipped this in when it was submitted and, yes, I didn't notice. When it was converted to the
with_infoAPI, it was too late to change it. That doesn't make it better, and it is most definitely not an argument to make for a new driver doing the same.
I'm sorry, I only wanted to mention where the idea comes from. I will start with channel 0.
Gerhard
Guenter Roeck
maintainerQuoted 55 lines · Gerhard Engleder
Quoted 27 lines · Guenter Roeck
Quoted 26 lines · Gerhard Engleder
Quoted 7 lines · Guenter Roeck
Quoted 6 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
Quoted 7 lines
Quoted 1 line
Signed-off-by Gerhard Engleder <[email protected]>Looking into the keba code, that is kind of piece by piece approach. I see a reference to fans in
drivers/misc/keba/cp500.c, so I guess I'll see a fan controller driver at some point in the future. I do not support the idea of having multiple drivers for the hardware monitoring functionality of a single device.Yes, the fan controller will follow. The cp500 driver supports multiple devices. All of them have the battery controller, but only some of them have the fan controller. Fanless devices don't have a fan controller in the FPGA. There are also devices with two fan controllers.
The battery controller and the fan controller are separate IP cores with its own 4k address range (also I2C, SPI, ...). So I see them as separate devices. There is also no guarantee if the address range of both controllers is next to each other or not.
Quoted 6 lines
Quoted 3 lines
Does that help you to see the battery controller and the fan controller as separate devices?
Barely. At this point I am not convinced that this should be a hwmon driver in the first place.
Here a more detailed description, which I would add to
Documentation/hwmon/in the proper form:The PLC devices use a coin cell battery for the RTC to keep the current time. The goal is to provide information about the coin cell state to user space. Actually the user space shall be informed that the coin cell battery is nearly empty and needs to be replaced.
The coin cell must be tested actively to get to know if its nearly empty or not. Therefore, a load is put on the coin cell and the resulting voltage is evaluated. This evaluation is done by some hard wired analog logic, which compares the voltage to a defined limit. If the voltage is above the limit, then the coin cell is assumed to be ok. If the voltage is below the limit, then the coin cell is nearly empty (or broken, or removed, ...) and shall be replaced by a new one. The battery controller allows to start the test of the coin cell and to get the result if the voltage is above or below the limit. The actual voltage is not available. Only the information if the voltage is below a limit is available.
Quoted 3 lines
That's why I thought a voltage alarm is a good fit. But I'm not an expert and I'm curious about your opinion.
It is ok, though in0_min_alarm would be a better fit. And, yes, please add the above to the documentation.
Given the above description, and the code itself, I'd be a bit concerned that reading the value repeatedly will cause the battery to be drain faster than necessary (otherwise it could be active all the time). If so, it might make sense to reduce the frequency of such readings to, say, once a day. From the MAX6916 datasheet:
"The MAX6916 does not constantly monitor an attached battery because such
monitoring would drastically reduce the life of the battery. As a result, the MAX6916 only tests the battery for 1s every 24hr"
Personally I'd have tried to rely on the rtc itself to read the battery status (RTC_VL_READ ioctl), but of course not every rtc supports that, and not every rtc driver supports actually reporting it. Just in case the rtc in your system does support it, and the driver doesn't (such as the above mentioned MAX6916), it might make sense to submit a patch for the rtc driver and use that mechanism, since that would be a more generic solution than relying on proprietary fpga functionality.
That is just a note; it is your call, obviously, to decide how and how often to check the battery status.
Thanks,
Guenter
Quoted 56 lines · Guenter Roeck
Quoted 55 lines · Gerhard Engleder
Quoted 27 lines · Guenter Roeck
Quoted 26 lines · Gerhard Engleder
Quoted 7 lines · Guenter Roeck
Quoted 6 lines · Gerhard Engleder
From: Gerhard Engleder <[email protected]>
The KEBA battery monitoring controller is found in the system FPGA of KEBA PLC devices. It puts a load on the coin cell battery to check the state of the battery.
Quoted 7 lines
Quoted 1 line
Signed-off-by Gerhard Engleder <[email protected]>Looking into the keba code, that is kind of piece by piece approach. I see a reference to fans in
drivers/misc/keba/cp500.c, so I guess I'll see a fan controller driver at some point in the future. I do not support the idea of having multiple drivers for the hardware monitoring functionality of a single device.Yes, the fan controller will follow. The cp500 driver supports multiple devices. All of them have the battery controller, but only some of them have the fan controller. Fanless devices don't have a fan controller in the FPGA. There are also devices with two fan controllers.
The battery controller and the fan controller are separate IP cores with its own 4k address range (also I2C, SPI, ...). So I see them as separate devices. There is also no guarantee if the address range of both controllers is next to each other or not.
Quoted 6 lines
Quoted 3 lines
Does that help you to see the battery controller and the fan controller as separate devices?
Barely. At this point I am not convinced that this should be a hwmon driver in the first place.
Here a more detailed description, which I would add to
Documentation/hwmon/in the proper form:The PLC devices use a coin cell battery for the RTC to keep the current time. The goal is to provide information about the coin cell state to user space. Actually the user space shall be informed that the coin cell battery is nearly empty and needs to be replaced.
The coin cell must be tested actively to get to know if its nearly empty or not. Therefore, a load is put on the coin cell and the resulting voltage is evaluated. This evaluation is done by some hard wired analog logic, which compares the voltage to a defined limit. If the voltage is above the limit, then the coin cell is assumed to be ok. If the voltage is below the limit, then the coin cell is nearly empty (or broken, or removed, ...) and shall be replaced by a new one. The battery controller allows to start the test of the coin cell and to get the result if the voltage is above or below the limit. The actual voltage is not available. Only the information if the voltage is below a limit is available.
Quoted 6 lines
Quoted 3 lines
That's why I thought a voltage alarm is a good fit. But I'm not an expert and I'm curious about your opinion.
It is ok, though
in0_min_alarmwould be a better fit. And, yes, please add the above to the documentation.
I will take a look at in0_min_alarm. Thank you for your advice.
Quoted 4 lines
Given the above description, and the code itself, I'd be a bit concerned that reading the value repeatedly will cause the battery to be drain faster than necessary (otherwise it could be active all the time). If so, it might make sense to reduce the frequency of such readings to, say, once a day.
You are right, this test would drain the battery too faster if done too often. This is why the test is limited to every 5 seconds and this is agreed with the electronics engineers. I will document that fact too.
Quoted 7 lines
Personally I'd have tried to rely on the rtc itself to read the battery status (
RTC_VL_READioctl), but of course not every rtc supports that, and not every rtc driver supports actually reporting it. Just in case the rtc in your system does support it, and the driver doesn't (such as the above mentioned MAX6916), it might make sense to submit a patch for the rtc driver and use that mechanism, since that would be a more generic solution than relying on proprietary fpga functionality.
That's possible for some designs yes. I will discuss that with the electronics engineer for future designs with separate RTC chip.
Thank you for your feedback!
Gerhard