hwmon: (gxp-fan-ctrl) Add GXP fan controller
authorNick Hawkins <nick.hawkins@hpe.com>
Tue, 3 Jan 2023 20:36:50 +0000 (14:36 -0600)
committerGuenter Roeck <linux@roeck-us.net>
Fri, 3 Feb 2023 15:30:09 +0000 (07:30 -0800)
The GXP SoC can support up to 16 fans through the interface provided by
the CPLD. The current support is limited to 8 fans. The fans speeds are
controlled via 8 different PWMs which can vary in value from  0-255. The
fans are also capable of reporting if they have failed to the CPLD which
in turn reports the status to the GXP SoC.

Signed-off-by: Nick Hawkins <nick.hawkins@hpe.com>
Link: https://lore.kernel.org/r/20230103203654.59322-2-nick.hawkins@hpe.com
[groeck: Improved alignment of defined, added missing include linux/bits.h]
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Documentation/hwmon/gxp-fan-ctrl.rst [new file with mode: 0644]
Documentation/hwmon/index.rst
drivers/hwmon/Kconfig
drivers/hwmon/Makefile
drivers/hwmon/gxp-fan-ctrl.c [new file with mode: 0644]

diff --git a/Documentation/hwmon/gxp-fan-ctrl.rst b/Documentation/hwmon/gxp-fan-ctrl.rst
new file mode 100644 (file)
index 0000000..ae3397e
--- /dev/null
@@ -0,0 +1,28 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Kernel driver gxp-fan-ctrl
+==========================
+
+Supported chips:
+
+  * HPE GXP SOC
+
+Author: Nick Hawkins <nick.hawkins@hpe.com>
+
+
+Description
+-----------
+
+gxp-fan-ctrl is a driver which provides fan control for the hpe gxp soc.
+The driver allows the gathering of fan status and the use of fan
+PWM control.
+
+
+Sysfs attributes
+----------------
+
+======================= ===========================================================
+pwm[0-7]               Fan 0 to 7 respective PWM value (0-255)
+fan[0-7]_fault         Fan 0 to 7 respective fault status: 1 fail, 0 ok
+fan[0-7]_enable         Fan 0 to 7 respective enabled status: 1 enabled, 0 disabled
+======================= ===========================================================
index fe2cc6b..7b36b08 100644 (file)
@@ -73,6 +73,7 @@ Hardware Monitoring Kernel Drivers
    g762
    gsc-hwmon
    gl518sm
+   gxp-fan-ctrl
    hih6130
    ibmaem
    ibm-cffps
index 3176c33..9c55b29 100644 (file)
@@ -714,6 +714,15 @@ config SENSORS_GPIO_FAN
          This driver can also be built as a module. If so, the module
          will be called gpio-fan.
 
+config SENSORS_GXP_FAN_CTRL
+       tristate "HPE GXP fan controller"
+       depends on ARCH_HPE_GXP || COMPILE_TEST
+       help
+         If you say yes here you get support for GXP fan control functionality.
+
+         The GXP controls fan function via the CPLD through the use of PWM
+         registers. This driver reports status and pwm setting of the fans.
+
 config SENSORS_HIH6130
        tristate "Honeywell Humidicon HIH-6130 humidity/temperature sensor"
        depends on I2C
index e2e4e87..dfea582 100644 (file)
@@ -83,6 +83,7 @@ obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o
 obj-$(CONFIG_SENSORS_GL520SM)  += gl520sm.o
 obj-$(CONFIG_SENSORS_GSC)      += gsc-hwmon.o
 obj-$(CONFIG_SENSORS_GPIO_FAN) += gpio-fan.o
+obj-$(CONFIG_SENSORS_GXP_FAN_CTRL) += gxp-fan-ctrl.o
 obj-$(CONFIG_SENSORS_HIH6130)  += hih6130.o
 obj-$(CONFIG_SENSORS_ULTRA45)  += ultra45_env.o
 obj-$(CONFIG_SENSORS_I5500)    += i5500_temp.o
