Added fan controls

This commit is contained in:
2025-01-30 01:15:18 -08:00
parent bcd4457c2f
commit 3a3fa98eab
5 changed files with 289 additions and 83 deletions
+155 -63
View File
@@ -4,6 +4,8 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "icx3.h"
@@ -14,15 +16,10 @@ enum icx3_product_id check_for_icx3(char *i2c_dev_path)
int fd, read_result;
struct icx3_info *temp_info;
fd = open(i2c_dev_path, O_RDONLY);
if (!fd)
fd = open_i2c_dev(i2c_dev_path);
if (fd == -1)
return ICX3_PRODUCT_NONE;
if (ioctl(fd, I2C_SLAVE, ICX3_I2C_ADDR) < 0) {
close(fd);
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) {
@@ -42,7 +39,68 @@ void print_icx3_fans(char *i2c_dev_path)
struct icx3_fan_control *fan_status;
/* First thing is to check for the product ID to figure out how many fans we have */
int fans_avail[ICX3_MAX_FANS] = {0};
char fans_avail[ICX3_MAX_FANS] = {0};
get_available_fans(i2c_dev_path, fans_avail);
fd = open_i2c_dev(i2c_dev_path);
if (fd == -1)
return;
for (int i=0; i < ICX3_MAX_FANS; i++) {
if (!fans_avail[i])
continue;
read_result = i2c_smbus_read_i2c_block_data(fd, ICX3_REG_FANCONTROL + i, ICX3_FANCONTROL_SIZE, data);
if (read_result != ICX3_FANCONTROL_SIZE) {
close(fd);
return;
}
fan_status = (struct icx3_fan_control*) &data;
printf("%s: %d RPM (%d/%d%%, %s)\n",
icx3_fan_names[i],
fan_status->rpm_status,
fan_status->duty_status,
fan_status->duty,
icx3_fan_mode_names[fan_status->fanmode]
);
}
close(fd);
}
void print_icx3_temps(char *i2c_dev_path)
{
char data[I2C_SMBUS_BLOCK_MAX] = {};
int fd, read_result;
struct icx3_temp_sensors *temp_sensors;
fd = open_i2c_dev(i2c_dev_path);
if (fd == -1)
return;
read_result = i2c_smbus_read_i2c_block_data(fd, ICX3_REG_TEMPSENSOR, ICX3_TEMPSENSOR_SIZE, data);
if (read_result != ICX3_TEMPSENSOR_SIZE) {
close(fd);
return;
}
temp_sensors = (struct icx3_temp_sensors*) &data;
float cur_temp;
short cur_data;
for (int i=0; i<ICX3_NUM_TEMP_SENSORS; i++) {
/* endian swap */
cur_data = (short)(temp_sensors->data[2*i+1] << 8) | (short)(temp_sensors->data[2*i]);
/* temp is reported in tenths of deg C */
cur_temp = (float)cur_data/10;
printf("%s: %.1f C\n",
icx3_temp_sensor_names[i],
cur_temp);
}
}
void get_available_fans(char *i2c_dev_path, char *fans_avail)
{
int product_id = check_for_icx3(i2c_dev_path);
/* From ICX3TotalFanCtrl.cs */
@@ -87,6 +145,7 @@ void print_icx3_fans(char *i2c_dev_path)
fans_avail[3] = 1;
break;
default:
/* default fans only */
fans_avail[0] = 1;
fans_avail[1] = 1;
break;
@@ -94,70 +153,103 @@ void print_icx3_fans(char *i2c_dev_path)
/* Not sure what this case is for */
break;
}
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)
return;
if (ioctl(fd, I2C_SLAVE, ICX3_I2C_ADDR) < 0) {
close(fd);
return;
}
for (int i=0; i < ICX3_MAX_FANS; i++) {
if (!fans_avail[i])
continue;
read_result = i2c_smbus_read_i2c_block_data(fd, ICX3_REG_FANCONTROL + i, ICX3_FANCONTROL_SIZE, data);
if (read_result != ICX3_FANCONTROL_SIZE) {
close(fd);
return;
}
fan_status = (struct icx3_fan_control*) &data;
printf("%s: %d RPM (%d/%d%%, %s)\n",
icx3_fan_names[i],
fan_status->rpm_status,
fan_status->duty_status,
fan_status->duty,
icx3_fan_mode_names[fan_status->fanmode]
);
}
}
void print_icx3_temps(char *i2c_dev_path)
int open_i2c_dev(char *i2c_dev_path)
{
char data[I2C_SMBUS_BLOCK_MAX] = {};
int fd, read_result;
struct icx3_temp_sensors *temp_sensors;
fd = open(i2c_dev_path, O_RDONLY);
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)
return;
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;
}
read_result = i2c_smbus_read_i2c_block_data(fd, ICX3_REG_TEMPSENSOR, ICX3_TEMPSENSOR_SIZE, data);
if (read_result != ICX3_TEMPSENSOR_SIZE) {
close(fd);
int fp = open_i2c_dev(i2c_dev_path);
if (fp == -1)
return;
}
temp_sensors = (struct icx3_temp_sensors*) &data;
float cur_temp;
short cur_data;
for (int i=0; i<ICX3_NUM_TEMP_SENSORS; i++) {
/* endian swap */
cur_data = (short)(temp_sensors->data[2*i+1] << 8) | (short)(temp_sensors->data[2*i]);
/* temp is reported in tenths of deg C */
cur_temp = (float)cur_data/10;
printf("%s %.1f C\n",
icx3_temp_sensor_names[i],
cur_temp);
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);
}