1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023, Intel Corporation. */
6 #include "ice_adminq_cmd.h"
8 #include <linux/hwmon.h>
10 #define TEMP_FROM_REG(reg) ((reg) * 1000)
12 static const struct hwmon_channel_info *ice_hwmon_info[] = {
13 HWMON_CHANNEL_INFO(temp,
14 HWMON_T_INPUT | HWMON_T_MAX |
15 HWMON_T_CRIT | HWMON_T_EMERGENCY),
19 static int ice_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
20 u32 attr, int channel, long *val)
22 struct ice_aqc_get_sensor_reading_resp resp;
23 struct ice_pf *pf = dev_get_drvdata(dev);
26 if (type != hwmon_temp)
29 ret = ice_aq_get_sensor_reading(&pf->hw, &resp);
31 dev_warn_ratelimited(dev,
32 "%s HW read failure (%d)\n",
39 case hwmon_temp_input:
40 *val = TEMP_FROM_REG(resp.data.s0f0.temp);
43 *val = TEMP_FROM_REG(resp.data.s0f0.temp_warning_threshold);
46 *val = TEMP_FROM_REG(resp.data.s0f0.temp_critical_threshold);
48 case hwmon_temp_emergency:
49 *val = TEMP_FROM_REG(resp.data.s0f0.temp_fatal_threshold);
52 dev_dbg(dev, "%s unsupported attribute (%d)\n",
60 static umode_t ice_hwmon_is_visible(const void *data,
61 enum hwmon_sensor_types type, u32 attr,
64 if (type != hwmon_temp)
68 case hwmon_temp_input:
71 case hwmon_temp_emergency:
78 static const struct hwmon_ops ice_hwmon_ops = {
79 .is_visible = ice_hwmon_is_visible,
80 .read = ice_hwmon_read
83 static const struct hwmon_chip_info ice_chip_info = {
84 .ops = &ice_hwmon_ops,
85 .info = ice_hwmon_info
88 static bool ice_is_internal_reading_supported(struct ice_pf *pf)
90 /* Only the first PF will report temperature for a chip.
91 * Note that internal temp reading is not supported
92 * for older FW (< v4.30).
97 unsigned long sensors = pf->hw.dev_caps.supported_sensors;
99 return test_bit(ICE_SENSOR_SUPPORT_E810_INT_TEMP_BIT, &sensors);
102 void ice_hwmon_init(struct ice_pf *pf)
104 struct device *dev = ice_pf_to_dev(pf);
107 if (!ice_is_internal_reading_supported(pf))
110 hdev = hwmon_device_register_with_info(dev, "ice", pf, &ice_chip_info,
114 "hwmon_device_register_with_info returns error (%ld)",
118 pf->hwmon_dev = hdev;
121 void ice_hwmon_exit(struct ice_pf *pf)
125 hwmon_device_unregister(pf->hwmon_dev);