1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2017 IBM Corp. 4 */ 5 6 #include <linux/delay.h> 7 #include <linux/kernel.h> 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/err.h> 11 #include <linux/i2c.h> 12 #include "pmbus.h" 13 14 enum max31785_regs { 15 MFR_REVISION = 0x9b, 16 MFR_FAN_CONFIG = 0xf1, 17 }; 18 19 #define MAX31785 0x3030 20 #define MAX31785A 0x3040 21 #define MAX31785B 0x3061 22 23 #define MFR_FAN_CONFIG_DUAL_TACH BIT(12) 24 25 #define MAX31785_NR_PAGES 23 26 #define MAX31785_NR_FAN_PAGES 6 27 #define MAX31785_WAIT_DELAY_US 250 28 29 struct max31785_data { 30 ktime_t access; /* Chip access time */ 31 struct pmbus_driver_info info; 32 }; 33 34 #define to_max31785_data(x) container_of(x, struct max31785_data, info) 35 36 /* 37 * MAX31785 Driver Workaround 38 * 39 * The MAX31785 fan controller occasionally exhibits communication issues. 40 * These issues are not indicated by the device itself, except for occasional 41 * NACK responses during master transactions. No error bits are set in STATUS_BYTE. 42 * 43 * To address this, we introduce a delay of 250us between consecutive accesses 44 * to the fan controller. This delay helps mitigate communication problems by 45 * allowing sufficient time between accesses. 46 */ 47 static inline void max31785_wait(const struct max31785_data *data) 48 { 49 s64 delta = ktime_us_delta(ktime_get(), data->access); 50 51 if (delta < MAX31785_WAIT_DELAY_US) 52 usleep_range(MAX31785_WAIT_DELAY_US - delta, 53 MAX31785_WAIT_DELAY_US); 54 } 55 56 static int max31785_i2c_write_byte_data(struct i2c_client *client, 57 struct max31785_data *driver_data, 58 int command, u16 data) 59 { 60 int rc; 61 62 max31785_wait(driver_data); 63 rc = i2c_smbus_write_byte_data(client, command, data); 64 driver_data->access = ktime_get(); 65 return rc; 66 } 67 68 static int max31785_i2c_read_word_data(struct i2c_client *client, 69 struct max31785_data *driver_data, 70 int command) 71 { 72 int rc; 73 74 max31785_wait(driver_data); 75 rc = i2c_smbus_read_word_data(client, command); 76 driver_data->access = ktime_get(); 77 return rc; 78 } 79 80 static int _max31785_read_byte_data(struct i2c_client *client, 81 struct max31785_data *driver_data, 82 int page, int command) 83 { 84 int rc; 85 86 max31785_wait(driver_data); 87 rc = pmbus_read_byte_data(client, page, command); 88 driver_data->access = ktime_get(); 89 return rc; 90 } 91 92 static int _max31785_write_byte_data(struct i2c_client *client, 93 struct max31785_data *driver_data, 94 int page, int command, u16 data) 95 { 96 int rc; 97 98 max31785_wait(driver_data); 99 rc = pmbus_write_byte_data(client, page, command, data); 100 driver_data->access = ktime_get(); 101 return rc; 102 } 103 104 static int _max31785_read_word_data(struct i2c_client *client, 105 struct max31785_data *driver_data, 106 int page, int phase, int command) 107 { 108 int rc; 109 110 max31785_wait(driver_data); 111 rc = pmbus_read_word_data(client, page, phase, command); 112 driver_data->access = ktime_get(); 113 return rc; 114 } 115 116 static int _max31785_write_word_data(struct i2c_client *client, 117 struct max31785_data *driver_data, 118 int page, int command, u16 data) 119 { 120 int rc; 121 122 max31785_wait(driver_data); 123 rc = pmbus_write_word_data(client, page, command, data); 124 driver_data->access = ktime_get(); 125 return rc; 126 } 127 128 static int max31785_read_byte_data(struct i2c_client *client, int page, int reg) 129 { 130 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 131 struct max31785_data *driver_data = to_max31785_data(info); 132 133 switch (reg) { 134 case PMBUS_VOUT_MODE: 135 return -ENOTSUPP; 136 case PMBUS_FAN_CONFIG_12: 137 return _max31785_read_byte_data(client, driver_data, 138 page - MAX31785_NR_PAGES, 139 reg); 140 } 141 142 return -ENODATA; 143 } 144 145 static int max31785_write_byte(struct i2c_client *client, int page, u8 value) 146 { 147 if (page < MAX31785_NR_PAGES) 148 return -ENODATA; 149 150 return -ENOTSUPP; 151 } 152 153 static int max31785_read_long_data(struct i2c_client *client, int page, 154 int reg, u32 *data) 155 { 156 unsigned char cmdbuf[1]; 157 unsigned char rspbuf[4]; 158 int rc; 159 160 struct i2c_msg msg[2] = { 161 { 162 .addr = client->addr, 163 .flags = 0, 164 .len = sizeof(cmdbuf), 165 .buf = cmdbuf, 166 }, 167 { 168 .addr = client->addr, 169 .flags = I2C_M_RD, 170 .len = sizeof(rspbuf), 171 .buf = rspbuf, 172 }, 173 }; 174 175 cmdbuf[0] = reg; 176 177 rc = pmbus_set_page(client, page, 0xff); 178 if (rc < 0) 179 return rc; 180 181 rc = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 182 if (rc < 0) 183 return rc; 184 185 *data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) | 186 (rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8)); 187 188 return rc; 189 } 190 191 static int max31785_get_pwm(struct i2c_client *client, int page) 192 { 193 int rv; 194 195 rv = pmbus_get_fan_rate_device(client, page, 0, percent); 196 if (rv < 0) 197 return rv; 198 else if (rv >= 0x8000) 199 return 0; 200 else if (rv >= 0x2711) 201 return 0x2710; 202 203 return rv; 204 } 205 206 static int max31785_get_pwm_mode(struct i2c_client *client, 207 struct max31785_data *driver_data, int page) 208 { 209 int config; 210 int command; 211 212 config = _max31785_read_byte_data(client, driver_data, page, 213 PMBUS_FAN_CONFIG_12); 214 if (config < 0) 215 return config; 216 217 command = _max31785_read_word_data(client, driver_data, page, 0xff, 218 PMBUS_FAN_COMMAND_1); 219 if (command < 0) 220 return command; 221 222 if (config & PB_FAN_1_RPM) 223 return (command >= 0x8000) ? 3 : 2; 224 225 if (command >= 0x8000) 226 return 3; 227 else if (command >= 0x2711) 228 return 0; 229 230 return 1; 231 } 232 233 static int max31785_read_word_data(struct i2c_client *client, int page, 234 int phase, int reg) 235 { 236 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 237 struct max31785_data *driver_data = to_max31785_data(info); 238 u32 val; 239 int rv; 240 241 switch (reg) { 242 case PMBUS_READ_FAN_SPEED_1: 243 if (page < MAX31785_NR_PAGES) 244 return -ENODATA; 245 246 rv = max31785_read_long_data(client, page - MAX31785_NR_PAGES, 247 reg, &val); 248 if (rv < 0) 249 return rv; 250 251 rv = (val >> 16) & 0xffff; 252 break; 253 case PMBUS_FAN_COMMAND_1: 254 /* 255 * PMBUS_FAN_COMMAND_x is probed to judge whether or not to 256 * expose fan control registers. 257 * 258 * Don't expose fan_target attribute for virtual pages. 259 */ 260 rv = (page >= MAX31785_NR_PAGES) ? -ENOTSUPP : -ENODATA; 261 break; 262 case PMBUS_VIRT_PWM_1: 263 rv = max31785_get_pwm(client, page); 264 break; 265 case PMBUS_VIRT_PWM_ENABLE_1: 266 rv = max31785_get_pwm_mode(client, driver_data, page); 267 break; 268 default: 269 rv = -ENODATA; 270 break; 271 } 272 273 return rv; 274 } 275 276 static inline u32 max31785_scale_pwm(u32 sensor_val) 277 { 278 /* 279 * The datasheet describes the accepted value range for manual PWM as 280 * [0, 0x2710], while the hwmon pwmX sysfs interface accepts values in 281 * [0, 255]. The MAX31785 uses DIRECT mode to scale the FAN_COMMAND 282 * registers and in PWM mode the coefficients are m=1, b=0, R=2. The 283 * important observation here is that 0x2710 == 10000 == 100 * 100. 284 * 285 * R=2 (== 10^2 == 100) accounts for scaling the value provided at the 286 * sysfs interface into the required hardware resolution, but it does 287 * not yet yield a value that we can write to the device (this initial 288 * scaling is handled by pmbus_data2reg()). Multiplying by 100 below 289 * translates the parameter value into the percentage units required by 290 * PMBus, and then we scale back by 255 as required by the hwmon pwmX 291 * interface to yield the percentage value at the appropriate 292 * resolution for hardware. 293 */ 294 return (sensor_val * 100) / 255; 295 } 296 297 static int max31785_update_fan(struct i2c_client *client, 298 struct max31785_data *driver_data, int page, 299 u8 config, u8 mask, u16 command) 300 { 301 int from, rv; 302 u8 to; 303 304 from = _max31785_read_byte_data(client, driver_data, page, 305 PMBUS_FAN_CONFIG_12); 306 if (from < 0) 307 return from; 308 309 to = (from & ~mask) | (config & mask); 310 311 if (to != from) { 312 rv = _max31785_write_byte_data(client, driver_data, page, 313 PMBUS_FAN_CONFIG_12, to); 314 if (rv < 0) 315 return rv; 316 } 317 318 rv = _max31785_write_word_data(client, driver_data, page, 319 PMBUS_FAN_COMMAND_1, command); 320 321 return rv; 322 } 323 324 static int max31785_pwm_enable(struct i2c_client *client, 325 struct max31785_data *driver_data, int page, 326 u16 word) 327 { 328 int config = 0; 329 int rate; 330 331 switch (word) { 332 case 0: 333 rate = 0x7fff; 334 break; 335 case 1: 336 rate = pmbus_get_fan_rate_cached(client, page, 0, percent); 337 if (rate < 0) 338 return rate; 339 rate = max31785_scale_pwm(rate); 340 break; 341 case 2: 342 config = PB_FAN_1_RPM; 343 rate = pmbus_get_fan_rate_cached(client, page, 0, rpm); 344 if (rate < 0) 345 return rate; 346 break; 347 case 3: 348 rate = 0xffff; 349 break; 350 default: 351 return -EINVAL; 352 } 353 354 return max31785_update_fan(client, driver_data, page, config, 355 PB_FAN_1_RPM, rate); 356 } 357 358 static int max31785_write_word_data(struct i2c_client *client, int page, 359 int reg, u16 word) 360 { 361 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 362 struct max31785_data *driver_data = to_max31785_data(info); 363 364 switch (reg) { 365 case PMBUS_VIRT_PWM_1: 366 return max31785_update_fan(client, driver_data, page, 0, 367 PB_FAN_1_RPM, 368 max31785_scale_pwm(word)); 369 case PMBUS_VIRT_PWM_ENABLE_1: 370 return max31785_pwm_enable(client, driver_data, page, word); 371 default: 372 break; 373 } 374 375 return -ENODATA; 376 } 377 378 #define MAX31785_FAN_FUNCS \ 379 (PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12) 380 381 #define MAX31785_TEMP_FUNCS \ 382 (PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP) 383 384 #define MAX31785_VOUT_FUNCS \ 385 (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT) 386 387 static const struct pmbus_driver_info max31785_info = { 388 .pages = MAX31785_NR_PAGES, 389 390 .write_word_data = max31785_write_word_data, 391 .read_byte_data = max31785_read_byte_data, 392 .read_word_data = max31785_read_word_data, 393 .write_byte = max31785_write_byte, 394 395 /* RPM */ 396 .format[PSC_FAN] = direct, 397 .m[PSC_FAN] = 1, 398 .b[PSC_FAN] = 0, 399 .R[PSC_FAN] = 0, 400 /* PWM */ 401 .format[PSC_PWM] = direct, 402 .m[PSC_PWM] = 1, 403 .b[PSC_PWM] = 0, 404 .R[PSC_PWM] = 2, 405 .func[0] = MAX31785_FAN_FUNCS, 406 .func[1] = MAX31785_FAN_FUNCS, 407 .func[2] = MAX31785_FAN_FUNCS, 408 .func[3] = MAX31785_FAN_FUNCS, 409 .func[4] = MAX31785_FAN_FUNCS, 410 .func[5] = MAX31785_FAN_FUNCS, 411 412 .format[PSC_TEMPERATURE] = direct, 413 .m[PSC_TEMPERATURE] = 1, 414 .b[PSC_TEMPERATURE] = 0, 415 .R[PSC_TEMPERATURE] = 2, 416 .func[6] = MAX31785_TEMP_FUNCS, 417 .func[7] = MAX31785_TEMP_FUNCS, 418 .func[8] = MAX31785_TEMP_FUNCS, 419 .func[9] = MAX31785_TEMP_FUNCS, 420 .func[10] = MAX31785_TEMP_FUNCS, 421 .func[11] = MAX31785_TEMP_FUNCS, 422 .func[12] = MAX31785_TEMP_FUNCS, 423 .func[13] = MAX31785_TEMP_FUNCS, 424 .func[14] = MAX31785_TEMP_FUNCS, 425 .func[15] = MAX31785_TEMP_FUNCS, 426 .func[16] = MAX31785_TEMP_FUNCS, 427 428 .format[PSC_VOLTAGE_OUT] = direct, 429 .m[PSC_VOLTAGE_OUT] = 1, 430 .b[PSC_VOLTAGE_OUT] = 0, 431 .R[PSC_VOLTAGE_OUT] = 0, 432 .func[17] = MAX31785_VOUT_FUNCS, 433 .func[18] = MAX31785_VOUT_FUNCS, 434 .func[19] = MAX31785_VOUT_FUNCS, 435 .func[20] = MAX31785_VOUT_FUNCS, 436 .func[21] = MAX31785_VOUT_FUNCS, 437 .func[22] = MAX31785_VOUT_FUNCS, 438 }; 439 440 static int max31785_configure_dual_tach(struct i2c_client *client, 441 struct pmbus_driver_info *info) 442 { 443 int ret; 444 int i; 445 struct max31785_data *driver_data = to_max31785_data(info); 446 447 for (i = 0; i < MAX31785_NR_FAN_PAGES; i++) { 448 ret = max31785_i2c_write_byte_data(client, driver_data, 449 PMBUS_PAGE, i); 450 if (ret < 0) 451 return ret; 452 453 ret = max31785_i2c_read_word_data(client, driver_data, 454 MFR_FAN_CONFIG); 455 if (ret < 0) 456 return ret; 457 458 if (ret & MFR_FAN_CONFIG_DUAL_TACH) { 459 int virtual = MAX31785_NR_PAGES + i; 460 461 info->pages = virtual + 1; 462 info->func[virtual] |= PMBUS_HAVE_FAN12; 463 info->func[virtual] |= PMBUS_PAGE_VIRTUAL; 464 } 465 } 466 467 return 0; 468 } 469 470 static int max31785_probe(struct i2c_client *client) 471 { 472 struct device *dev = &client->dev; 473 struct pmbus_driver_info *info; 474 struct max31785_data *driver_data; 475 bool dual_tach = false; 476 int ret; 477 478 if (!i2c_check_functionality(client->adapter, 479 I2C_FUNC_SMBUS_BYTE_DATA | 480 I2C_FUNC_SMBUS_WORD_DATA)) 481 return -ENODEV; 482 483 driver_data = devm_kzalloc(dev, sizeof(struct max31785_data), GFP_KERNEL); 484 if (!driver_data) 485 return -ENOMEM; 486 487 info = &driver_data->info; 488 driver_data->access = ktime_get(); 489 *info = max31785_info; 490 491 ret = max31785_i2c_write_byte_data(client, driver_data, 492 PMBUS_PAGE, 255); 493 if (ret < 0) 494 return ret; 495 496 ret = i2c_smbus_read_word_data(client, MFR_REVISION); 497 if (ret < 0) 498 return ret; 499 500 if (ret == MAX31785A || ret == MAX31785B) { 501 dual_tach = true; 502 } else if (ret == MAX31785) { 503 if (!strcmp("max31785a", client->name) || 504 !strcmp("max31785b", client->name)) 505 dev_warn(dev, "Expected max31785a/b, found max31785: cannot provide secondary tachometer readings\n"); 506 } else { 507 dev_err(dev, "Unrecognized MAX31785 revision: %x\n", ret); 508 return -ENODEV; 509 } 510 511 if (dual_tach) { 512 ret = max31785_configure_dual_tach(client, info); 513 if (ret < 0) 514 return ret; 515 } 516 517 return pmbus_do_probe(client, info); 518 } 519 520 static const struct i2c_device_id max31785_id[] = { 521 { "max31785" }, 522 { "max31785a" }, 523 { "max31785b" }, 524 { }, 525 }; 526 527 MODULE_DEVICE_TABLE(i2c, max31785_id); 528 529 static const struct of_device_id max31785_of_match[] = { 530 { .compatible = "maxim,max31785" }, 531 { .compatible = "maxim,max31785a" }, 532 { .compatible = "maxim,max31785b" }, 533 { }, 534 }; 535 536 MODULE_DEVICE_TABLE(of, max31785_of_match); 537 538 static struct i2c_driver max31785_driver = { 539 .driver = { 540 .name = "max31785", 541 .of_match_table = max31785_of_match, 542 }, 543 .probe = max31785_probe, 544 .id_table = max31785_id, 545 }; 546 547 module_i2c_driver(max31785_driver); 548 549 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>"); 550 MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785"); 551 MODULE_LICENSE("GPL"); 552 MODULE_IMPORT_NS(PMBUS); 553