1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2017 IBM Corp. 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/err.h> 10 #include <linux/i2c.h> 11 #include "pmbus.h" 12 13 enum max31785_regs { 14 MFR_REVISION = 0x9b, 15 MFR_FAN_CONFIG = 0xf1, 16 }; 17 18 #define MAX31785 0x3030 19 #define MAX31785A 0x3040 20 21 #define MFR_FAN_CONFIG_DUAL_TACH BIT(12) 22 23 #define MAX31785_NR_PAGES 23 24 #define MAX31785_NR_FAN_PAGES 6 25 26 static int max31785_read_byte_data(struct i2c_client *client, int page, 27 int reg) 28 { 29 if (page < MAX31785_NR_PAGES) 30 return -ENODATA; 31 32 switch (reg) { 33 case PMBUS_VOUT_MODE: 34 return -ENOTSUPP; 35 case PMBUS_FAN_CONFIG_12: 36 return pmbus_read_byte_data(client, page - MAX31785_NR_PAGES, 37 reg); 38 } 39 40 return -ENODATA; 41 } 42 43 static int max31785_write_byte(struct i2c_client *client, int page, u8 value) 44 { 45 if (page < MAX31785_NR_PAGES) 46 return -ENODATA; 47 48 return -ENOTSUPP; 49 } 50 51 static int max31785_read_long_data(struct i2c_client *client, int page, 52 int reg, u32 *data) 53 { 54 unsigned char cmdbuf[1]; 55 unsigned char rspbuf[4]; 56 int rc; 57 58 struct i2c_msg msg[2] = { 59 { 60 .addr = client->addr, 61 .flags = 0, 62 .len = sizeof(cmdbuf), 63 .buf = cmdbuf, 64 }, 65 { 66 .addr = client->addr, 67 .flags = I2C_M_RD, 68 .len = sizeof(rspbuf), 69 .buf = rspbuf, 70 }, 71 }; 72 73 cmdbuf[0] = reg; 74 75 rc = pmbus_set_page(client, page, 0xff); 76 if (rc < 0) 77 return rc; 78 79 rc = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 80 if (rc < 0) 81 return rc; 82 83 *data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) | 84 (rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8)); 85 86 return rc; 87 } 88 89 static int max31785_get_pwm(struct i2c_client *client, int page) 90 { 91 int rv; 92 93 rv = pmbus_get_fan_rate_device(client, page, 0, percent); 94 if (rv < 0) 95 return rv; 96 else if (rv >= 0x8000) 97 return 0; 98 else if (rv >= 0x2711) 99 return 0x2710; 100 101 return rv; 102 } 103 104 static int max31785_get_pwm_mode(struct i2c_client *client, int page) 105 { 106 int config; 107 int command; 108 109 config = pmbus_read_byte_data(client, page, PMBUS_FAN_CONFIG_12); 110 if (config < 0) 111 return config; 112 113 command = pmbus_read_word_data(client, page, 0xff, PMBUS_FAN_COMMAND_1); 114 if (command < 0) 115 return command; 116 117 if (config & PB_FAN_1_RPM) 118 return (command >= 0x8000) ? 3 : 2; 119 120 if (command >= 0x8000) 121 return 3; 122 else if (command >= 0x2711) 123 return 0; 124 125 return 1; 126 } 127 128 static int max31785_read_word_data(struct i2c_client *client, int page, 129 int phase, int reg) 130 { 131 u32 val; 132 int rv; 133 134 switch (reg) { 135 case PMBUS_READ_FAN_SPEED_1: 136 if (page < MAX31785_NR_PAGES) 137 return -ENODATA; 138 139 rv = max31785_read_long_data(client, page - MAX31785_NR_PAGES, 140 reg, &val); 141 if (rv < 0) 142 return rv; 143 144 rv = (val >> 16) & 0xffff; 145 break; 146 case PMBUS_FAN_COMMAND_1: 147 /* 148 * PMBUS_FAN_COMMAND_x is probed to judge whether or not to 149 * expose fan control registers. 150 * 151 * Don't expose fan_target attribute for virtual pages. 152 */ 153 rv = (page >= MAX31785_NR_PAGES) ? -ENOTSUPP : -ENODATA; 154 break; 155 case PMBUS_VIRT_PWM_1: 156 rv = max31785_get_pwm(client, page); 157 break; 158 case PMBUS_VIRT_PWM_ENABLE_1: 159 rv = max31785_get_pwm_mode(client, page); 160 break; 161 default: 162 rv = -ENODATA; 163 break; 164 } 165 166 return rv; 167 } 168 169 static inline u32 max31785_scale_pwm(u32 sensor_val) 170 { 171 /* 172 * The datasheet describes the accepted value range for manual PWM as 173 * [0, 0x2710], while the hwmon pwmX sysfs interface accepts values in 174 * [0, 255]. The MAX31785 uses DIRECT mode to scale the FAN_COMMAND 175 * registers and in PWM mode the coefficients are m=1, b=0, R=2. The 176 * important observation here is that 0x2710 == 10000 == 100 * 100. 177 * 178 * R=2 (== 10^2 == 100) accounts for scaling the value provided at the 179 * sysfs interface into the required hardware resolution, but it does 180 * not yet yield a value that we can write to the device (this initial 181 * scaling is handled by pmbus_data2reg()). Multiplying by 100 below 182 * translates the parameter value into the percentage units required by 183 * PMBus, and then we scale back by 255 as required by the hwmon pwmX 184 * interface to yield the percentage value at the appropriate 185 * resolution for hardware. 186 */ 187 return (sensor_val * 100) / 255; 188 } 189 190 static int max31785_pwm_enable(struct i2c_client *client, int page, 191 u16 word) 192 { 193 int config = 0; 194 int rate; 195 196 switch (word) { 197 case 0: 198 rate = 0x7fff; 199 break; 200 case 1: 201 rate = pmbus_get_fan_rate_cached(client, page, 0, percent); 202 if (rate < 0) 203 return rate; 204 rate = max31785_scale_pwm(rate); 205 break; 206 case 2: 207 config = PB_FAN_1_RPM; 208 rate = pmbus_get_fan_rate_cached(client, page, 0, rpm); 209 if (rate < 0) 210 return rate; 211 break; 212 case 3: 213 rate = 0xffff; 214 break; 215 default: 216 return -EINVAL; 217 } 218 219 return pmbus_update_fan(client, page, 0, config, PB_FAN_1_RPM, rate); 220 } 221 222 static int max31785_write_word_data(struct i2c_client *client, int page, 223 int reg, u16 word) 224 { 225 switch (reg) { 226 case PMBUS_VIRT_PWM_1: 227 return pmbus_update_fan(client, page, 0, 0, PB_FAN_1_RPM, 228 max31785_scale_pwm(word)); 229 case PMBUS_VIRT_PWM_ENABLE_1: 230 return max31785_pwm_enable(client, page, word); 231 default: 232 break; 233 } 234 235 return -ENODATA; 236 } 237 238 #define MAX31785_FAN_FUNCS \ 239 (PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12) 240 241 #define MAX31785_TEMP_FUNCS \ 242 (PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP) 243 244 #define MAX31785_VOUT_FUNCS \ 245 (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT) 246 247 static const struct pmbus_driver_info max31785_info = { 248 .pages = MAX31785_NR_PAGES, 249 250 .write_word_data = max31785_write_word_data, 251 .read_byte_data = max31785_read_byte_data, 252 .read_word_data = max31785_read_word_data, 253 .write_byte = max31785_write_byte, 254 255 /* RPM */ 256 .format[PSC_FAN] = direct, 257 .m[PSC_FAN] = 1, 258 .b[PSC_FAN] = 0, 259 .R[PSC_FAN] = 0, 260 /* PWM */ 261 .format[PSC_PWM] = direct, 262 .m[PSC_PWM] = 1, 263 .b[PSC_PWM] = 0, 264 .R[PSC_PWM] = 2, 265 .func[0] = MAX31785_FAN_FUNCS, 266 .func[1] = MAX31785_FAN_FUNCS, 267 .func[2] = MAX31785_FAN_FUNCS, 268 .func[3] = MAX31785_FAN_FUNCS, 269 .func[4] = MAX31785_FAN_FUNCS, 270 .func[5] = MAX31785_FAN_FUNCS, 271 272 .format[PSC_TEMPERATURE] = direct, 273 .m[PSC_TEMPERATURE] = 1, 274 .b[PSC_TEMPERATURE] = 0, 275 .R[PSC_TEMPERATURE] = 2, 276 .func[6] = MAX31785_TEMP_FUNCS, 277 .func[7] = MAX31785_TEMP_FUNCS, 278 .func[8] = MAX31785_TEMP_FUNCS, 279 .func[9] = MAX31785_TEMP_FUNCS, 280 .func[10] = MAX31785_TEMP_FUNCS, 281 .func[11] = MAX31785_TEMP_FUNCS, 282 .func[12] = MAX31785_TEMP_FUNCS, 283 .func[13] = MAX31785_TEMP_FUNCS, 284 .func[14] = MAX31785_TEMP_FUNCS, 285 .func[15] = MAX31785_TEMP_FUNCS, 286 .func[16] = MAX31785_TEMP_FUNCS, 287 288 .format[PSC_VOLTAGE_OUT] = direct, 289 .m[PSC_VOLTAGE_OUT] = 1, 290 .b[PSC_VOLTAGE_OUT] = 0, 291 .R[PSC_VOLTAGE_OUT] = 0, 292 .func[17] = MAX31785_VOUT_FUNCS, 293 .func[18] = MAX31785_VOUT_FUNCS, 294 .func[19] = MAX31785_VOUT_FUNCS, 295 .func[20] = MAX31785_VOUT_FUNCS, 296 .func[21] = MAX31785_VOUT_FUNCS, 297 .func[22] = MAX31785_VOUT_FUNCS, 298 }; 299 300 static int max31785_configure_dual_tach(struct i2c_client *client, 301 struct pmbus_driver_info *info) 302 { 303 int ret; 304 int i; 305 306 for (i = 0; i < MAX31785_NR_FAN_PAGES; i++) { 307 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i); 308 if (ret < 0) 309 return ret; 310 311 ret = i2c_smbus_read_word_data(client, MFR_FAN_CONFIG); 312 if (ret < 0) 313 return ret; 314 315 if (ret & MFR_FAN_CONFIG_DUAL_TACH) { 316 int virtual = MAX31785_NR_PAGES + i; 317 318 info->pages = virtual + 1; 319 info->func[virtual] |= PMBUS_HAVE_FAN12; 320 info->func[virtual] |= PMBUS_PAGE_VIRTUAL; 321 } 322 } 323 324 return 0; 325 } 326 327 static int max31785_probe(struct i2c_client *client, 328 const struct i2c_device_id *id) 329 { 330 struct device *dev = &client->dev; 331 struct pmbus_driver_info *info; 332 bool dual_tach = false; 333 s64 ret; 334 335 if (!i2c_check_functionality(client->adapter, 336 I2C_FUNC_SMBUS_BYTE_DATA | 337 I2C_FUNC_SMBUS_WORD_DATA)) 338 return -ENODEV; 339 340 info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL); 341 if (!info) 342 return -ENOMEM; 343 344 *info = max31785_info; 345 346 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 255); 347 if (ret < 0) 348 return ret; 349 350 ret = i2c_smbus_read_word_data(client, MFR_REVISION); 351 if (ret < 0) 352 return ret; 353 354 if (ret == MAX31785A) { 355 dual_tach = true; 356 } else if (ret == MAX31785) { 357 if (!strcmp("max31785a", id->name)) 358 dev_warn(dev, "Expected max3175a, found max31785: cannot provide secondary tachometer readings\n"); 359 } else { 360 return -ENODEV; 361 } 362 363 if (dual_tach) { 364 ret = max31785_configure_dual_tach(client, info); 365 if (ret < 0) 366 return ret; 367 } 368 369 return pmbus_do_probe(client, id, info); 370 } 371 372 static const struct i2c_device_id max31785_id[] = { 373 { "max31785", 0 }, 374 { "max31785a", 0 }, 375 { }, 376 }; 377 378 MODULE_DEVICE_TABLE(i2c, max31785_id); 379 380 static const struct of_device_id max31785_of_match[] = { 381 { .compatible = "maxim,max31785" }, 382 { .compatible = "maxim,max31785a" }, 383 { }, 384 }; 385 386 MODULE_DEVICE_TABLE(of, max31785_of_match); 387 388 static struct i2c_driver max31785_driver = { 389 .driver = { 390 .name = "max31785", 391 .of_match_table = max31785_of_match, 392 }, 393 .probe = max31785_probe, 394 .remove = pmbus_do_remove, 395 .id_table = max31785_id, 396 }; 397 398 module_i2c_driver(max31785_driver); 399 400 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>"); 401 MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785"); 402 MODULE_LICENSE("GPL"); 403