blob: a0d746ac00c5879a640416543a2e221d20c8db7a [file] [log] [blame]
Michael Niewöhnere1e65cb2021-12-01 19:09:13 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <acpi/acpigen.h>
4#include <console/console.h>
5#include <device/device.h>
6#include <device/mmio.h>
7
8#include "chip.h"
9#include "ec.h"
10#include "commands.h"
11
12static bool is_curve_valid(struct ec_clevo_it5570e_fan_curve curve)
13{
14 /*
15 * Fan curve speeds have to be non-decreasing.
16 * Fan curve temperatures have to be increasing (to avoid division by 0).
17 * This also covers the case when the curve is all zeroes (i.e. not configured).
18 */
19
20 for (int i = 1; i < IT5570E_FAN_CURVE_LEN; i++) {
21 if (curve.speed[i] < curve.speed[i - 1] ||
22 curve.temperature[i] <= curve.temperature[i - 1])
23 return false;
24 }
25
26 return true;
27}
28
29static void write_fan_curve(struct ec_clevo_it5570e_fan_curve curve, int fan)
30{
31 uint16_t ramp;
32 char fieldname[5];
33
34 /* Curve points */
35 for (int i = 0; i < IT5570E_FAN_CURVE_LEN; i++) {
36 snprintf(fieldname, 5, "F%dT%d", fan + 1, i + 1);
37 acpigen_write_store_int_to_namestr(curve.temperature[i], fieldname);
38 snprintf(fieldname, 5, "F%dD%d", fan + 1, i + 1);
39 acpigen_write_store_int_to_namestr(curve.speed[i] * 255 / 100, fieldname);
40 }
41
42 /* Ramps */
43 for (int i = 0; i < (IT5570E_FAN_CURVE_LEN - 1); i++) {
44 ramp = 255 * 16 *
45 (curve.speed[i + 1] - curve.speed[i]) /
46 (curve.temperature[i + 1] - curve.temperature[i]) /
47 100;
48
49 snprintf(fieldname, 5, "F%dR%d", fan + 1, i + 1);
50 acpigen_write_store_int_to_namestr(ramp, fieldname);
51 }
52}
53
54static void write_fan_opregion(int fan_cnt)
55{
56 char fieldname[5];
57 uint8_t flags = FIELD_ANYACC | FIELD_LOCK | FIELD_PRESERVE;
58 struct opregion opreg = {
59 .name = "FNCV",
60 .regionspace = SYSTEMMEMORY,
61 .regionoffset = CONFIG_EC_CLEVO_IT5570E_MEM_BASE + 0x38c,
62 .regionlen = fan_cnt * 14,
63 };
64
65 acpigen_write_opregion(&opreg);
66 acpigen_emit_ext_op(FIELD_OP);
67 acpigen_write_len_f();
68 acpigen_emit_namestring(opreg.name);
69 acpigen_emit_byte(flags);
70
71 for (int fan = 1; fan <= fan_cnt; fan++) {
72 /* temps */
73 for (int i = 1; i <= IT5570E_FAN_CURVE_LEN; i++) {
74 snprintf(fieldname, 5, "F%dT%d", fan, i);
75 acpigen_write_field_name(fieldname, 8);
76 }
77
78 /* duties */
79 for (int i = 1; i <= IT5570E_FAN_CURVE_LEN; i++) {
80 snprintf(fieldname, 5, "F%dD%d", fan, i);
81 acpigen_write_field_name(fieldname, 8);
82 }
83
84 /* ramps */
85 for (int i = 1; i < IT5570E_FAN_CURVE_LEN; i++) {
86 snprintf(fieldname, 5, "F%dR%d", fan, i);
87 acpigen_write_field_name(fieldname, 16);
88 }
89 }
90
91 acpigen_pop_len(); /* Field */
92}
93
94/*
95 * Set Fan curve
96 * The function must exist even if the fan curve isn't enabled in devicetree.
97 */
98void ec_fan_curve_fill_ssdt(const struct device *dev)
99{
100 const ec_config_t *config = config_of(dev);
101 const int fan_cnt = read8p(ECRAM + FANC);
102
103 acpigen_write_scope(acpi_device_path(dev));
104 write_fan_opregion(fan_cnt);
105 acpigen_write_method("SFCV", 0);
106
107 if (config->fan_mode == FAN_MODE_CUSTOM) {
108 int curve_cnt = 0;
109
110 /* Check curve count against fan count from EC */
111 for (int i = 0; i < IT5570E_MAX_FAN_CNT; i++)
112 if (*config->fan_curves[i].speed && *config->fan_curves[i].temperature)
113 curve_cnt++;
114
115 if (curve_cnt != fan_cnt) {
116 printk(BIOS_WARNING,
117 "EC: Fan curve count (%d) does not match fan count (%d). "
118 "Check your devicetree!\n", curve_cnt, fan_cnt);
119 goto pop;
120 }
121
122 /*
123 * Check all curves.
124 * Custom mode can only be enabled for all fans or none. Thus, all
125 * custom curves must be valid before custom mode can be enabled.
126 */
127 bool error = false;
128 for (int i = 0; i < fan_cnt; i++) {
129 if (!is_curve_valid(config->fan_curves[i])) {
130 printk(BIOS_ERR,
131 "EC: Fan %d curve invalid. Check your devicetree!\n", i);
132 error = true;
133 }
134 }
135 if (error)
136 goto pop;
137
138 acpigen_write_debug_string("EC: Apply custom fan curve");
139
140 for (int i = 0; i < fan_cnt; i++)
141 write_fan_curve(config->fan_curves[i], i);
142
143 /* Enable custom fan mode */
144 acpigen_write_store_int_to_namestr(0x04, "FDAT");
145 acpigen_emit_namestring("SFCC");
146 acpigen_write_integer(0xd7);
147 }
148
149pop:
150 acpigen_pop_len(); /* Method */
151 acpigen_pop_len(); /* Scope */
152}