1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /*************************************************************************** 3 * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com> * 4 * * 5 ***************************************************************************/ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/slab.h> 12 #include <linux/jiffies.h> 13 #include <linux/platform_device.h> 14 #include <linux/hwmon.h> 15 #include <linux/err.h> 16 #include <linux/mutex.h> 17 #include "sch56xx-common.h" 18 19 #define DRVNAME "sch5627" 20 #define DEVNAME DRVNAME /* We only support one model */ 21 22 #define SCH5627_HWMON_ID 0xa5 23 #define SCH5627_COMPANY_ID 0x5c 24 #define SCH5627_PRIMARY_ID 0xa0 25 26 #define SCH5627_REG_BUILD_CODE 0x39 27 #define SCH5627_REG_BUILD_ID 0x3a 28 #define SCH5627_REG_HWMON_ID 0x3c 29 #define SCH5627_REG_HWMON_REV 0x3d 30 #define SCH5627_REG_COMPANY_ID 0x3e 31 #define SCH5627_REG_PRIMARY_ID 0x3f 32 #define SCH5627_REG_CTRL 0x40 33 34 #define SCH5627_NO_TEMPS 8 35 #define SCH5627_NO_FANS 4 36 #define SCH5627_NO_IN 5 37 38 static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = { 39 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 }; 40 static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = { 41 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 }; 42 static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = { 43 0, 0, 1, 1, 0, 0, 0, 1 }; 44 static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = { 45 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 }; 46 static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = { 47 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 }; 48 49 static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = { 50 0x2C, 0x2E, 0x30, 0x32 }; 51 static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = { 52 0x62, 0x64, 0x66, 0x68 }; 53 54 static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = { 55 0x22, 0x23, 0x24, 0x25, 0x189 }; 56 static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = { 57 0xE4, 0xE4, 0xE3, 0xE3, 0x18A }; 58 static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = { 59 1, 0, 1, 0, 1 }; 60 static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = { 61 10745, 3660, 9765, 10745, 3660 }; 62 static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = { 63 "VCC", "VTT", "VBAT", "VTR", "V_IN" }; 64 65 struct sch5627_data { 66 unsigned short addr; 67 struct sch56xx_watchdog_data *watchdog; 68 u8 control; 69 u8 temp_max[SCH5627_NO_TEMPS]; 70 u8 temp_crit[SCH5627_NO_TEMPS]; 71 u16 fan_min[SCH5627_NO_FANS]; 72 73 struct mutex update_lock; 74 unsigned long last_battery; /* In jiffies */ 75 char temp_valid; /* !=0 if following fields are valid */ 76 char fan_valid; 77 char in_valid; 78 unsigned long temp_last_updated; /* In jiffies */ 79 unsigned long fan_last_updated; 80 unsigned long in_last_updated; 81 u16 temp[SCH5627_NO_TEMPS]; 82 u16 fan[SCH5627_NO_FANS]; 83 u16 in[SCH5627_NO_IN]; 84 }; 85 86 static int sch5627_update_temp(struct sch5627_data *data) 87 { 88 int ret = 0; 89 int i, val; 90 91 mutex_lock(&data->update_lock); 92 93 /* Cache the values for 1 second */ 94 if (time_after(jiffies, data->temp_last_updated + HZ) || !data->temp_valid) { 95 for (i = 0; i < SCH5627_NO_TEMPS; i++) { 96 val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_TEMP_MSB[i], 97 SCH5627_REG_TEMP_LSN[i], 98 SCH5627_REG_TEMP_HIGH_NIBBLE[i]); 99 if (unlikely(val < 0)) { 100 ret = val; 101 goto abort; 102 } 103 data->temp[i] = val; 104 } 105 data->temp_last_updated = jiffies; 106 data->temp_valid = 1; 107 } 108 abort: 109 mutex_unlock(&data->update_lock); 110 return ret; 111 } 112 113 static int sch5627_update_fan(struct sch5627_data *data) 114 { 115 int ret = 0; 116 int i, val; 117 118 mutex_lock(&data->update_lock); 119 120 /* Cache the values for 1 second */ 121 if (time_after(jiffies, data->fan_last_updated + HZ) || !data->fan_valid) { 122 for (i = 0; i < SCH5627_NO_FANS; i++) { 123 val = sch56xx_read_virtual_reg16(data->addr, SCH5627_REG_FAN[i]); 124 if (unlikely(val < 0)) { 125 ret = val; 126 goto abort; 127 } 128 data->fan[i] = val; 129 } 130 data->fan_last_updated = jiffies; 131 data->fan_valid = 1; 132 } 133 abort: 134 mutex_unlock(&data->update_lock); 135 return ret; 136 } 137 138 static int sch5627_update_in(struct sch5627_data *data) 139 { 140 int ret = 0; 141 int i, val; 142 143 mutex_lock(&data->update_lock); 144 145 /* Trigger a Vbat voltage measurement every 5 minutes */ 146 if (time_after(jiffies, data->last_battery + 300 * HZ)) { 147 sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, data->control | 0x10); 148 data->last_battery = jiffies; 149 } 150 151 /* Cache the values for 1 second */ 152 if (time_after(jiffies, data->in_last_updated + HZ) || !data->in_valid) { 153 for (i = 0; i < SCH5627_NO_IN; i++) { 154 val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_IN_MSB[i], 155 SCH5627_REG_IN_LSN[i], 156 SCH5627_REG_IN_HIGH_NIBBLE[i]); 157 if (unlikely(val < 0)) { 158 ret = val; 159 goto abort; 160 } 161 data->in[i] = val; 162 } 163 data->in_last_updated = jiffies; 164 data->in_valid = 1; 165 } 166 abort: 167 mutex_unlock(&data->update_lock); 168 return ret; 169 } 170 171 static int sch5627_read_limits(struct sch5627_data *data) 172 { 173 int i, val; 174 175 for (i = 0; i < SCH5627_NO_TEMPS; i++) { 176 /* 177 * Note what SMSC calls ABS, is what lm_sensors calls max 178 * (aka high), and HIGH is what lm_sensors calls crit. 179 */ 180 val = sch56xx_read_virtual_reg(data->addr, 181 SCH5627_REG_TEMP_ABS[i]); 182 if (val < 0) 183 return val; 184 data->temp_max[i] = val; 185 186 val = sch56xx_read_virtual_reg(data->addr, 187 SCH5627_REG_TEMP_HIGH[i]); 188 if (val < 0) 189 return val; 190 data->temp_crit[i] = val; 191 } 192 for (i = 0; i < SCH5627_NO_FANS; i++) { 193 val = sch56xx_read_virtual_reg16(data->addr, 194 SCH5627_REG_FAN_MIN[i]); 195 if (val < 0) 196 return val; 197 data->fan_min[i] = val; 198 } 199 200 return 0; 201 } 202 203 static int reg_to_temp(u16 reg) 204 { 205 return (reg * 625) / 10 - 64000; 206 } 207 208 static int reg_to_temp_limit(u8 reg) 209 { 210 return (reg - 64) * 1000; 211 } 212 213 static int reg_to_rpm(u16 reg) 214 { 215 if (reg == 0) 216 return -EIO; 217 if (reg == 0xffff) 218 return 0; 219 220 return 5400540 / reg; 221 } 222 223 static umode_t sch5627_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, 224 int channel) 225 { 226 return 0444; 227 } 228 229 static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, 230 long *val) 231 { 232 struct sch5627_data *data = dev_get_drvdata(dev); 233 int ret; 234 235 switch (type) { 236 case hwmon_temp: 237 ret = sch5627_update_temp(data); 238 if (ret < 0) 239 return ret; 240 switch (attr) { 241 case hwmon_temp_input: 242 *val = reg_to_temp(data->temp[channel]); 243 return 0; 244 case hwmon_temp_max: 245 *val = reg_to_temp_limit(data->temp_max[channel]); 246 return 0; 247 case hwmon_temp_crit: 248 *val = reg_to_temp_limit(data->temp_crit[channel]); 249 return 0; 250 case hwmon_temp_fault: 251 *val = (data->temp[channel] == 0); 252 return 0; 253 default: 254 break; 255 } 256 break; 257 case hwmon_fan: 258 ret = sch5627_update_fan(data); 259 if (ret < 0) 260 return ret; 261 switch (attr) { 262 case hwmon_fan_input: 263 ret = reg_to_rpm(data->fan[channel]); 264 if (ret < 0) 265 return ret; 266 *val = ret; 267 return 0; 268 case hwmon_fan_min: 269 ret = reg_to_rpm(data->fan_min[channel]); 270 if (ret < 0) 271 return ret; 272 *val = ret; 273 return 0; 274 case hwmon_fan_fault: 275 *val = (data->fan[channel] == 0xffff); 276 return 0; 277 default: 278 break; 279 } 280 break; 281 case hwmon_in: 282 ret = sch5627_update_in(data); 283 if (ret < 0) 284 return ret; 285 switch (attr) { 286 case hwmon_in_input: 287 *val = DIV_ROUND_CLOSEST(data->in[channel] * SCH5627_REG_IN_FACTOR[channel], 288 10000); 289 return 0; 290 default: 291 break; 292 } 293 break; 294 default: 295 break; 296 } 297 298 return -EOPNOTSUPP; 299 } 300 301 static int sch5627_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr, 302 int channel, const char **str) 303 { 304 switch (type) { 305 case hwmon_in: 306 switch (attr) { 307 case hwmon_in_label: 308 *str = SCH5627_IN_LABELS[channel]; 309 return 0; 310 default: 311 break; 312 } 313 break; 314 default: 315 break; 316 } 317 318 return -EOPNOTSUPP; 319 } 320 321 static const struct hwmon_ops sch5627_ops = { 322 .is_visible = sch5627_is_visible, 323 .read = sch5627_read, 324 .read_string = sch5627_read_string, 325 }; 326 327 static const struct hwmon_channel_info *sch5627_info[] = { 328 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), 329 HWMON_CHANNEL_INFO(temp, 330 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, 331 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, 332 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, 333 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, 334 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, 335 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, 336 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, 337 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT 338 ), 339 HWMON_CHANNEL_INFO(fan, 340 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT, 341 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT, 342 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT, 343 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT 344 ), 345 HWMON_CHANNEL_INFO(in, 346 HWMON_I_INPUT | HWMON_I_LABEL, 347 HWMON_I_INPUT | HWMON_I_LABEL, 348 HWMON_I_INPUT | HWMON_I_LABEL, 349 HWMON_I_INPUT | HWMON_I_LABEL, 350 HWMON_I_INPUT 351 ), 352 NULL 353 }; 354 355 static const struct hwmon_chip_info sch5627_chip_info = { 356 .ops = &sch5627_ops, 357 .info = sch5627_info, 358 }; 359 360 static int sch5627_remove(struct platform_device *pdev) 361 { 362 struct sch5627_data *data = platform_get_drvdata(pdev); 363 364 if (data->watchdog) 365 sch56xx_watchdog_unregister(data->watchdog); 366 367 return 0; 368 } 369 370 static int sch5627_probe(struct platform_device *pdev) 371 { 372 struct sch5627_data *data; 373 struct device *hwmon_dev; 374 int err, build_code, build_id, hwmon_rev, val; 375 376 data = devm_kzalloc(&pdev->dev, sizeof(struct sch5627_data), 377 GFP_KERNEL); 378 if (!data) 379 return -ENOMEM; 380 381 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start; 382 mutex_init(&data->update_lock); 383 platform_set_drvdata(pdev, data); 384 385 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_HWMON_ID); 386 if (val < 0) 387 return val; 388 389 if (val != SCH5627_HWMON_ID) { 390 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon", 391 val, SCH5627_HWMON_ID); 392 return -ENODEV; 393 } 394 395 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_COMPANY_ID); 396 if (val < 0) 397 return val; 398 399 if (val != SCH5627_COMPANY_ID) { 400 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company", 401 val, SCH5627_COMPANY_ID); 402 return -ENODEV; 403 } 404 405 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PRIMARY_ID); 406 if (val < 0) 407 return val; 408 409 if (val != SCH5627_PRIMARY_ID) { 410 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary", 411 val, SCH5627_PRIMARY_ID); 412 return -ENODEV; 413 } 414 415 build_code = sch56xx_read_virtual_reg(data->addr, 416 SCH5627_REG_BUILD_CODE); 417 if (build_code < 0) 418 return build_code; 419 420 build_id = sch56xx_read_virtual_reg16(data->addr, 421 SCH5627_REG_BUILD_ID); 422 if (build_id < 0) 423 return build_id; 424 425 hwmon_rev = sch56xx_read_virtual_reg(data->addr, 426 SCH5627_REG_HWMON_REV); 427 if (hwmon_rev < 0) 428 return hwmon_rev; 429 430 val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_CTRL); 431 if (val < 0) 432 return val; 433 434 data->control = val; 435 if (!(data->control & 0x01)) { 436 pr_err("hardware monitoring not enabled\n"); 437 return -ENODEV; 438 } 439 /* Trigger a Vbat voltage measurement, so that we get a valid reading 440 the first time we read Vbat */ 441 sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, 442 data->control | 0x10); 443 data->last_battery = jiffies; 444 445 /* 446 * Read limits, we do this only once as reading a register on 447 * the sch5627 is quite expensive (and they don't change). 448 */ 449 err = sch5627_read_limits(data); 450 if (err) 451 return err; 452 453 pr_info("found %s chip at %#hx\n", DEVNAME, data->addr); 454 pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n", 455 build_code, build_id, hwmon_rev); 456 457 hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, DEVNAME, data, 458 &sch5627_chip_info, NULL); 459 if (IS_ERR(hwmon_dev)) 460 return PTR_ERR(hwmon_dev); 461 462 /* Note failing to register the watchdog is not a fatal error */ 463 data->watchdog = sch56xx_watchdog_register(&pdev->dev, data->addr, 464 (build_code << 24) | (build_id << 8) | hwmon_rev, 465 &data->update_lock, 1); 466 467 return 0; 468 } 469 470 static struct platform_driver sch5627_driver = { 471 .driver = { 472 .name = DRVNAME, 473 }, 474 .probe = sch5627_probe, 475 .remove = sch5627_remove, 476 }; 477 478 module_platform_driver(sch5627_driver); 479 480 MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver"); 481 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 482 MODULE_LICENSE("GPL"); 483