1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * max6639.c - Support for Maxim MAX6639 4 * 5 * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller 6 * 7 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de> 8 * 9 * based on the initial MAX6639 support from semptian.net 10 * by He Changqing <hechangqing@semptian.com> 11 */ 12 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/slab.h> 16 #include <linux/jiffies.h> 17 #include <linux/i2c.h> 18 #include <linux/hwmon.h> 19 #include <linux/hwmon-sysfs.h> 20 #include <linux/err.h> 21 #include <linux/mutex.h> 22 #include <linux/platform_data/max6639.h> 23 #include <linux/regmap.h> 24 25 /* Addresses to scan */ 26 static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; 27 28 /* The MAX6639 registers, valid channel numbers: 0, 1 */ 29 #define MAX6639_REG_TEMP(ch) (0x00 + (ch)) 30 #define MAX6639_REG_STATUS 0x02 31 #define MAX6639_REG_OUTPUT_MASK 0x03 32 #define MAX6639_REG_GCONFIG 0x04 33 #define MAX6639_REG_TEMP_EXT(ch) (0x05 + (ch)) 34 #define MAX6639_REG_ALERT_LIMIT(ch) (0x08 + (ch)) 35 #define MAX6639_REG_OT_LIMIT(ch) (0x0A + (ch)) 36 #define MAX6639_REG_THERM_LIMIT(ch) (0x0C + (ch)) 37 #define MAX6639_REG_FAN_CONFIG1(ch) (0x10 + (ch) * 4) 38 #define MAX6639_REG_FAN_CONFIG2a(ch) (0x11 + (ch) * 4) 39 #define MAX6639_REG_FAN_CONFIG2b(ch) (0x12 + (ch) * 4) 40 #define MAX6639_REG_FAN_CONFIG3(ch) (0x13 + (ch) * 4) 41 #define MAX6639_REG_FAN_CNT(ch) (0x20 + (ch)) 42 #define MAX6639_REG_TARGET_CNT(ch) (0x22 + (ch)) 43 #define MAX6639_REG_FAN_PPR(ch) (0x24 + (ch)) 44 #define MAX6639_REG_TARGTDUTY(ch) (0x26 + (ch)) 45 #define MAX6639_REG_FAN_START_TEMP(ch) (0x28 + (ch)) 46 #define MAX6639_REG_DEVID 0x3D 47 #define MAX6639_REG_MANUID 0x3E 48 #define MAX6639_REG_DEVREV 0x3F 49 50 /* Register bits */ 51 #define MAX6639_GCONFIG_STANDBY 0x80 52 #define MAX6639_GCONFIG_POR 0x40 53 #define MAX6639_GCONFIG_DISABLE_TIMEOUT 0x20 54 #define MAX6639_GCONFIG_CH2_LOCAL 0x10 55 #define MAX6639_GCONFIG_PWM_FREQ_HI 0x08 56 57 #define MAX6639_FAN_CONFIG1_PWM 0x80 58 59 #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40 60 61 #define MAX6639_NUM_CHANNELS 2 62 63 static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; 64 65 #define FAN_FROM_REG(val, rpm_range) ((val) == 0 || (val) == 255 ? \ 66 0 : (rpm_ranges[rpm_range] * 30) / (val)) 67 #define TEMP_LIMIT_TO_REG(val) clamp_val((val) / 1000, 0, 255) 68 69 /* 70 * Client data (each client gets its own) 71 */ 72 struct max6639_data { 73 struct regmap *regmap; 74 75 /* Register values initialized only once */ 76 u8 ppr; /* Pulses per rotation 0..3 for 1..4 ppr */ 77 u8 rpm_range; /* Index in above rpm_ranges table */ 78 79 /* Optional regulator for FAN supply */ 80 struct regulator *reg; 81 }; 82 83 static ssize_t temp_input_show(struct device *dev, 84 struct device_attribute *dev_attr, char *buf) 85 { 86 long temp; 87 struct max6639_data *data = dev_get_drvdata(dev); 88 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 89 unsigned int val; 90 int res; 91 92 /* 93 * Lock isn't needed as MAX6639_REG_TEMP wpnt change for at least 250ms after reading 94 * MAX6639_REG_TEMP_EXT 95 */ 96 res = regmap_read(data->regmap, MAX6639_REG_TEMP_EXT(attr->index), &val); 97 if (res < 0) 98 return res; 99 100 temp = val >> 5; 101 res = regmap_read(data->regmap, MAX6639_REG_TEMP(attr->index), &val); 102 if (res < 0) 103 return res; 104 105 temp |= val << 3; 106 temp *= 125; 107 108 return sprintf(buf, "%ld\n", temp); 109 } 110 111 static ssize_t temp_fault_show(struct device *dev, 112 struct device_attribute *dev_attr, char *buf) 113 { 114 struct max6639_data *data = dev_get_drvdata(dev); 115 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 116 unsigned int val; 117 int res; 118 119 res = regmap_read(data->regmap, MAX6639_REG_TEMP_EXT(attr->index), &val); 120 if (res < 0) 121 return res; 122 123 return sprintf(buf, "%d\n", val & 1); 124 } 125 126 static ssize_t temp_max_show(struct device *dev, 127 struct device_attribute *dev_attr, char *buf) 128 { 129 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 130 struct max6639_data *data = dev_get_drvdata(dev); 131 unsigned int val; 132 int res; 133 134 res = regmap_read(data->regmap, MAX6639_REG_THERM_LIMIT(attr->index), &val); 135 if (res < 0) 136 return res; 137 138 return sprintf(buf, "%d\n", (val * 1000)); 139 } 140 141 static ssize_t temp_max_store(struct device *dev, 142 struct device_attribute *dev_attr, 143 const char *buf, size_t count) 144 { 145 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 146 struct max6639_data *data = dev_get_drvdata(dev); 147 unsigned long val; 148 int res; 149 150 res = kstrtoul(buf, 10, &val); 151 if (res) 152 return res; 153 154 regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(attr->index), 155 TEMP_LIMIT_TO_REG(val)); 156 return count; 157 } 158 159 static ssize_t temp_crit_show(struct device *dev, 160 struct device_attribute *dev_attr, char *buf) 161 { 162 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 163 struct max6639_data *data = dev_get_drvdata(dev); 164 unsigned int val; 165 int res; 166 167 res = regmap_read(data->regmap, MAX6639_REG_ALERT_LIMIT(attr->index), &val); 168 if (res < 0) 169 return res; 170 171 return sprintf(buf, "%d\n", (val * 1000)); 172 } 173 174 static ssize_t temp_crit_store(struct device *dev, 175 struct device_attribute *dev_attr, 176 const char *buf, size_t count) 177 { 178 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 179 struct max6639_data *data = dev_get_drvdata(dev); 180 unsigned long val; 181 int res; 182 183 res = kstrtoul(buf, 10, &val); 184 if (res) 185 return res; 186 187 regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(attr->index), 188 TEMP_LIMIT_TO_REG(val)); 189 return count; 190 } 191 192 static ssize_t temp_emergency_show(struct device *dev, 193 struct device_attribute *dev_attr, 194 char *buf) 195 { 196 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 197 struct max6639_data *data = dev_get_drvdata(dev); 198 unsigned int val; 199 int res; 200 201 res = regmap_read(data->regmap, MAX6639_REG_OT_LIMIT(attr->index), &val); 202 if (res < 0) 203 return res; 204 205 return sprintf(buf, "%d\n", (val * 1000)); 206 } 207 208 static ssize_t temp_emergency_store(struct device *dev, 209 struct device_attribute *dev_attr, 210 const char *buf, size_t count) 211 { 212 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 213 struct max6639_data *data = dev_get_drvdata(dev); 214 unsigned long val; 215 int res; 216 217 res = kstrtoul(buf, 10, &val); 218 if (res) 219 return res; 220 221 regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(attr->index), TEMP_LIMIT_TO_REG(val)); 222 223 return count; 224 } 225 226 static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr, 227 char *buf) 228 { 229 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 230 struct max6639_data *data = dev_get_drvdata(dev); 231 unsigned int val; 232 int res; 233 234 res = regmap_read(data->regmap, MAX6639_REG_TARGTDUTY(attr->index), &val); 235 if (res < 0) 236 return res; 237 238 return sprintf(buf, "%d\n", val * 255 / 120); 239 } 240 241 static ssize_t pwm_store(struct device *dev, 242 struct device_attribute *dev_attr, const char *buf, 243 size_t count) 244 { 245 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 246 struct max6639_data *data = dev_get_drvdata(dev); 247 unsigned long val; 248 int res; 249 250 res = kstrtoul(buf, 10, &val); 251 if (res) 252 return res; 253 254 val = clamp_val(val, 0, 255); 255 256 regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(attr->index), val * 120 / 255); 257 258 return count; 259 } 260 261 static ssize_t fan_input_show(struct device *dev, 262 struct device_attribute *dev_attr, char *buf) 263 { 264 struct max6639_data *data = dev_get_drvdata(dev); 265 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 266 unsigned int val; 267 int res; 268 269 res = regmap_read(data->regmap, MAX6639_REG_FAN_CNT(attr->index), &val); 270 if (res < 0) 271 return res; 272 273 return sprintf(buf, "%d\n", FAN_FROM_REG(val, data->rpm_range)); 274 } 275 276 static ssize_t alarm_show(struct device *dev, 277 struct device_attribute *dev_attr, char *buf) 278 { 279 struct max6639_data *data = dev_get_drvdata(dev); 280 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 281 unsigned int val; 282 int res; 283 284 res = regmap_read(data->regmap, MAX6639_REG_STATUS, &val); 285 if (res < 0) 286 return res; 287 288 return sprintf(buf, "%d\n", !!(val & (1 << attr->index))); 289 } 290 291 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); 292 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1); 293 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0); 294 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1); 295 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); 296 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); 297 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0); 298 static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1); 299 static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0); 300 static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1); 301 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); 302 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1); 303 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0); 304 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1); 305 static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1); 306 static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0); 307 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3); 308 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2); 309 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7); 310 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6); 311 static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5); 312 static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4); 313 314 315 static struct attribute *max6639_attrs[] = { 316 &sensor_dev_attr_temp1_input.dev_attr.attr, 317 &sensor_dev_attr_temp2_input.dev_attr.attr, 318 &sensor_dev_attr_temp1_fault.dev_attr.attr, 319 &sensor_dev_attr_temp2_fault.dev_attr.attr, 320 &sensor_dev_attr_temp1_max.dev_attr.attr, 321 &sensor_dev_attr_temp2_max.dev_attr.attr, 322 &sensor_dev_attr_temp1_crit.dev_attr.attr, 323 &sensor_dev_attr_temp2_crit.dev_attr.attr, 324 &sensor_dev_attr_temp1_emergency.dev_attr.attr, 325 &sensor_dev_attr_temp2_emergency.dev_attr.attr, 326 &sensor_dev_attr_pwm1.dev_attr.attr, 327 &sensor_dev_attr_pwm2.dev_attr.attr, 328 &sensor_dev_attr_fan1_input.dev_attr.attr, 329 &sensor_dev_attr_fan2_input.dev_attr.attr, 330 &sensor_dev_attr_fan1_fault.dev_attr.attr, 331 &sensor_dev_attr_fan2_fault.dev_attr.attr, 332 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 333 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 334 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 335 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 336 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr, 337 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, 338 NULL 339 }; 340 ATTRIBUTE_GROUPS(max6639); 341 342 /* 343 * returns respective index in rpm_ranges table 344 * 1 by default on invalid range 345 */ 346 static int rpm_range_to_reg(int range) 347 { 348 int i; 349 350 for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) { 351 if (rpm_ranges[i] == range) 352 return i; 353 } 354 355 return 1; /* default: 4000 RPM */ 356 } 357 358 static int max6639_set_ppr(struct max6639_data *data, u8 channel, u8 ppr) 359 { 360 return regmap_write(data->regmap, MAX6639_REG_FAN_PPR(channel), ppr << 6); 361 } 362 363 static int max6639_init_client(struct i2c_client *client, 364 struct max6639_data *data) 365 { 366 struct max6639_platform_data *max6639_info = 367 dev_get_platdata(&client->dev); 368 int i; 369 int rpm_range = 1; /* default: 4000 RPM */ 370 int err, ppr; 371 372 /* Reset chip to default values, see below for GCONFIG setup */ 373 err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_POR); 374 if (err) 375 return err; 376 377 /* Fans pulse per revolution is 2 by default */ 378 if (max6639_info && max6639_info->ppr > 0 && 379 max6639_info->ppr < 5) 380 ppr = max6639_info->ppr; 381 else 382 ppr = 2; 383 ppr -= 1; 384 385 if (max6639_info) 386 rpm_range = rpm_range_to_reg(max6639_info->rpm_range); 387 data->rpm_range = rpm_range; 388 389 for (i = 0; i < MAX6639_NUM_CHANNELS; i++) { 390 391 /* Set Fan pulse per revolution */ 392 err = max6639_set_ppr(data, i, ppr); 393 if (err) 394 return err; 395 396 /* Fans config PWM, RPM */ 397 err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG1(i), 398 MAX6639_FAN_CONFIG1_PWM | rpm_range); 399 if (err) 400 return err; 401 402 /* Fans PWM polarity high by default */ 403 if (max6639_info && max6639_info->pwm_polarity == 0) 404 err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), 0x00); 405 else 406 err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), 0x02); 407 if (err) 408 return err; 409 410 /* 411 * /THERM full speed enable, 412 * PWM frequency 25kHz, see also GCONFIG below 413 */ 414 err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG3(i), 415 MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03); 416 if (err) 417 return err; 418 419 /* Max. temp. 80C/90C/100C */ 420 err = regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(i), 80); 421 if (err) 422 return err; 423 err = regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(i), 90); 424 if (err) 425 return err; 426 err = regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(i), 100); 427 if (err) 428 return err; 429 430 /* PWM 120/120 (i.e. 100%) */ 431 err = regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(i), 120); 432 if (err) 433 return err; 434 } 435 /* Start monitoring */ 436 return regmap_write(data->regmap, MAX6639_REG_GCONFIG, 437 MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL | 438 MAX6639_GCONFIG_PWM_FREQ_HI); 439 440 } 441 442 /* Return 0 if detection is successful, -ENODEV otherwise */ 443 static int max6639_detect(struct i2c_client *client, 444 struct i2c_board_info *info) 445 { 446 struct i2c_adapter *adapter = client->adapter; 447 int dev_id, manu_id; 448 449 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 450 return -ENODEV; 451 452 /* Actual detection via device and manufacturer ID */ 453 dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID); 454 manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID); 455 if (dev_id != 0x58 || manu_id != 0x4D) 456 return -ENODEV; 457 458 strscpy(info->type, "max6639", I2C_NAME_SIZE); 459 460 return 0; 461 } 462 463 static void max6639_regulator_disable(void *data) 464 { 465 regulator_disable(data); 466 } 467 468 static bool max6639_regmap_is_volatile(struct device *dev, unsigned int reg) 469 { 470 switch (reg) { 471 case MAX6639_REG_TEMP(0): 472 case MAX6639_REG_TEMP_EXT(0): 473 case MAX6639_REG_TEMP(1): 474 case MAX6639_REG_TEMP_EXT(1): 475 case MAX6639_REG_STATUS: 476 case MAX6639_REG_FAN_CNT(0): 477 case MAX6639_REG_FAN_CNT(1): 478 case MAX6639_REG_TARGTDUTY(0): 479 case MAX6639_REG_TARGTDUTY(1): 480 return true; 481 default: 482 return false; 483 } 484 } 485 486 static const struct regmap_config max6639_regmap_config = { 487 .reg_bits = 8, 488 .val_bits = 8, 489 .max_register = MAX6639_REG_DEVREV, 490 .cache_type = REGCACHE_MAPLE, 491 .volatile_reg = max6639_regmap_is_volatile, 492 }; 493 494 static int max6639_probe(struct i2c_client *client) 495 { 496 struct device *dev = &client->dev; 497 struct max6639_data *data; 498 struct device *hwmon_dev; 499 int err; 500 501 data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL); 502 if (!data) 503 return -ENOMEM; 504 505 data->regmap = devm_regmap_init_i2c(client, &max6639_regmap_config); 506 if (IS_ERR(data->regmap)) 507 return dev_err_probe(dev, 508 PTR_ERR(data->regmap), 509 "regmap initialization failed\n"); 510 511 data->reg = devm_regulator_get_optional(dev, "fan"); 512 if (IS_ERR(data->reg)) { 513 if (PTR_ERR(data->reg) != -ENODEV) 514 return PTR_ERR(data->reg); 515 516 data->reg = NULL; 517 } else { 518 /* Spin up fans */ 519 err = regulator_enable(data->reg); 520 if (err) { 521 dev_err(dev, "Failed to enable fan supply: %d\n", err); 522 return err; 523 } 524 err = devm_add_action_or_reset(dev, max6639_regulator_disable, 525 data->reg); 526 if (err) { 527 dev_err(dev, "Failed to register action: %d\n", err); 528 return err; 529 } 530 } 531 532 /* Initialize the max6639 chip */ 533 err = max6639_init_client(client, data); 534 if (err < 0) 535 return err; 536 537 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 538 data, 539 max6639_groups); 540 return PTR_ERR_OR_ZERO(hwmon_dev); 541 } 542 543 static int max6639_suspend(struct device *dev) 544 { 545 struct max6639_data *data = dev_get_drvdata(dev); 546 547 if (data->reg) 548 regulator_disable(data->reg); 549 550 return regmap_write_bits(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_STANDBY, 551 MAX6639_GCONFIG_STANDBY); 552 } 553 554 static int max6639_resume(struct device *dev) 555 { 556 struct max6639_data *data = dev_get_drvdata(dev); 557 int ret; 558 559 if (data->reg) { 560 ret = regulator_enable(data->reg); 561 if (ret) { 562 dev_err(dev, "Failed to enable fan supply: %d\n", ret); 563 return ret; 564 } 565 } 566 567 return regmap_write_bits(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_STANDBY, 568 ~MAX6639_GCONFIG_STANDBY); 569 } 570 571 static const struct i2c_device_id max6639_id[] = { 572 {"max6639"}, 573 { } 574 }; 575 576 MODULE_DEVICE_TABLE(i2c, max6639_id); 577 578 static DEFINE_SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume); 579 580 static const struct of_device_id max6639_of_match[] = { 581 { .compatible = "maxim,max6639", }, 582 { }, 583 }; 584 585 static struct i2c_driver max6639_driver = { 586 .class = I2C_CLASS_HWMON, 587 .driver = { 588 .name = "max6639", 589 .pm = pm_sleep_ptr(&max6639_pm_ops), 590 .of_match_table = max6639_of_match, 591 }, 592 .probe = max6639_probe, 593 .id_table = max6639_id, 594 .detect = max6639_detect, 595 .address_list = normal_i2c, 596 }; 597 598 module_i2c_driver(max6639_driver); 599 600 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); 601 MODULE_DESCRIPTION("max6639 driver"); 602 MODULE_LICENSE("GPL"); 603