Delta TN48M has a CPLD that also monitors the power supply
statuses.
These are useful to be presented to the users, so lets
add a driver for HWMON part of the CPLD.
Signed-off-by: Robert Marko <robert.marko@xxxxxxxxxx>
---
drivers/hwmon/Kconfig | 10 +++
drivers/hwmon/Makefile | 1 +
drivers/hwmon/tn48m-hwmon.c | 148 ++++++++++++++++++++++++++++++++++++
drivers/mfd/tn48m-cpld.c | 3 +
include/linux/mfd/tn48m.h | 1 +
5 files changed, 163 insertions(+)
create mode 100644 drivers/hwmon/tn48m-hwmon.c
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 54f04e61fb83..89271dfeb775 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -1924,6 +1924,16 @@ config SENSORS_TMP513
This driver can also be built as a module. If so, the module
will be called tmp513.
+config SENSORS_TN48M
+ tristate "Delta Networks TN48M switch CPLD HWMON driver"
+ depends on MFD_TN48M_CPLD
+ help
+ If you say yes here you get support for Delta Networks TN48M
+ CPLD.
+
+ This driver can also be built as a module. If so, the module
+ will be called tn48m-hwmon.
+
config SENSORS_VEXPRESS
tristate "Versatile Express"
depends on VEXPRESS_CONFIG
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index fe38e8a5c979..22e470057ffe 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -187,6 +187,7 @@ obj-$(CONFIG_SENSORS_TMP108) += tmp108.o
obj-$(CONFIG_SENSORS_TMP401) += tmp401.o
obj-$(CONFIG_SENSORS_TMP421) += tmp421.o
obj-$(CONFIG_SENSORS_TMP513) += tmp513.o
+obj-$(CONFIG_SENSORS_TN48M) += tn48m-hwmon.o
obj-$(CONFIG_SENSORS_VEXPRESS) += vexpress-hwmon.o
obj-$(CONFIG_SENSORS_VIA_CPUTEMP)+= via-cputemp.o
obj-$(CONFIG_SENSORS_VIA686A) += via686a.o
diff --git a/drivers/hwmon/tn48m-hwmon.c b/drivers/hwmon/tn48m-hwmon.c
new file mode 100644
index 000000000000..80fd18d74f1d
--- /dev/null
+++ b/drivers/hwmon/tn48m-hwmon.c
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Delta TN48M CPLD HWMON driver
+ *
+ * Copyright 2020 Sartura Ltd
+ *
+ * Author: Robert Marko <robert.marko@xxxxxxxxxx>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon.h>
+#include <linux/kernel.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#include <linux/mfd/tn48m.h>
+
+#define PSU1_PRESENT_MASK BIT(0)
+#define PSU2_PRESENT_MASK BIT(1)
+#define PSU1_POWERGOOD_MASK BIT(2)
+#define PSU2_POWERGOOD_MASK BIT(3)
+#define PSU1_ALERT_MASK BIT(4)
+#define PSU2_ALERT_MASK BIT(5)
+
+static int board_id_get(struct tn48m_data *data)
+{
+ unsigned int regval;
+
+ regmap_read(data->regmap, BOARD_ID, ®val);
+
+ switch (regval) {
+ case BOARD_ID_TN48M:
+ return BOARD_ID_TN48M;
+ case BOARD_ID_TN48M_P:
+ return BOARD_ID_TN48M_P;
+ default:
+ return -EINVAL;
+ }
+}
+
+static ssize_t psu_present_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
+ struct tn48m_data *data = dev_get_drvdata(dev);
+ unsigned int regval, status;
+
+ if (board_id_get(data) == BOARD_ID_TN48M_P) {
+ regmap_read(data->regmap, attr2->nr, ®val);
+
+ if (attr2->index == 1)
+ status = !FIELD_GET(PSU1_PRESENT_MASK, regval);
+ else
+ status = !FIELD_GET(PSU2_PRESENT_MASK, regval);
+ } else
+ status = 0;
+
+ return sprintf(buf, "%d\n", status);
+}
+
+static ssize_t psu_pg_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
+ struct tn48m_data *data = dev_get_drvdata(dev);
+ unsigned int regval, status;
+
+ regmap_read(data->regmap, attr2->nr, ®val);
+
+ if (attr2->index == 1)
+ status = FIELD_GET(PSU1_POWERGOOD_MASK, regval);
+ else
+ status = FIELD_GET(PSU2_POWERGOOD_MASK, regval);
+
+ return sprintf(buf, "%d\n", status);
+}
+
+static ssize_t psu_alert_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
+ struct tn48m_data *data = dev_get_drvdata(dev);
+ unsigned int regval, status;
+
+ if (board_id_get(data) == BOARD_ID_TN48M_P) {
+ regmap_read(data->regmap, attr2->nr, ®val);
+
+ if (attr2->index == 1)
+ status = !FIELD_GET(PSU1_ALERT_MASK, regval);
+ else
+ status = !FIELD_GET(PSU2_ALERT_MASK, regval);
+ } else
+ status = 0;
+
+ return sprintf(buf, "%d\n", status);
+}
+
+static SENSOR_DEVICE_ATTR_2_RO(psu1_present, psu_present, PSU_STATUS, 1);
+static SENSOR_DEVICE_ATTR_2_RO(psu2_present, psu_present, PSU_STATUS, 2);
+static SENSOR_DEVICE_ATTR_2_RO(psu1_pg, psu_pg, PSU_STATUS, 1);
+static SENSOR_DEVICE_ATTR_2_RO(psu2_pg, psu_pg, PSU_STATUS, 2);
+static SENSOR_DEVICE_ATTR_2_RO(psu1_alert, psu_alert, PSU_STATUS, 1);
+static SENSOR_DEVICE_ATTR_2_RO(psu2_alert, psu_alert, PSU_STATUS, 2);
+
+static struct attribute *tn48m_hwmon_attrs[] = {
+ &sensor_dev_attr_psu1_present.dev_attr.attr,
+ &sensor_dev_attr_psu2_present.dev_attr.attr,
+ &sensor_dev_attr_psu1_pg.dev_attr.attr,
+ &sensor_dev_attr_psu2_pg.dev_attr.attr,
+ &sensor_dev_attr_psu1_alert.dev_attr.attr,
+ &sensor_dev_attr_psu2_alert.dev_attr.attr,