| 1 |
|
| 2 |
|
| 3 |
#include <acpi/acpigen.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <device/device.h> |
| 6 |
#include <device/mmio.h> |
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
#include "chip.h" |
| 10 |
#include "ec.h" |
| 11 |
#include "commands.h" |
| 12 |
|
| 13 |
static bool is_curve_valid(struct ec_clevo_it5570e_fan_curve curve) |
| 14 |
{ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
for (int i = 1; i < IT5570E_FAN_CURVE_LEN; i++) { |
| 22 |
if (curve.speed[i] < curve.speed[i - 1] || |
| 23 |
curve.temperature[i] <= curve.temperature[i - 1]) |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
static void write_fan_curve(struct ec_clevo_it5570e_fan_curve curve, int fan) |
| 31 |
{ |
| 32 |
uint16_t ramp; |
| 33 |
char fieldname[5]; |
| 34 |
|
| 35 |
|
| 36 |
for (int i = 0; i < IT5570E_FAN_CURVE_LEN; i++) { |
| 37 |
snprintf(fieldname, 5, "F%dT%d", fan + 1, i + 1); |
| 38 |
acpigen_write_store_int_to_namestr(curve.temperature[i], fieldname); |
| 39 |
snprintf(fieldname, 5, "F%dD%d", fan + 1, i + 1); |
| 40 |
acpigen_write_store_int_to_namestr(curve.speed[i] * 255 / 100, fieldname); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
for (int i = 0; i < (IT5570E_FAN_CURVE_LEN - 1); i++) { |
| 45 |
ramp = 255 * 16 * |
| 46 |
(curve.speed[i + 1] - curve.speed[i]) / |
| 47 |
(curve.temperature[i + 1] - curve.temperature[i]) / |
| 48 |
100; |
| 49 |
|
| 50 |
snprintf(fieldname, 5, "F%dR%d", fan + 1, i + 1); |
| 51 |
acpigen_write_store_int_to_namestr(ramp, fieldname); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static void write_fan_opregion(int fan_cnt) |
| 56 |
{ |
| 57 |
char fieldname[5]; |
| 58 |
uint8_t flags = FIELD_ANYACC | FIELD_LOCK | FIELD_PRESERVE; |
| 59 |
struct opregion opreg = { |
| 60 |
.name = "FNCV", |
| 61 |
.regionspace = SYSTEMMEMORY, |
| 62 |
.regionoffset = CONFIG_EC_CLEVO_IT5570E_MEM_BASE + 0x38c, |
| 63 |
.regionlen = fan_cnt * 14, |
| 64 |
}; |
| 65 |
|
| 66 |
acpigen_write_opregion(&opreg); |
| 67 |
acpigen_emit_ext_op(FIELD_OP); |
| 68 |
acpigen_write_len_f(); |
| 69 |
acpigen_emit_namestring(opreg.name); |
| 70 |
acpigen_emit_byte(flags); |
| 71 |
|
| 72 |
for (int fan = 1; fan <= fan_cnt; fan++) { |
| 73 |
|
| 74 |
for (int i = 1; i <= IT5570E_FAN_CURVE_LEN; i++) { |
| 75 |
snprintf(fieldname, 5, "F%dT%d", fan, i); |
| 76 |
acpigen_write_field_name(fieldname, 8); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
for (int i = 1; i <= IT5570E_FAN_CURVE_LEN; i++) { |
| 81 |
snprintf(fieldname, 5, "F%dD%d", fan, i); |
| 82 |
acpigen_write_field_name(fieldname, 8); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
for (int i = 1; i < IT5570E_FAN_CURVE_LEN; i++) { |
| 87 |
snprintf(fieldname, 5, "F%dR%d", fan, i); |
| 88 |
acpigen_write_field_name(fieldname, 16); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
acpigen_pop_len(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void ec_fan_curve_fill_ssdt(const struct device *dev) |
| 100 |
{ |
| 101 |
const ec_config_t *config = config_of(dev); |
| 102 |
const int fan_cnt = read8p(ECRAM + FANC); |
| 103 |
|
| 104 |
acpigen_write_scope(acpi_device_path(dev)); |
| 105 |
write_fan_opregion(fan_cnt); |
| 106 |
acpigen_write_method("SFCV", 0); |
| 107 |
|
| 108 |
if (config->fan_mode == FAN_MODE_CUSTOM) { |
| 109 |
int curve_cnt = 0; |
| 110 |
|
| 111 |
|
| 112 |
for (int i = 0; i < IT5570E_MAX_FAN_CNT; i++) |
| 113 |
if (*config->fan_curves[i].speed && *config->fan_curves[i].temperature) |
| 114 |
curve_cnt++; |
| 115 |
|
| 116 |
if (curve_cnt != fan_cnt) { |
| 117 |
printk(BIOS_WARNING, |
| 118 |
"EC: Fan curve count (%d) does not match fan count (%d). " |
| 119 |
"Check your devicetree!\n", curve_cnt, fan_cnt); |
| 120 |
goto pop; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
bool error = false; |
| 129 |
for (int i = 0; i < fan_cnt; i++) { |
| 130 |
if (!is_curve_valid(config->fan_curves[i])) { |
| 131 |
printk(BIOS_ERR, |
| 132 |
"EC: Fan %d curve invalid. Check your devicetree!\n", i); |
| 133 |
error = true; |
| 134 |
} |
| 135 |
} |
| 136 |
if (error) |
| 137 |
goto pop; |
| 138 |
|
| 139 |
acpigen_write_debug_string("EC: Apply custom fan curve"); |
| 140 |
|
| 141 |
for (int i = 0; i < fan_cnt; i++) |
| 142 |
write_fan_curve(config->fan_curves[i], i); |
| 143 |
|
| 144 |
|
| 145 |
acpigen_write_store_int_to_namestr(0x04, "FDAT"); |
| 146 |
acpigen_emit_namestring("SFCC"); |
| 147 |
acpigen_write_integer(0xd7); |
| 148 |
} |
| 149 |
|
| 150 |
pop: |
| 151 |
acpigen_pop_len(); |
| 152 |
acpigen_pop_len(); |
| 153 |
} |