#include #include #include #include #include #include #include #include #include "icx3.h" /* Check an I2C device file for an ICX3 controller, returns the product id, or ICX3_PRODUCT_NONE on bad read. */ enum icx3_product_id check_for_icx3(char *i2c_dev_path) { char data[I2C_SMBUS_BLOCK_MAX] = {}; int fd, read_result; struct icx3_info *temp_info; fd = open_i2c_dev(i2c_dev_path); if (fd == -1) return ICX3_PRODUCT_NONE; read_result = i2c_smbus_read_i2c_block_data(fd, ICX3_REG_READINFORMATION, ICX3_READINFORMATION_SIZE, data); close(fd); if (read_result == ICX3_READINFORMATION_SIZE) { temp_info = (struct icx3_info*)&data; if (temp_info->slave_address == ICX3_I2C_ADDR) return temp_info->product_id; } return ICX3_PRODUCT_NONE; } void print_icx3_fans(char *i2c_dev_path) { struct icx3_fan_control fans[ICX3_MAX_FANS]; get_fan_status(fans, i2c_dev_path); for (int i=0; i < ICX3_MAX_FANS; i++) { printf("%s: %d RPM (%d/%d%%, %s)\n", icx3_fan_names[i], fans[i].rpm_status, fans[i].duty_status, fans[i].duty, icx3_fan_mode_names[fans[i].fanmode] ); } } void print_icx3_fans_oneline(char *i2c_dev_path) { struct icx3_fan_control fans[ICX3_MAX_FANS]; get_fan_status(fans, i2c_dev_path); printf("FAN"); for (int i=0; i < ICX3_MAX_FANS; i++) { printf(" %3d", fans[i].duty_status); } printf("%%"); } void print_icx3_temps(char *i2c_dev_path) { float temps[ICX3_NUM_TEMP_SENSORS]; get_temp_sensors(temps, i2c_dev_path); for (int i=0; idata[2*i+1] << 8) | (short)(temp_sensors->data[2*i]); /* temp is reported in tenths of deg C */ temps[i] = (float)cur_data/10; } } int open_i2c_dev(char *i2c_dev_path) { int fd = open(i2c_dev_path, O_RDONLY); /* Error silently here because we should have already checked we can read the i2c in check_for_icx3() */ if (fd == -1) return -1; if (ioctl(fd, I2C_SLAVE, ICX3_I2C_ADDR) < 0) { close(fd); return -1; } return fd; } void enable_write(int enable, char *i2c_dev_path) { int fd = open_i2c_dev(i2c_dev_path); if (fd == -1) return; unsigned char *data; if (enable) data = icx3_write_enable; else data = icx3_write_disable; /* Enable or disable write */ i2c_smbus_write_i2c_block_data(fd, ICX3_REG_ENABLEWRITE, ICX3_ENABLEWRITE_SIZE, data); /* Read back the result to verify */ unsigned char read_result[ICX3_ENABLEWRITE_SIZE]; int write_ok = 0; i2c_smbus_read_i2c_block_data(fd, ICX3_REG_ENABLEWRITE, ICX3_ENABLEWRITE_SIZE, read_result); if (enable) write_ok = (read_result[1] == 0xFC); else write_ok = (read_result[1] == 0xFE); if (!write_ok) printf("Unable to enable/disable write on %s\n", i2c_dev_path); } /* Sets a given fan on the GPU according to a string setting 'auto' resets the fan to default (fans 1-2 go to GPU control, 3-4 to +0 RPM offset from GPU control) A number without a sign (e.g. 50) manually sets the fan to a given % duty cycle A number with a sign (e.g. +500) sets the fan to run at an RPM offset from GPU control */ void set_fan(int fan, char *setting, char *i2c_dev_path) { char fans_avail[ICX3_MAX_FANS] = {0}; struct icx3_fan_control fan_control = {}; struct icx3_fan_control fan_readback = {}; char reg = ICX3_REG_FANCONTROL + fan; int write_result; /* Check to make sure we're setting a valid fan */ get_available_fans(i2c_dev_path, fans_avail); if (fans_avail[fan] == 0) { printf("Fan %d does not exist on this card \n", fan); return; } int fp = open_i2c_dev(i2c_dev_path); if (fp == -1) return; fan_control.length = ICX3_FANCONTROL_SIZE - 1; if (strcmp(setting, "auto") == 0) { /* auto setting */ fan_control.rpm_offset = 0; if (fan <= 1) fan_control.fanmode = ICX3_FANMODE_GPU_CONTROL; else fan_control.fanmode = ICX3_FANMODE_GPU_RPM_OFFSET; } else if (setting[0] == '+' || setting[0] == '-') { /* RPM offset */ fan_control.fanmode = ICX3_FANMODE_GPU_RPM_OFFSET; fan_control.rpm_offset = atoi(setting); } else { fan_control.fanmode = ICX3_FANMODE_RPM; fan_control.duty = atoi(setting); } enable_write(1, i2c_dev_path); i2c_smbus_write_i2c_block_data(fp, reg, ICX3_FANCONTROL_SIZE, (char *)&fan_control); /* Read back data and verify we set the fan properly */ i2c_smbus_read_i2c_block_data(fp, reg, ICX3_FANCONTROL_SIZE, (char *)&fan_readback); if (fan_readback.fanmode != fan_control.fanmode || fan_readback.rpm_offset != fan_control.rpm_offset || fan_readback.duty != fan_control.duty) printf("Error setting fan %d on %s\n", fan, i2c_dev_path); close(fp); }