1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * cgbc-hwmon - Congatec Board Controller hardware monitoring driver 4 * 5 * Copyright (C) 2024 Thomas Richard <thomas.richard@bootlin.com> 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/device.h> 10 #include <linux/hwmon.h> 11 #include <linux/mfd/cgbc.h> 12 #include <linux/module.h> 13 #include <linux/platform_device.h> 14 15 #define CGBC_HWMON_CMD_SENSOR 0x77 16 #define CGBC_HWMON_CMD_SENSOR_DATA_SIZE 0x05 17 18 #define CGBC_HWMON_TYPE_MASK GENMASK(6, 5) 19 #define CGBC_HWMON_ID_MASK GENMASK(4, 0) 20 #define CGBC_HWMON_ACTIVE_BIT BIT(7) 21 22 struct cgbc_hwmon_sensor { 23 enum hwmon_sensor_types type; 24 bool active; 25 unsigned int index; 26 unsigned int channel; 27 const char *label; 28 }; 29 30 struct cgbc_hwmon_data { 31 struct cgbc_device_data *cgbc; 32 unsigned int nb_sensors; 33 struct cgbc_hwmon_sensor *sensors; 34 }; 35 36 enum cgbc_sensor_types { 37 CGBC_HWMON_TYPE_TEMP = 1, 38 CGBC_HWMON_TYPE_IN, 39 CGBC_HWMON_TYPE_FAN 40 }; 41 42 static const char * const cgbc_hwmon_labels_temp[] = { 43 "CPU Temperature", 44 "Box Temperature", 45 "Ambient Temperature", 46 "Board Temperature", 47 "Carrier Temperature", 48 "Chipset Temperature", 49 "Video Temperature", 50 "Other Temperature", 51 "TOPDIM Temperature", 52 "BOTTOMDIM Temperature", 53 }; 54 55 static const struct { 56 enum hwmon_sensor_types type; 57 const char *label; 58 } cgbc_hwmon_labels_in[] = { 59 { hwmon_in, "CPU Voltage" }, 60 { hwmon_in, "DC Runtime Voltage" }, 61 { hwmon_in, "DC Standby Voltage" }, 62 { hwmon_in, "CMOS Battery Voltage" }, 63 { hwmon_in, "Battery Voltage" }, 64 { hwmon_in, "AC Voltage" }, 65 { hwmon_in, "Other Voltage" }, 66 { hwmon_in, "5V Voltage" }, 67 { hwmon_in, "5V Standby Voltage" }, 68 { hwmon_in, "3V3 Voltage" }, 69 { hwmon_in, "3V3 Standby Voltage" }, 70 { hwmon_in, "VCore A Voltage" }, 71 { hwmon_in, "VCore B Voltage" }, 72 { hwmon_in, "12V Voltage" }, 73 { hwmon_curr, "DC Current" }, 74 { hwmon_curr, "5V Current" }, 75 { hwmon_curr, "12V Current" }, 76 }; 77 78 #define CGBC_HWMON_NB_IN_SENSORS 14 79 80 static const char * const cgbc_hwmon_labels_fan[] = { 81 "CPU Fan", 82 "Box Fan", 83 "Ambient Fan", 84 "Chipset Fan", 85 "Video Fan", 86 "Other Fan", 87 }; 88 89 static int cgbc_hwmon_cmd(struct cgbc_device_data *cgbc, u8 index, u8 *data) 90 { 91 u8 cmd[2] = {CGBC_HWMON_CMD_SENSOR, index}; 92 93 return cgbc_command(cgbc, cmd, sizeof(cmd), data, CGBC_HWMON_CMD_SENSOR_DATA_SIZE, NULL); 94 } 95 96 static int cgbc_hwmon_probe_sensors(struct device *dev, struct cgbc_hwmon_data *hwmon) 97 { 98 struct cgbc_device_data *cgbc = hwmon->cgbc; 99 struct cgbc_hwmon_sensor *sensor = hwmon->sensors; 100 u8 data[CGBC_HWMON_CMD_SENSOR_DATA_SIZE], nb_sensors, i; 101 int ret; 102 103 ret = cgbc_hwmon_cmd(cgbc, 0, &data[0]); 104 if (ret) 105 return ret; 106 107 nb_sensors = data[0]; 108 109 hwmon->sensors = devm_kzalloc(dev, sizeof(*hwmon->sensors) * nb_sensors, GFP_KERNEL); 110 sensor = hwmon->sensors; 111 112 for (i = 0; i < nb_sensors; i++) { 113 enum cgbc_sensor_types type; 114 unsigned int channel; 115 116 /* 117 * No need to request data for the first sensor. 118 * We got data for the first sensor when we ask the number of sensors to the Board 119 * Controller. 120 */ 121 if (i) { 122 ret = cgbc_hwmon_cmd(cgbc, i, &data[0]); 123 if (ret) 124 return ret; 125 } 126 127 type = FIELD_GET(CGBC_HWMON_TYPE_MASK, data[1]); 128 channel = FIELD_GET(CGBC_HWMON_ID_MASK, data[1]) - 1; 129 130 if (type == CGBC_HWMON_TYPE_TEMP && channel < ARRAY_SIZE(cgbc_hwmon_labels_temp)) { 131 sensor->type = hwmon_temp; 132 sensor->label = cgbc_hwmon_labels_temp[channel]; 133 } else if (type == CGBC_HWMON_TYPE_IN && 134 channel < ARRAY_SIZE(cgbc_hwmon_labels_in)) { 135 /* 136 * The Board Controller doesn't differentiate current and voltage sensors. 137 * Get the sensor type from cgbc_hwmon_labels_in[channel].type instead. 138 */ 139 sensor->type = cgbc_hwmon_labels_in[channel].type; 140 sensor->label = cgbc_hwmon_labels_in[channel].label; 141 } else if (type == CGBC_HWMON_TYPE_FAN && 142 channel < ARRAY_SIZE(cgbc_hwmon_labels_fan)) { 143 sensor->type = hwmon_fan; 144 sensor->label = cgbc_hwmon_labels_fan[channel]; 145 } else { 146 dev_warn(dev, "Board Controller returned an unknown sensor (type=%d, channel=%d), ignore it", 147 type, channel); 148 continue; 149 } 150 151 sensor->active = FIELD_GET(CGBC_HWMON_ACTIVE_BIT, data[1]); 152 sensor->channel = channel; 153 sensor->index = i; 154 sensor++; 155 hwmon->nb_sensors++; 156 } 157 158 return 0; 159 } 160 161 static struct cgbc_hwmon_sensor *cgbc_hwmon_find_sensor(struct cgbc_hwmon_data *hwmon, 162 enum hwmon_sensor_types type, int channel) 163 { 164 struct cgbc_hwmon_sensor *sensor = NULL; 165 int i; 166 167 /* 168 * The Board Controller doesn't differentiate current and voltage sensors. 169 * The channel value (from the Board Controller point of view) shall be computed for current 170 * sensors. 171 */ 172 if (type == hwmon_curr) 173 channel += CGBC_HWMON_NB_IN_SENSORS; 174 175 for (i = 0; i < hwmon->nb_sensors; i++) { 176 if (hwmon->sensors[i].type == type && hwmon->sensors[i].channel == channel) { 177 sensor = &hwmon->sensors[i]; 178 break; 179 } 180 } 181 182 return sensor; 183 } 184 185 static int cgbc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, 186 long *val) 187 { 188 struct cgbc_hwmon_data *hwmon = dev_get_drvdata(dev); 189 struct cgbc_hwmon_sensor *sensor = cgbc_hwmon_find_sensor(hwmon, type, channel); 190 struct cgbc_device_data *cgbc = hwmon->cgbc; 191 u8 data[CGBC_HWMON_CMD_SENSOR_DATA_SIZE]; 192 int ret; 193 194 ret = cgbc_hwmon_cmd(cgbc, sensor->index, &data[0]); 195 if (ret) 196 return ret; 197 198 *val = (data[3] << 8) | data[2]; 199 200 /* 201 * For the Board Controller 1lsb = 0.1 degree centigrade. 202 * Other units are as expected. 203 */ 204 if (sensor->type == hwmon_temp) 205 *val *= 100; 206 207 return 0; 208 } 209 210 static umode_t cgbc_hwmon_is_visible(const void *_data, enum hwmon_sensor_types type, u32 attr, 211 int channel) 212 { 213 struct cgbc_hwmon_data *data = (struct cgbc_hwmon_data *)_data; 214 struct cgbc_hwmon_sensor *sensor; 215 216 sensor = cgbc_hwmon_find_sensor(data, type, channel); 217 if (!sensor) 218 return 0; 219 220 return sensor->active ? 0444 : 0; 221 } 222 223 static int cgbc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr, 224 int channel, const char **str) 225 { 226 struct cgbc_hwmon_data *hwmon = dev_get_drvdata(dev); 227 struct cgbc_hwmon_sensor *sensor = cgbc_hwmon_find_sensor(hwmon, type, channel); 228 229 *str = sensor->label; 230 231 return 0; 232 } 233 234 static const struct hwmon_channel_info * const cgbc_hwmon_info[] = { 235 HWMON_CHANNEL_INFO(temp, 236 HWMON_T_INPUT | HWMON_T_LABEL, HWMON_T_INPUT | HWMON_T_LABEL, 237 HWMON_T_INPUT | HWMON_T_LABEL, HWMON_T_INPUT | HWMON_T_LABEL, 238 HWMON_T_INPUT | HWMON_T_LABEL, HWMON_T_INPUT | HWMON_T_LABEL, 239 HWMON_T_INPUT | HWMON_T_LABEL, HWMON_T_INPUT | HWMON_T_LABEL, 240 HWMON_T_INPUT | HWMON_T_LABEL, HWMON_T_INPUT | HWMON_T_LABEL), 241 HWMON_CHANNEL_INFO(in, 242 HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL, 243 HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL, 244 HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL, 245 HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL, 246 HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL, 247 HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL, 248 HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL), 249 HWMON_CHANNEL_INFO(curr, 250 HWMON_C_INPUT | HWMON_C_LABEL, HWMON_C_INPUT | HWMON_C_LABEL, 251 HWMON_C_INPUT | HWMON_C_LABEL), 252 HWMON_CHANNEL_INFO(fan, 253 HWMON_F_INPUT | HWMON_F_LABEL, HWMON_F_INPUT | HWMON_F_LABEL, 254 HWMON_F_INPUT | HWMON_F_LABEL, HWMON_F_INPUT | HWMON_F_LABEL, 255 HWMON_F_INPUT | HWMON_F_LABEL, HWMON_F_INPUT | HWMON_F_LABEL), 256 NULL 257 }; 258 259 static const struct hwmon_ops cgbc_hwmon_ops = { 260 .is_visible = cgbc_hwmon_is_visible, 261 .read = cgbc_hwmon_read, 262 .read_string = cgbc_hwmon_read_string, 263 }; 264 265 static const struct hwmon_chip_info cgbc_chip_info = { 266 .ops = &cgbc_hwmon_ops, 267 .info = cgbc_hwmon_info, 268 }; 269 270 static int cgbc_hwmon_probe(struct platform_device *pdev) 271 { 272 struct cgbc_device_data *cgbc = dev_get_drvdata(pdev->dev.parent); 273 struct device *dev = &pdev->dev; 274 struct cgbc_hwmon_data *data; 275 struct device *hwmon_dev; 276 int ret; 277 278 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 279 if (!data) 280 return -ENOMEM; 281 282 data->cgbc = cgbc; 283 284 ret = cgbc_hwmon_probe_sensors(dev, data); 285 if (ret) 286 return dev_err_probe(dev, ret, "Failed to probe sensors"); 287 288 hwmon_dev = devm_hwmon_device_register_with_info(dev, "cgbc_hwmon", data, &cgbc_chip_info, 289 NULL); 290 return PTR_ERR_OR_ZERO(hwmon_dev); 291 } 292 293 static struct platform_driver cgbc_hwmon_driver = { 294 .driver = { 295 .name = "cgbc-hwmon", 296 }, 297 .probe = cgbc_hwmon_probe, 298 }; 299 300 module_platform_driver(cgbc_hwmon_driver); 301 302 MODULE_AUTHOR("Thomas Richard <thomas.richard@bootlin.com>"); 303 MODULE_DESCRIPTION("Congatec Board Controller Hardware Monitoring Driver"); 304 MODULE_LICENSE("GPL"); 305