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