1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Hardware monitoring driver for EMC2305 fan controller 4 * 5 * Copyright (C) 2022 Nvidia Technologies Ltd. 6 */ 7 8 #include <linux/err.h> 9 #include <linux/hwmon.h> 10 #include <linux/i2c.h> 11 #include <linux/module.h> 12 #include <linux/platform_data/emc2305.h> 13 #include <linux/thermal.h> 14 15 static const unsigned short 16 emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END }; 17 18 #define EMC2305_REG_DRIVE_FAIL_STATUS 0x27 19 #define EMC2305_REG_VENDOR 0xfe 20 #define EMC2305_FAN_MAX 0xff 21 #define EMC2305_FAN_MIN 0x00 22 #define EMC2305_FAN_MAX_STATE 10 23 #define EMC2305_DEVICE 0x34 24 #define EMC2305_VENDOR 0x5d 25 #define EMC2305_REG_PRODUCT_ID 0xfd 26 #define EMC2305_TACH_REGS_UNUSE_BITS 3 27 #define EMC2305_TACH_CNT_MULTIPLIER 0x02 28 #define EMC2305_TACH_RANGE_MIN 480 29 30 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \ 31 DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max)) 32 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \ 33 DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state)) 34 35 /* 36 * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges 37 * equal (poles * 2 + 1). 38 */ 39 #define EMC2305_RPM_FACTOR 3932160 40 41 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n)) 42 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n)) 43 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n)) 44 45 enum emc230x_product_id { 46 EMC2305 = 0x34, 47 EMC2303 = 0x35, 48 EMC2302 = 0x36, 49 EMC2301 = 0x37, 50 }; 51 52 static const struct i2c_device_id emc2305_ids[] = { 53 { "emc2305", 0 }, 54 { "emc2303", 0 }, 55 { "emc2302", 0 }, 56 { "emc2301", 0 }, 57 { } 58 }; 59 MODULE_DEVICE_TABLE(i2c, emc2305_ids); 60 61 /** 62 * @cdev: cooling device; 63 * @curr_state: cooling current state; 64 * @last_hwmon_state: last cooling state updated by hwmon subsystem; 65 * @last_thermal_state: last cooling state updated by thermal subsystem; 66 * 67 * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit 68 * speed feature. The purpose of this feature is to provides ability to limit fan speed 69 * according to some system wise considerations, like absence of some replaceable units (PSU or 70 * line cards), high system ambient temperature, unreliable transceivers temperature sensing or 71 * some other factors which indirectly impacts system's airflow 72 * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is 73 * used for setting low limit for fan speed in case 'thermal' subsystem is configured in 74 * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal' 75 * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm' 76 * attribute. 77 * From other side, fan speed is to be updated in hardware through 'pwm' only in case the 78 * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan 79 * speed will be just stored with no PWM update. 80 */ 81 struct emc2305_cdev_data { 82 struct thermal_cooling_device *cdev; 83 unsigned int cur_state; 84 unsigned long last_hwmon_state; 85 unsigned long last_thermal_state; 86 }; 87 88 /** 89 * @client: i2c client; 90 * @hwmon_dev: hwmon device; 91 * @max_state: maximum cooling state of the cooling device; 92 * @pwm_num: number of PWM channels; 93 * @pwm_separate: separate PWM settings for every channel; 94 * @pwm_min: array of minimum PWM per channel; 95 * @cdev_data: array of cooling devices data; 96 */ 97 struct emc2305_data { 98 struct i2c_client *client; 99 struct device *hwmon_dev; 100 u8 max_state; 101 u8 pwm_num; 102 bool pwm_separate; 103 u8 pwm_min[EMC2305_PWM_MAX]; 104 struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX]; 105 }; 106 107 static char *emc2305_fan_name[] = { 108 "emc2305_fan", 109 "emc2305_fan1", 110 "emc2305_fan2", 111 "emc2305_fan3", 112 "emc2305_fan4", 113 "emc2305_fan5", 114 }; 115 116 static void emc2305_unset_tz(struct device *dev); 117 118 static int emc2305_get_max_channel(const struct emc2305_data *data) 119 { 120 return data->pwm_num; 121 } 122 123 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev) 124 { 125 struct emc2305_data *data = cdev->devdata; 126 size_t len = strlen(cdev->type); 127 int ret; 128 129 if (len <= 0) 130 return -EINVAL; 131 132 /* 133 * Returns index of cooling device 0..4 in case of separate PWM setting. 134 * Zero index is used in case of one common PWM setting. 135 * If the mode is not set as pwm_separate, all PWMs are to be bound 136 * to the common thermal zone and should work at the same speed 137 * to perform cooling for the same thermal junction. 138 * Otherwise, return specific channel that will be used in bound 139 * related PWM to the thermal zone. 140 */ 141 if (!data->pwm_separate) 142 return 0; 143 144 ret = cdev->type[len - 1]; 145 switch (ret) { 146 case '1' ... '5': 147 return ret - '1'; 148 default: 149 break; 150 } 151 return -EINVAL; 152 } 153 154 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state) 155 { 156 int cdev_idx; 157 struct emc2305_data *data = cdev->devdata; 158 159 cdev_idx = emc2305_get_cdev_idx(cdev); 160 if (cdev_idx < 0) 161 return cdev_idx; 162 163 *state = data->cdev_data[cdev_idx].cur_state; 164 return 0; 165 } 166 167 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state) 168 { 169 struct emc2305_data *data = cdev->devdata; 170 *state = data->max_state; 171 return 0; 172 } 173 174 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) 175 { 176 int cdev_idx, ret; 177 struct emc2305_data *data = cdev->devdata; 178 struct i2c_client *client = data->client; 179 u8 val, i; 180 181 if (state > data->max_state) 182 return -EINVAL; 183 184 cdev_idx = emc2305_get_cdev_idx(cdev); 185 if (cdev_idx < 0) 186 return cdev_idx; 187 188 /* Save thermal state. */ 189 data->cdev_data[cdev_idx].last_thermal_state = state; 190 state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state); 191 192 val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX); 193 194 data->cdev_data[cdev_idx].cur_state = state; 195 if (data->pwm_separate) { 196 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val); 197 if (ret < 0) 198 return ret; 199 } else { 200 /* 201 * Set the same PWM value in all channels 202 * if common PWM channel is used. 203 */ 204 for (i = 0; i < data->pwm_num; i++) { 205 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val); 206 if (ret < 0) 207 return ret; 208 } 209 } 210 211 return 0; 212 } 213 214 static const struct thermal_cooling_device_ops emc2305_cooling_ops = { 215 .get_max_state = emc2305_get_max_state, 216 .get_cur_state = emc2305_get_cur_state, 217 .set_cur_state = emc2305_set_cur_state, 218 }; 219 220 static int emc2305_show_fault(struct device *dev, int channel) 221 { 222 struct emc2305_data *data = dev_get_drvdata(dev); 223 struct i2c_client *client = data->client; 224 int status_reg; 225 226 status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS); 227 if (status_reg < 0) 228 return status_reg; 229 230 return status_reg & (1 << channel) ? 1 : 0; 231 } 232 233 static int emc2305_show_fan(struct device *dev, int channel) 234 { 235 struct emc2305_data *data = dev_get_drvdata(dev); 236 struct i2c_client *client = data->client; 237 int ret; 238 239 ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel)); 240 if (ret <= 0) 241 return ret; 242 243 ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS; 244 ret = EMC2305_RPM_FACTOR / ret; 245 if (ret <= EMC2305_TACH_RANGE_MIN) 246 return 0; 247 248 return ret * EMC2305_TACH_CNT_MULTIPLIER; 249 } 250 251 static int emc2305_show_pwm(struct device *dev, int channel) 252 { 253 struct emc2305_data *data = dev_get_drvdata(dev); 254 struct i2c_client *client = data->client; 255 256 return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel)); 257 } 258 259 static int emc2305_set_pwm(struct device *dev, long val, int channel) 260 { 261 struct emc2305_data *data = dev_get_drvdata(dev); 262 struct i2c_client *client = data->client; 263 int ret; 264 265 if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX) 266 return -EINVAL; 267 268 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val); 269 if (ret < 0) 270 return ret; 271 data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state, 272 EMC2305_FAN_MAX); 273 return 0; 274 } 275 276 static int emc2305_set_single_tz(struct device *dev, int idx) 277 { 278 struct emc2305_data *data = dev_get_drvdata(dev); 279 long pwm; 280 int i, cdev_idx, ret; 281 282 cdev_idx = (idx) ? idx - 1 : 0; 283 pwm = data->pwm_min[cdev_idx]; 284 285 data->cdev_data[cdev_idx].cdev = 286 thermal_cooling_device_register(emc2305_fan_name[idx], data, 287 &emc2305_cooling_ops); 288 289 if (IS_ERR(data->cdev_data[cdev_idx].cdev)) { 290 dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]); 291 return PTR_ERR(data->cdev_data[cdev_idx].cdev); 292 } 293 /* Set minimal PWM speed. */ 294 if (data->pwm_separate) { 295 ret = emc2305_set_pwm(dev, pwm, cdev_idx); 296 if (ret < 0) 297 return ret; 298 } else { 299 for (i = 0; i < data->pwm_num; i++) { 300 ret = emc2305_set_pwm(dev, pwm, i); 301 if (ret < 0) 302 return ret; 303 } 304 } 305 data->cdev_data[cdev_idx].cur_state = 306 EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state, 307 EMC2305_FAN_MAX); 308 data->cdev_data[cdev_idx].last_hwmon_state = 309 EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state, 310 EMC2305_FAN_MAX); 311 return 0; 312 } 313 314 static int emc2305_set_tz(struct device *dev) 315 { 316 struct emc2305_data *data = dev_get_drvdata(dev); 317 int i, ret; 318 319 if (!data->pwm_separate) 320 return emc2305_set_single_tz(dev, 0); 321 322 for (i = 0; i < data->pwm_num; i++) { 323 ret = emc2305_set_single_tz(dev, i + 1); 324 if (ret) 325 goto thermal_cooling_device_register_fail; 326 } 327 return 0; 328 329 thermal_cooling_device_register_fail: 330 emc2305_unset_tz(dev); 331 return ret; 332 } 333 334 static void emc2305_unset_tz(struct device *dev) 335 { 336 struct emc2305_data *data = dev_get_drvdata(dev); 337 int i; 338 339 /* Unregister cooling device. */ 340 for (i = 0; i < EMC2305_PWM_MAX; i++) 341 if (data->cdev_data[i].cdev) 342 thermal_cooling_device_unregister(data->cdev_data[i].cdev); 343 } 344 345 static umode_t 346 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel) 347 { 348 int max_channel = emc2305_get_max_channel(data); 349 350 /* Don't show channels which are not physically connected. */ 351 if (channel >= max_channel) 352 return 0; 353 switch (type) { 354 case hwmon_fan: 355 switch (attr) { 356 case hwmon_fan_input: 357 return 0444; 358 case hwmon_fan_fault: 359 return 0444; 360 default: 361 break; 362 } 363 break; 364 case hwmon_pwm: 365 switch (attr) { 366 case hwmon_pwm_input: 367 return 0644; 368 default: 369 break; 370 } 371 break; 372 default: 373 break; 374 } 375 376 return 0; 377 }; 378 379 static int 380 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val) 381 { 382 struct emc2305_data *data = dev_get_drvdata(dev); 383 int cdev_idx; 384 385 switch (type) { 386 case hwmon_pwm: 387 switch (attr) { 388 case hwmon_pwm_input: 389 /* If thermal is configured - handle PWM limit setting. */ 390 if (IS_REACHABLE(CONFIG_THERMAL)) { 391 if (data->pwm_separate) 392 cdev_idx = channel; 393 else 394 cdev_idx = 0; 395 data->cdev_data[cdev_idx].last_hwmon_state = 396 EMC2305_PWM_DUTY2STATE(val, data->max_state, 397 EMC2305_FAN_MAX); 398 /* 399 * Update PWM only in case requested state is not less than the 400 * last thermal state. 401 */ 402 if (data->cdev_data[cdev_idx].last_hwmon_state >= 403 data->cdev_data[cdev_idx].last_thermal_state) 404 return emc2305_set_cur_state(data->cdev_data[cdev_idx].cdev, 405 data->cdev_data[cdev_idx].last_hwmon_state); 406 return 0; 407 } 408 return emc2305_set_pwm(dev, val, channel); 409 default: 410 break; 411 } 412 break; 413 default: 414 break; 415 } 416 417 return -EOPNOTSUPP; 418 }; 419 420 static int 421 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) 422 { 423 int ret; 424 425 switch (type) { 426 case hwmon_fan: 427 switch (attr) { 428 case hwmon_fan_input: 429 ret = emc2305_show_fan(dev, channel); 430 if (ret < 0) 431 return ret; 432 *val = ret; 433 return 0; 434 case hwmon_fan_fault: 435 ret = emc2305_show_fault(dev, channel); 436 if (ret < 0) 437 return ret; 438 *val = ret; 439 return 0; 440 default: 441 break; 442 } 443 break; 444 case hwmon_pwm: 445 switch (attr) { 446 case hwmon_pwm_input: 447 ret = emc2305_show_pwm(dev, channel); 448 if (ret < 0) 449 return ret; 450 *val = ret; 451 return 0; 452 default: 453 break; 454 } 455 break; 456 default: 457 break; 458 } 459 460 return -EOPNOTSUPP; 461 }; 462 463 static const struct hwmon_ops emc2305_ops = { 464 .is_visible = emc2305_is_visible, 465 .read = emc2305_read, 466 .write = emc2305_write, 467 }; 468 469 static const struct hwmon_channel_info *emc2305_info[] = { 470 HWMON_CHANNEL_INFO(fan, 471 HWMON_F_INPUT | HWMON_F_FAULT, 472 HWMON_F_INPUT | HWMON_F_FAULT, 473 HWMON_F_INPUT | HWMON_F_FAULT, 474 HWMON_F_INPUT | HWMON_F_FAULT, 475 HWMON_F_INPUT | HWMON_F_FAULT), 476 HWMON_CHANNEL_INFO(pwm, 477 HWMON_PWM_INPUT, 478 HWMON_PWM_INPUT, 479 HWMON_PWM_INPUT, 480 HWMON_PWM_INPUT, 481 HWMON_PWM_INPUT), 482 NULL 483 }; 484 485 static const struct hwmon_chip_info emc2305_chip_info = { 486 .ops = &emc2305_ops, 487 .info = emc2305_info, 488 }; 489 490 static int emc2305_identify(struct device *dev) 491 { 492 struct i2c_client *client = to_i2c_client(dev); 493 struct emc2305_data *data = i2c_get_clientdata(client); 494 int ret; 495 496 ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID); 497 if (ret < 0) 498 return ret; 499 500 switch (ret) { 501 case EMC2305: 502 data->pwm_num = 5; 503 break; 504 case EMC2303: 505 data->pwm_num = 3; 506 break; 507 case EMC2302: 508 data->pwm_num = 2; 509 break; 510 case EMC2301: 511 data->pwm_num = 1; 512 break; 513 default: 514 return -ENODEV; 515 } 516 517 return 0; 518 } 519 520 static int emc2305_probe(struct i2c_client *client) 521 { 522 struct i2c_adapter *adapter = client->adapter; 523 struct device *dev = &client->dev; 524 struct emc2305_data *data; 525 struct emc2305_platform_data *pdata; 526 int vendor; 527 int ret; 528 int i; 529 530 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) 531 return -ENODEV; 532 533 vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR); 534 if (vendor != EMC2305_VENDOR) 535 return -ENODEV; 536 537 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 538 if (!data) 539 return -ENOMEM; 540 541 i2c_set_clientdata(client, data); 542 data->client = client; 543 544 ret = emc2305_identify(dev); 545 if (ret) 546 return ret; 547 548 pdata = dev_get_platdata(&client->dev); 549 if (pdata) { 550 if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE) 551 return -EINVAL; 552 data->max_state = pdata->max_state; 553 /* 554 * Validate a number of active PWM channels. Note that 555 * configured number can be less than the actual maximum 556 * supported by the device. 557 */ 558 if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX) 559 return -EINVAL; 560 data->pwm_num = pdata->pwm_num; 561 data->pwm_separate = pdata->pwm_separate; 562 for (i = 0; i < EMC2305_PWM_MAX; i++) 563 data->pwm_min[i] = pdata->pwm_min[i]; 564 } else { 565 data->max_state = EMC2305_FAN_MAX_STATE; 566 data->pwm_separate = false; 567 for (i = 0; i < EMC2305_PWM_MAX; i++) 568 data->pwm_min[i] = EMC2305_FAN_MIN; 569 } 570 571 data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data, 572 &emc2305_chip_info, NULL); 573 if (IS_ERR(data->hwmon_dev)) 574 return PTR_ERR(data->hwmon_dev); 575 576 if (IS_REACHABLE(CONFIG_THERMAL)) { 577 ret = emc2305_set_tz(dev); 578 if (ret != 0) 579 return ret; 580 } 581 582 for (i = 0; i < data->pwm_num; i++) { 583 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i), 584 data->pwm_min[i]); 585 if (ret < 0) 586 return ret; 587 } 588 589 return 0; 590 } 591 592 static void emc2305_remove(struct i2c_client *client) 593 { 594 struct device *dev = &client->dev; 595 596 if (IS_REACHABLE(CONFIG_THERMAL)) 597 emc2305_unset_tz(dev); 598 } 599 600 static struct i2c_driver emc2305_driver = { 601 .class = I2C_CLASS_HWMON, 602 .driver = { 603 .name = "emc2305", 604 }, 605 .probe_new = emc2305_probe, 606 .remove = emc2305_remove, 607 .id_table = emc2305_ids, 608 .address_list = emc2305_normal_i2c, 609 }; 610 611 module_i2c_driver(emc2305_driver); 612 613 MODULE_AUTHOR("Nvidia"); 614 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver"); 615 MODULE_LICENSE("GPL"); 616