diff --git a/drivers/hwmon/gxp-fan-ctrl.c b/drivers/hwmon/gxp-fan-ctrl.c
new file mode 100644 (file)
index 0000000..0e9225a
--- /dev/null
@@ -0,0 +1,255 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (C) 2022 Hewlett-Packard Enterprise Development Company, L.P. */
+
+#include <linux/bits.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+
+#define OFS_FAN_INST   0 /* Is 0 because plreg base will be set at INST */
+#define OFS_FAN_FAIL   2 /* Is 2 bytes after base */
+#define OFS_SEVSTAT    0 /* Is 0 because fn2 base will be set at SEVSTAT */
+#define POWER_BIT      24
+
+struct gxp_fan_ctrl_drvdata {
+       void __iomem    *base;
+       void __iomem    *plreg;
+       void __iomem    *fn2;
+};
+
+static bool fan_installed(struct device *dev, int fan)
+{
+       struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
+       u8 val;
+
+       val = readb(drvdata->plreg + OFS_FAN_INST);
+
+       return !!(val & BIT(fan));
+}
+
+static long fan_failed(struct device *dev, int fan)
+{
+       struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
+       u8 val;
+
+       val = readb(drvdata->plreg + OFS_FAN_FAIL);
+
+       return !!(val & BIT(fan));
+}
+
+static long fan_enabled(struct device *dev, int fan)
+{
+       struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
+       u32 val;
+
+       /*
+        * Check the power status as if the platform is off the value
+        * reported for the PWM will be incorrect. Report fan as
+        * disabled.
+        */
+       val = readl(drvdata->fn2 + OFS_SEVSTAT);
+
+       return !!((val & BIT(POWER_BIT)) && fan_installed(dev, fan));
+}
+
+static int gxp_pwm_write(struct device *dev, u32 attr, int channel, long val)
+{
+       struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
+
+       switch (attr) {
+       case hwmon_pwm_input:
+               if (val > 255 || val < 0)
+                       return -EINVAL;
+               writeb(val, drvdata->base + channel);
+               return 0;
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
+static int gxp_fan_ctrl_write(struct device *dev, enum hwmon_sensor_types type,
+                             u32 attr, int channel, long val)
+{
+       switch (type) {
+       case hwmon_pwm:
+               return gxp_pwm_write(dev, attr, channel, val);
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
+static int gxp_fan_read(struct device *dev, u32 attr, int channel, long *val)
+{
+       switch (attr) {
+       case hwmon_fan_enable:
+               *val = fan_enabled(dev, channel);
+               return 0;
+       case hwmon_fan_fault:
+               *val = fan_failed(dev, channel);
+               return 0;
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
+static int gxp_pwm_read(struct device *dev, u32 attr, int channel, long *val)
+{
+       struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
+       u32 reg;
+
+       /*
+        * Check the power status of the platform. If the platform is off
+        * the value reported for the PWM will be incorrect. In this case
+        * report a PWM of zero.
+        */
+
+       reg = readl(drvdata->fn2 + OFS_SEVSTAT);
+
+       if (reg & BIT(POWER_BIT))
+               *val = fan_installed(dev, channel) ? readb(drvdata->base + channel) : 0;
+       else
+               *val = 0;
+
+       return 0;
+}
+
+static int gxp_fan_ctrl_read(struct device *dev, enum hwmon_sensor_types type,
+                            u32 attr, int channel, long *val)
+{
+       switch (type) {
+       case hwmon_fan:
+               return gxp_fan_read(dev, attr, channel, val);
+       case hwmon_pwm:
+               return gxp_pwm_read(dev, attr, channel, val);
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
+static umode_t gxp_fan_ctrl_is_visible(const void *_data,
+                                      enum hwmon_sensor_types type,
+                                      u32 attr, int channel)
+{
+       umode_t mode = 0;
+
+       switch (type) {
+       case hwmon_fan:
+               switch (attr) {
+               case hwmon_fan_enable:
+               case hwmon_fan_fault:
+                       mode = 0444;
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case hwmon_pwm:
+               switch (attr) {
+               case hwmon_pwm_input:
+                       mode = 0644;
+                       break;
+               default:
+                       break;
+               }
+               break;
+       default:
+               break;
+       }
+
+       return mode;
+}
+
+static const struct hwmon_ops gxp_fan_ctrl_ops = {
+       .is_visible = gxp_fan_ctrl_is_visible,
+       .read = gxp_fan_ctrl_read,
+       .write = gxp_fan_ctrl_write,
+};
+
+static const struct hwmon_channel_info *gxp_fan_ctrl_info[] = {
+       HWMON_CHANNEL_INFO(fan,
+                          HWMON_F_FAULT | HWMON_F_ENABLE,
+                          HWMON_F_FAULT | HWMON_F_ENABLE,
+                          HWMON_F_FAULT | HWMON_F_ENABLE,
+                          HWMON_F_FAULT | HWMON_F_ENABLE,
+                          HWMON_F_FAULT | HWMON_F_ENABLE,
+                          HWMON_F_FAULT | HWMON_F_ENABLE,
+                          HWMON_F_FAULT | HWMON_F_ENABLE,
+                          HWMON_F_FAULT | HWMON_F_ENABLE),
+       HWMON_CHANNEL_INFO(pwm,
+                          HWMON_PWM_INPUT,
+                          HWMON_PWM_INPUT,
+                          HWMON_PWM_INPUT,
+                          HWMON_PWM_INPUT,
+                          HWMON_PWM_INPUT,
+                          HWMON_PWM_INPUT,
+                          HWMON_PWM_INPUT,
+                          HWMON_PWM_INPUT),
+       NULL
+};
+
+static const struct hwmon_chip_info gxp_fan_ctrl_chip_info = {
+       .ops = &gxp_fan_ctrl_ops,
+       .info = gxp_fan_ctrl_info,
+
+};
+
+static int gxp_fan_ctrl_probe(struct platform_device *pdev)
+{
+       struct gxp_fan_ctrl_drvdata *drvdata;
+       struct resource *res;
+       struct device *dev = &pdev->dev;
+       struct device *hwmon_dev;
+
+       drvdata = devm_kzalloc(dev, sizeof(struct gxp_fan_ctrl_drvdata),
+                              GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       drvdata->base = devm_ioremap_resource(&pdev->dev, res);
+       if (IS_ERR(drvdata->base))
+               return dev_err_probe(dev, PTR_ERR(drvdata->base),
+                                    "failed to map base\n");
+
+       drvdata->plreg = devm_platform_ioremap_resource_byname(pdev,
+                                                              "pl");
+       if (IS_ERR(drvdata->plreg))
+               return dev_err_probe(dev, PTR_ERR(drvdata->plreg),
+                                    "failed to map plreg\n");
+
+       drvdata->fn2 = devm_platform_ioremap_resource_byname(pdev,
+                                                            "fn2");
+       if (IS_ERR(drvdata->fn2))
+               return dev_err_probe(dev, PTR_ERR(drvdata->fn2),
+                                    "failed to map fn2\n");
+
+       hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
+                                                        "hpe_gxp_fan_ctrl",
+                                                        drvdata,
+                                                        &gxp_fan_ctrl_chip_info,
+                                                        NULL);
+
+       return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static const struct of_device_id gxp_fan_ctrl_of_match[] = {
+       { .compatible = "hpe,gxp-fan-ctrl", },
+       {},
+};
+MODULE_DEVICE_TABLE(of, gxp_fan_ctrl_of_match);
+
+static struct platform_driver gxp_fan_ctrl_driver = {
+       .probe          = gxp_fan_ctrl_probe,
+       .driver = {
+               .name   = "gxp-fan-ctrl",
+               .of_match_table = gxp_fan_ctrl_of_match,
+       },
+};
+module_platform_driver(gxp_fan_ctrl_driver);
+
+MODULE_AUTHOR("Nick Hawkins <nick.hawkins@hpe.com>");
+MODULE_DESCRIPTION("HPE GXP fan controller");
+MODULE_LICENSE("GPL");