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 #include <linux/pwm.h> 15 #include <linux/of_device.h> 16 #include <linux/util_macros.h> 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 #define EMC2305_DEFAULT_OUTPUT 0x0 30 #define EMC2305_DEFAULT_POLARITY 0x0 31 #define EMC2305_REG_POLARITY 0x2a 32 #define EMC2305_REG_DRIVE_PWM_OUT 0x2b 33 #define EMC2305_OPEN_DRAIN 0x0 34 #define EMC2305_PUSH_PULL 0x1 35 #define EMC2305_PWM_SHUTDOWN_UNSET -1 36 37 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \ 38 DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max)) 39 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \ 40 DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state)) 41 42 /* 43 * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges 44 * equal (poles * 2 + 1). 45 */ 46 #define EMC2305_RPM_FACTOR 3932160 47 48 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n)) 49 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n)) 50 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n)) 51 52 /* Supported base PWM frequencies */ 53 static const unsigned int base_freq_table[] = { 2441, 4882, 19530, 26000 }; 54 55 enum emc230x_product_id { 56 EMC2305 = 0x34, 57 EMC2303 = 0x35, 58 EMC2302 = 0x36, 59 EMC2301 = 0x37, 60 }; 61 62 static const struct i2c_device_id emc2305_ids[] = { 63 { .name = "emc2305" }, 64 { .name = "emc2303" }, 65 { .name = "emc2302" }, 66 { .name = "emc2301" }, 67 { } 68 }; 69 MODULE_DEVICE_TABLE(i2c, emc2305_ids); 70 71 /** 72 * struct emc2305_cdev_data - device-specific cooling device state 73 * @cdev: cooling device 74 * @cur_state: cooling current state 75 * @last_hwmon_state: last cooling state updated by hwmon subsystem 76 * @last_thermal_state: last cooling state updated by thermal subsystem 77 * 78 * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit 79 * speed feature. The purpose of this feature is to provides ability to limit fan speed 80 * according to some system wise considerations, like absence of some replaceable units (PSU or 81 * line cards), high system ambient temperature, unreliable transceivers temperature sensing or 82 * some other factors which indirectly impacts system's airflow 83 * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is 84 * used for setting low limit for fan speed in case 'thermal' subsystem is configured in 85 * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal' 86 * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm' 87 * attribute. 88 * From other side, fan speed is to be updated in hardware through 'pwm' only in case the 89 * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan 90 * speed will be just stored with no PWM update. 91 */ 92 struct emc2305_cdev_data { 93 struct thermal_cooling_device *cdev; 94 unsigned int cur_state; 95 unsigned long last_hwmon_state; 96 unsigned long last_thermal_state; 97 }; 98 99 /** 100 * struct emc2305_data - device-specific data 101 * @client: i2c client 102 * @hwmon_dev: hwmon device 103 * @max_state: maximum cooling state of the cooling device 104 * @pwm_num: number of PWM channels 105 * @pwm_output_mask: PWM output mask 106 * @pwm_polarity_mask: PWM polarity mask 107 * @pwm_separate: separate PWM settings for every channel 108 * @pwm_shutdown: Set shutdown PWM. 109 * @pwm_min: array of minimum PWM per channel 110 * @pwm_freq: array of PWM frequency per channel 111 * @cdev_data: array of cooling devices data 112 */ 113 struct emc2305_data { 114 struct i2c_client *client; 115 struct device *hwmon_dev; 116 u8 max_state; 117 u8 pwm_num; 118 u8 pwm_output_mask; 119 u8 pwm_polarity_mask; 120 bool pwm_separate; 121 s16 pwm_shutdown[EMC2305_PWM_MAX]; 122 u8 pwm_min[EMC2305_PWM_MAX]; 123 u16 pwm_freq[EMC2305_PWM_MAX]; 124 struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX]; 125 }; 126 127 static char *emc2305_fan_name[] = { 128 "emc2305_fan", 129 "emc2305_fan1", 130 "emc2305_fan2", 131 "emc2305_fan3", 132 "emc2305_fan4", 133 "emc2305_fan5", 134 }; 135 136 static int emc2305_get_max_channel(const struct emc2305_data *data) 137 { 138 return data->pwm_num; 139 } 140 141 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev) 142 { 143 struct emc2305_data *data = cdev->devdata; 144 size_t len = strlen(cdev->type); 145 int ret; 146 147 if (len <= 0) 148 return -EINVAL; 149 150 /* 151 * Returns index of cooling device 0..4 in case of separate PWM setting. 152 * Zero index is used in case of one common PWM setting. 153 * If the mode is not set as pwm_separate, all PWMs are to be bound 154 * to the common thermal zone and should work at the same speed 155 * to perform cooling for the same thermal junction. 156 * Otherwise, return specific channel that will be used in bound 157 * related PWM to the thermal zone. 158 */ 159 if (!data->pwm_separate) 160 return 0; 161 162 ret = cdev->type[len - 1]; 163 switch (ret) { 164 case '1' ... '5': 165 return ret - '1'; 166 default: 167 break; 168 } 169 return -EINVAL; 170 } 171 172 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state) 173 { 174 int cdev_idx; 175 struct emc2305_data *data = cdev->devdata; 176 177 cdev_idx = emc2305_get_cdev_idx(cdev); 178 if (cdev_idx < 0) 179 return cdev_idx; 180 181 *state = data->cdev_data[cdev_idx].cur_state; 182 return 0; 183 } 184 185 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state) 186 { 187 struct emc2305_data *data = cdev->devdata; 188 *state = data->max_state; 189 return 0; 190 } 191 192 static int __emc2305_set_cur_state(struct emc2305_data *data, int cdev_idx, unsigned long state) 193 { 194 int ret; 195 struct i2c_client *client = data->client; 196 u8 val, i; 197 198 state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state); 199 200 val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX); 201 202 data->cdev_data[cdev_idx].cur_state = state; 203 if (data->pwm_separate) { 204 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val); 205 if (ret < 0) 206 return ret; 207 } else { 208 /* 209 * Set the same PWM value in all channels 210 * if common PWM channel is used. 211 */ 212 for (i = 0; i < data->pwm_num; i++) { 213 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val); 214 if (ret < 0) 215 return ret; 216 } 217 } 218 219 return 0; 220 } 221 222 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) 223 { 224 int cdev_idx, ret; 225 struct emc2305_data *data = cdev->devdata; 226 227 if (state > data->max_state) 228 return -EINVAL; 229 230 cdev_idx = emc2305_get_cdev_idx(cdev); 231 if (cdev_idx < 0) 232 return cdev_idx; 233 234 /* Save thermal state. */ 235 data->cdev_data[cdev_idx].last_thermal_state = state; 236 ret = __emc2305_set_cur_state(data, cdev_idx, state); 237 if (ret < 0) 238 return ret; 239 240 return 0; 241 } 242 243 static const struct thermal_cooling_device_ops emc2305_cooling_ops = { 244 .get_max_state = emc2305_get_max_state, 245 .get_cur_state = emc2305_get_cur_state, 246 .set_cur_state = emc2305_set_cur_state, 247 }; 248 249 static int emc2305_show_fault(struct device *dev, int channel) 250 { 251 struct emc2305_data *data = dev_get_drvdata(dev); 252 struct i2c_client *client = data->client; 253 int status_reg; 254 255 status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS); 256 if (status_reg < 0) 257 return status_reg; 258 259 return status_reg & (1 << channel) ? 1 : 0; 260 } 261 262 static int emc2305_show_fan(struct device *dev, int channel) 263 { 264 struct emc2305_data *data = dev_get_drvdata(dev); 265 struct i2c_client *client = data->client; 266 int ret; 267 268 ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel)); 269 if (ret <= 0) 270 return ret; 271 272 ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS; 273 ret = EMC2305_RPM_FACTOR / ret; 274 if (ret <= EMC2305_TACH_RANGE_MIN) 275 return 0; 276 277 return ret * EMC2305_TACH_CNT_MULTIPLIER; 278 } 279 280 static int emc2305_show_pwm(struct device *dev, int channel) 281 { 282 struct emc2305_data *data = dev_get_drvdata(dev); 283 struct i2c_client *client = data->client; 284 285 return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel)); 286 } 287 288 static int emc2305_set_pwm(struct device *dev, long val, int channel) 289 { 290 struct emc2305_data *data = dev_get_drvdata(dev); 291 struct i2c_client *client = data->client; 292 int ret; 293 294 if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX) 295 return -EINVAL; 296 297 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val); 298 if (ret < 0) 299 return ret; 300 data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state, 301 EMC2305_FAN_MAX); 302 return 0; 303 } 304 305 static int emc2305_set_single_tz(struct device *dev, struct device_node *fan_node, int idx) 306 { 307 struct emc2305_data *data = dev_get_drvdata(dev); 308 long pwm; 309 int i, cdev_idx, ret; 310 311 cdev_idx = (idx) ? idx - 1 : 0; 312 pwm = data->pwm_min[cdev_idx]; 313 314 data->cdev_data[cdev_idx].cdev = 315 devm_thermal_of_child_cooling_device_register(dev, fan_node, 316 emc2305_fan_name[idx], data, 317 &emc2305_cooling_ops); 318 319 if (IS_ERR(data->cdev_data[cdev_idx].cdev)) { 320 dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]); 321 return PTR_ERR(data->cdev_data[cdev_idx].cdev); 322 } 323 324 if (data->cdev_data[cdev_idx].cur_state > 0) 325 /* Update pwm when temperature is above trips */ 326 pwm = EMC2305_PWM_STATE2DUTY(data->cdev_data[cdev_idx].cur_state, 327 data->max_state, EMC2305_FAN_MAX); 328 329 /* Set minimal PWM speed. */ 330 if (data->pwm_separate) { 331 ret = emc2305_set_pwm(dev, pwm, cdev_idx); 332 if (ret < 0) 333 return ret; 334 } else { 335 for (i = 0; i < data->pwm_num; i++) { 336 ret = emc2305_set_pwm(dev, pwm, i); 337 if (ret < 0) 338 return ret; 339 } 340 } 341 data->cdev_data[cdev_idx].cur_state = 342 EMC2305_PWM_DUTY2STATE(pwm, data->max_state, 343 EMC2305_FAN_MAX); 344 data->cdev_data[cdev_idx].last_hwmon_state = 345 EMC2305_PWM_DUTY2STATE(pwm, data->max_state, 346 EMC2305_FAN_MAX); 347 return 0; 348 } 349 350 static int emc2305_set_tz(struct device *dev) 351 { 352 struct emc2305_data *data = dev_get_drvdata(dev); 353 int i, ret; 354 355 if (!data->pwm_separate) 356 return emc2305_set_single_tz(dev, dev->of_node, 0); 357 358 for (i = 0; i < data->pwm_num; i++) { 359 ret = emc2305_set_single_tz(dev, dev->of_node, i + 1); 360 if (ret) 361 return ret; 362 } 363 return 0; 364 } 365 366 static umode_t 367 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel) 368 { 369 int max_channel = emc2305_get_max_channel(data); 370 371 /* Don't show channels which are not physically connected. */ 372 if (channel >= max_channel) 373 return 0; 374 switch (type) { 375 case hwmon_fan: 376 switch (attr) { 377 case hwmon_fan_input: 378 return 0444; 379 case hwmon_fan_fault: 380 return 0444; 381 default: 382 break; 383 } 384 break; 385 case hwmon_pwm: 386 switch (attr) { 387 case hwmon_pwm_input: 388 return 0644; 389 default: 390 break; 391 } 392 break; 393 default: 394 break; 395 } 396 397 return 0; 398 }; 399 400 static int 401 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val) 402 { 403 struct emc2305_data *data = dev_get_drvdata(dev); 404 int cdev_idx; 405 406 switch (type) { 407 case hwmon_pwm: 408 switch (attr) { 409 case hwmon_pwm_input: 410 /* If thermal is configured - handle PWM limit setting. */ 411 if (IS_REACHABLE(CONFIG_THERMAL)) { 412 if (data->pwm_separate) 413 cdev_idx = channel; 414 else 415 cdev_idx = 0; 416 data->cdev_data[cdev_idx].last_hwmon_state = 417 EMC2305_PWM_DUTY2STATE(val, data->max_state, 418 EMC2305_FAN_MAX); 419 /* 420 * Update PWM only in case requested state is not less than the 421 * last thermal state. 422 */ 423 if (data->cdev_data[cdev_idx].last_hwmon_state >= 424 data->cdev_data[cdev_idx].last_thermal_state) 425 return __emc2305_set_cur_state(data, cdev_idx, 426 data->cdev_data[cdev_idx].last_hwmon_state); 427 return 0; 428 } 429 return emc2305_set_pwm(dev, val, channel); 430 default: 431 break; 432 } 433 break; 434 default: 435 break; 436 } 437 438 return -EOPNOTSUPP; 439 }; 440 441 static int 442 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) 443 { 444 int ret; 445 446 switch (type) { 447 case hwmon_fan: 448 switch (attr) { 449 case hwmon_fan_input: 450 ret = emc2305_show_fan(dev, channel); 451 if (ret < 0) 452 return ret; 453 *val = ret; 454 return 0; 455 case hwmon_fan_fault: 456 ret = emc2305_show_fault(dev, channel); 457 if (ret < 0) 458 return ret; 459 *val = ret; 460 return 0; 461 default: 462 break; 463 } 464 break; 465 case hwmon_pwm: 466 switch (attr) { 467 case hwmon_pwm_input: 468 ret = emc2305_show_pwm(dev, channel); 469 if (ret < 0) 470 return ret; 471 *val = ret; 472 return 0; 473 default: 474 break; 475 } 476 break; 477 default: 478 break; 479 } 480 481 return -EOPNOTSUPP; 482 }; 483 484 static const struct hwmon_ops emc2305_ops = { 485 .is_visible = emc2305_is_visible, 486 .read = emc2305_read, 487 .write = emc2305_write, 488 }; 489 490 static const struct hwmon_channel_info * const emc2305_info[] = { 491 HWMON_CHANNEL_INFO(fan, 492 HWMON_F_INPUT | HWMON_F_FAULT, 493 HWMON_F_INPUT | HWMON_F_FAULT, 494 HWMON_F_INPUT | HWMON_F_FAULT, 495 HWMON_F_INPUT | HWMON_F_FAULT, 496 HWMON_F_INPUT | HWMON_F_FAULT), 497 HWMON_CHANNEL_INFO(pwm, 498 HWMON_PWM_INPUT, 499 HWMON_PWM_INPUT, 500 HWMON_PWM_INPUT, 501 HWMON_PWM_INPUT, 502 HWMON_PWM_INPUT), 503 NULL 504 }; 505 506 static const struct hwmon_chip_info emc2305_chip_info = { 507 .ops = &emc2305_ops, 508 .info = emc2305_info, 509 }; 510 511 static int emc2305_identify(struct device *dev) 512 { 513 struct i2c_client *client = to_i2c_client(dev); 514 struct emc2305_data *data = i2c_get_clientdata(client); 515 int ret; 516 517 ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID); 518 if (ret < 0) 519 return ret; 520 521 switch (ret) { 522 case EMC2305: 523 data->pwm_num = 5; 524 break; 525 case EMC2303: 526 data->pwm_num = 3; 527 break; 528 case EMC2302: 529 data->pwm_num = 2; 530 break; 531 case EMC2301: 532 data->pwm_num = 1; 533 break; 534 default: 535 return -ENODEV; 536 } 537 538 return 0; 539 } 540 541 static int emc2305_of_parse_pwm_child(struct device *dev, 542 struct device_node *child, 543 struct emc2305_data *data) 544 { u32 ch; 545 u32 pwm_shutdown_percent; 546 int ret; 547 struct of_phandle_args args; 548 549 ret = of_property_read_u32(child, "reg", &ch); 550 if (ret) { 551 dev_err(dev, "missing reg property of %pOFn\n", child); 552 return ret; 553 } 554 555 if (ch >= data->pwm_num) { 556 dev_err(dev, "invalid reg %u for node %pOF (valid range 0-%u)\n", ch, child, 557 data->pwm_num - 1); 558 return -EINVAL; 559 } 560 561 ret = of_parse_phandle_with_args(child, "pwms", "#pwm-cells", 0, &args); 562 563 if (ret) 564 return ret; 565 566 if (args.args_count > 0) { 567 data->pwm_freq[ch] = find_closest(args.args[0], base_freq_table, 568 ARRAY_SIZE(base_freq_table)); 569 } else { 570 data->pwm_freq[ch] = base_freq_table[3]; 571 } 572 573 if (args.args_count > 1) { 574 if (args.args[1] == PWM_POLARITY_NORMAL || args.args[1] == PWM_POLARITY_INVERSED) 575 data->pwm_polarity_mask |= args.args[1] << ch; 576 else 577 dev_err(dev, "Wrong PWM polarity config provided: %d\n", args.args[0]); 578 } else { 579 data->pwm_polarity_mask |= PWM_POLARITY_NORMAL << ch; 580 } 581 582 if (args.args_count > 2) { 583 if (args.args[2] == EMC2305_PUSH_PULL || args.args[2] <= EMC2305_OPEN_DRAIN) 584 data->pwm_output_mask |= args.args[2] << ch; 585 else 586 dev_err(dev, "Wrong PWM output config provided: %d\n", args.args[1]); 587 } else { 588 data->pwm_output_mask |= EMC2305_OPEN_DRAIN << ch; 589 } 590 591 of_node_put(args.np); 592 593 ret = of_property_read_u32(child, "fan-shutdown-percent", 594 &pwm_shutdown_percent); 595 596 if (!ret) { 597 pwm_shutdown_percent = clamp(pwm_shutdown_percent, 0, 100); 598 data->pwm_shutdown[ch] = 599 DIV_ROUND_CLOSEST(pwm_shutdown_percent * EMC2305_FAN_MAX, 100); 600 } 601 602 return 0; 603 } 604 605 static int emc2305_probe_childs_from_dt(struct device *dev) 606 { 607 struct emc2305_data *data = dev_get_drvdata(dev); 608 struct device_node *child; 609 int ret, count = 0; 610 611 data->pwm_output_mask = 0x0; 612 data->pwm_polarity_mask = 0x0; 613 614 for_each_child_of_node(dev->of_node, child) { 615 if (of_property_present(child, "reg")) { 616 ret = emc2305_of_parse_pwm_child(dev, child, data); 617 if (ret) 618 continue; 619 count++; 620 } 621 } 622 return count; 623 } 624 625 static int emc2305_probe(struct i2c_client *client) 626 { 627 struct i2c_adapter *adapter = client->adapter; 628 struct device *dev = &client->dev; 629 struct emc2305_data *data; 630 struct emc2305_platform_data *pdata; 631 int vendor; 632 int ret; 633 int i; 634 int pwm_childs; 635 u32 ch; 636 637 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) 638 return -ENODEV; 639 640 vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR); 641 if (vendor != EMC2305_VENDOR) 642 return -ENODEV; 643 644 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 645 if (!data) 646 return -ENOMEM; 647 648 i2c_set_clientdata(client, data); 649 data->client = client; 650 651 ret = emc2305_identify(dev); 652 if (ret) 653 return ret; 654 655 for (i = 0; i < EMC2305_PWM_MAX; i++) 656 data->pwm_shutdown[i] = EMC2305_PWM_SHUTDOWN_UNSET; 657 658 pwm_childs = emc2305_probe_childs_from_dt(dev); 659 660 pdata = dev_get_platdata(&client->dev); 661 662 if (!pwm_childs) { 663 if (pdata) { 664 if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE) 665 return -EINVAL; 666 data->max_state = pdata->max_state; 667 /* 668 * Validate a number of active PWM channels. Note that 669 * configured number can be less than the actual maximum 670 * supported by the device. 671 */ 672 if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX) 673 return -EINVAL; 674 data->pwm_num = pdata->pwm_num; 675 data->pwm_output_mask = pdata->pwm_output_mask; 676 data->pwm_polarity_mask = pdata->pwm_polarity_mask; 677 data->pwm_separate = pdata->pwm_separate; 678 for (i = 0; i < EMC2305_PWM_MAX; i++) { 679 data->pwm_min[i] = pdata->pwm_min[i]; 680 data->pwm_freq[i] = pdata->pwm_freq[i]; 681 } 682 } else { 683 data->max_state = EMC2305_FAN_MAX_STATE; 684 data->pwm_separate = false; 685 data->pwm_output_mask = EMC2305_DEFAULT_OUTPUT; 686 data->pwm_polarity_mask = EMC2305_DEFAULT_POLARITY; 687 for (i = 0; i < EMC2305_PWM_MAX; i++) { 688 data->pwm_min[i] = EMC2305_FAN_MIN; 689 data->pwm_freq[i] = base_freq_table[3]; 690 } 691 } 692 } else { 693 data->max_state = EMC2305_FAN_MAX_STATE; 694 data->pwm_separate = false; 695 for (i = 0; i < EMC2305_PWM_MAX; i++) 696 data->pwm_min[i] = EMC2305_FAN_MIN; 697 } 698 699 data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data, 700 &emc2305_chip_info, NULL); 701 if (IS_ERR(data->hwmon_dev)) 702 return PTR_ERR(data->hwmon_dev); 703 704 if (IS_REACHABLE(CONFIG_THERMAL)) { 705 /* Parse and check for the available PWM child nodes */ 706 if (pwm_childs > 0) { 707 for_each_child_of_node_scoped(dev->of_node, child) { 708 ret = of_property_read_u32(child, "reg", &ch); 709 if (ret || ch >= data->pwm_num) 710 continue; 711 712 /* 713 * emc2305_set_single_tz() uses 0 for the common cooling 714 * device and 1..pwm_num for individual fan channels. 715 */ 716 ret = emc2305_set_single_tz(dev, child, ch + 1); 717 if (ret != 0) 718 return ret; 719 } 720 } else { 721 ret = emc2305_set_tz(dev); 722 if (ret != 0) 723 return ret; 724 } 725 } 726 727 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_DRIVE_PWM_OUT, 728 data->pwm_output_mask); 729 if (ret < 0) 730 dev_err(dev, "Failed to configure pwm output, using default\n"); 731 732 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_POLARITY, 733 data->pwm_polarity_mask); 734 if (ret < 0) 735 dev_err(dev, "Failed to configure pwm polarity, using default\n"); 736 737 for (i = 0; i < data->pwm_num; i++) { 738 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i), 739 data->pwm_min[i]); 740 if (ret < 0) 741 return ret; 742 } 743 744 return 0; 745 } 746 747 static void emc2305_shutdown(struct i2c_client *client) 748 { 749 int i; 750 int ret; 751 struct emc2305_data *data = i2c_get_clientdata(client); 752 753 for (i = 0; i < data->pwm_num; i++) { 754 if (data->pwm_shutdown[i] != EMC2305_PWM_SHUTDOWN_UNSET) { 755 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), 756 data->pwm_shutdown[i]); 757 if (ret < 0) 758 dev_warn(&client->dev, 759 "Failed to set shutdown PWM for ch %d\n", i); 760 } 761 } 762 } 763 764 static const struct of_device_id of_emc2305_match_table[] = { 765 { .compatible = "microchip,emc2305", }, 766 {}, 767 }; 768 MODULE_DEVICE_TABLE(of, of_emc2305_match_table); 769 770 static struct i2c_driver emc2305_driver = { 771 .driver = { 772 .name = "emc2305", 773 .of_match_table = of_emc2305_match_table, 774 }, 775 .probe = emc2305_probe, 776 .shutdown = emc2305_shutdown, 777 .id_table = emc2305_ids, 778 }; 779 780 module_i2c_driver(emc2305_driver); 781 782 MODULE_AUTHOR("Nvidia"); 783 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver"); 784 MODULE_LICENSE("GPL"); 785