Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
0002 /* Copyright (C) 2017 Netronome Systems, Inc. */
0003 
0004 #include <linux/kernel.h>
0005 #include <linux/bitops.h>
0006 #include <linux/hwmon.h>
0007 
0008 #include "nfpcore/nfp_cpp.h"
0009 #include "nfpcore/nfp_nsp.h"
0010 #include "nfp_main.h"
0011 
0012 #define NFP_TEMP_MAX        (95 * 1000)
0013 #define NFP_TEMP_CRIT       (105 * 1000)
0014 
0015 #define NFP_POWER_MAX       (25 * 1000 * 1000)
0016 
0017 static int nfp_hwmon_sensor_id(enum hwmon_sensor_types type, int channel)
0018 {
0019     if (type == hwmon_temp)
0020         return NFP_SENSOR_CHIP_TEMPERATURE;
0021     if (type == hwmon_power)
0022         return NFP_SENSOR_ASSEMBLY_POWER + channel;
0023     return -EINVAL;
0024 }
0025 
0026 static int
0027 nfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
0028            int channel, long *val)
0029 {
0030     static const struct {
0031         enum hwmon_sensor_types type;
0032         u32 attr;
0033         long val;
0034     } const_vals[] = {
0035         { hwmon_temp,   hwmon_temp_max,     NFP_TEMP_MAX },
0036         { hwmon_temp,   hwmon_temp_crit,    NFP_TEMP_CRIT },
0037         { hwmon_power,  hwmon_power_max,    NFP_POWER_MAX },
0038     };
0039     struct nfp_pf *pf = dev_get_drvdata(dev);
0040     enum nfp_nsp_sensor_id id;
0041     int err, i;
0042 
0043     for (i = 0; i < ARRAY_SIZE(const_vals); i++)
0044         if (const_vals[i].type == type && const_vals[i].attr == attr) {
0045             *val = const_vals[i].val;
0046             return 0;
0047         }
0048 
0049     err = nfp_hwmon_sensor_id(type, channel);
0050     if (err < 0)
0051         return err;
0052     id = err;
0053 
0054     if (!(pf->nspi->sensor_mask & BIT(id)))
0055         return -EOPNOTSUPP;
0056 
0057     if (type == hwmon_temp && attr == hwmon_temp_input)
0058         return nfp_hwmon_read_sensor(pf->cpp, id, val);
0059     if (type == hwmon_power && attr == hwmon_power_input)
0060         return nfp_hwmon_read_sensor(pf->cpp, id, val);
0061 
0062     return -EINVAL;
0063 }
0064 
0065 static umode_t
0066 nfp_hwmon_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
0067              int channel)
0068 {
0069     if (type == hwmon_temp) {
0070         switch (attr) {
0071         case hwmon_temp_input:
0072         case hwmon_temp_crit:
0073         case hwmon_temp_max:
0074             return 0444;
0075         }
0076     } else if (type == hwmon_power) {
0077         switch (attr) {
0078         case hwmon_power_input:
0079         case hwmon_power_max:
0080             return 0444;
0081         }
0082     }
0083     return 0;
0084 }
0085 
0086 static u32 nfp_chip_config[] = {
0087     HWMON_C_REGISTER_TZ,
0088     0
0089 };
0090 
0091 static const struct hwmon_channel_info nfp_chip = {
0092     .type = hwmon_chip,
0093     .config = nfp_chip_config,
0094 };
0095 
0096 static u32 nfp_temp_config[] = {
0097     HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT,
0098     0
0099 };
0100 
0101 static const struct hwmon_channel_info nfp_temp = {
0102     .type = hwmon_temp,
0103     .config = nfp_temp_config,
0104 };
0105 
0106 static u32 nfp_power_config[] = {
0107     HWMON_P_INPUT | HWMON_P_MAX,
0108     HWMON_P_INPUT,
0109     HWMON_P_INPUT,
0110     0
0111 };
0112 
0113 static const struct hwmon_channel_info nfp_power = {
0114     .type = hwmon_power,
0115     .config = nfp_power_config,
0116 };
0117 
0118 static const struct hwmon_channel_info *nfp_hwmon_info[] = {
0119     &nfp_chip,
0120     &nfp_temp,
0121     &nfp_power,
0122     NULL
0123 };
0124 
0125 static const struct hwmon_ops nfp_hwmon_ops = {
0126     .is_visible = nfp_hwmon_is_visible,
0127     .read = nfp_hwmon_read,
0128 };
0129 
0130 static const struct hwmon_chip_info nfp_chip_info = {
0131     .ops = &nfp_hwmon_ops,
0132     .info = nfp_hwmon_info,
0133 };
0134 
0135 int nfp_hwmon_register(struct nfp_pf *pf)
0136 {
0137     if (!IS_REACHABLE(CONFIG_HWMON))
0138         return 0;
0139 
0140     if (!pf->nspi) {
0141         nfp_warn(pf->cpp, "not registering HWMON (no NSP info)\n");
0142         return 0;
0143     }
0144     if (!pf->nspi->sensor_mask) {
0145         nfp_info(pf->cpp,
0146              "not registering HWMON (NSP doesn't report sensors)\n");
0147         return 0;
0148     }
0149 
0150     pf->hwmon_dev = hwmon_device_register_with_info(&pf->pdev->dev, "nfp",
0151                             pf, &nfp_chip_info,
0152                             NULL);
0153     return PTR_ERR_OR_ZERO(pf->hwmon_dev);
0154 }
0155 
0156 void nfp_hwmon_unregister(struct nfp_pf *pf)
0157 {
0158     if (!IS_REACHABLE(CONFIG_HWMON) || !pf->hwmon_dev)
0159         return;
0160 
0161     hwmon_device_unregister(pf->hwmon_dev);
0162 }