xref: /aosp_15_r20/external/coreboot/src/ec/kontron/it8516e/ec.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pnp.h>
6 #include <ec/acpi/ec.h>
7 #include <option.h>
8 
9 #include "ec.h"
10 #include "chip.h"
11 
12 typedef struct ec_kontron_it8516e_config config_t;
13 
14 enum { /* EC commands */
15 	IT8516E_CMD_SET_SYSTEMP_TYPE	= 0x06,
16 	IT8516E_CMD_GET_SYSTEMP_TYPE	= 0x07,
17 	IT8516E_CMD_GET_FAN_MODE	= 0x10,
18 	IT8516E_CMD_SET_FAN_MODE	= 0x11,
19 	IT8516E_CMD_GET_FAN_PWM		= 0x12,
20 	IT8516E_CMD_SET_FAN_PWM		= 0x13,
21 	IT8516E_CMD_GET_FAN_SPEED	= 0x14,
22 	IT8516E_CMD_SET_FAN_SPEED	= 0x15,
23 	IT8516E_CMD_GET_FAN_TEMP	= 0x16,
24 	IT8516E_CMD_SET_FAN_TEMP	= 0x17,
25 	IT8516E_CMD_SET_FAN_LIMITS	= 0x1a,
26 };
27 
28 /**
29  * Sets the type of the external temperature sensor used
30  *
31  * @param type Type of sensor to set
32  */
it8516e_set_systemp_type(const u8 type)33 static void it8516e_set_systemp_type(const u8 type)
34 {
35 	if (send_ec_command(IT8516E_CMD_SET_SYSTEMP_TYPE))
36 		return;
37 	send_ec_data(type);
38 }
39 
40 /**
41  * Sets the operating mode of a fan
42  *
43  * @param idx Selects the fan; 0: CPU, 1: System
44  * @param mode Mode to set
45  */
it8516e_set_fan_mode(const u8 idx,const u8 mode)46 static void it8516e_set_fan_mode(const u8 idx, const u8 mode)
47 {
48 	if (send_ec_command(IT8516E_CMD_SET_FAN_MODE))
49 		return;
50 	if (send_ec_data(idx))
51 		return;
52 	send_ec_data(mode);
53 }
54 
55 /**
56  * Sets the PWM rate of a fan in IT8516E_MODE_PWM
57  *
58  * @param idx Selects the fan; 0: CPU, 1: System
59  * @param pwm PWM rate measured in 255ths
60  */
it8516e_set_fan_pwm(const u8 idx,const u8 pwm)61 static void it8516e_set_fan_pwm(const u8 idx, const u8 pwm)
62 {
63 	if (send_ec_command(IT8516E_CMD_SET_FAN_PWM))
64 		return;
65 	if (send_ec_data(idx))
66 		return;
67 	send_ec_data(pwm);
68 }
69 
70 /**
71  * Sets the target speed in RPM for a fan in IT8516E_MODE_SPEED
72  *
73  * @param idx Selects the fan; 0: CPU, 1: System
74  * @param speed Speed in RPM
75  */
it8516e_set_fan_speed(const u8 idx,const u16 speed)76 static void it8516e_set_fan_speed(const u8 idx, const u16 speed)
77 {
78 	if (send_ec_command(IT8516E_CMD_SET_FAN_SPEED))
79 		return;
80 	if (send_ec_data(idx))
81 		return;
82 	if (send_ec_data(speed & 0xff))
83 		return;
84 	send_ec_data(speed >> 8);
85 }
86 
87 /**
88  * Sets the target temperature for a fan in IT8516E_MODE_THERMAL
89  *
90  * @param idx Selects the fan; 0: CPU, 1: System
91  * @param temp Temperature in 64ths degree C
92  */
it8516e_set_fan_temperature(const u8 idx,const u16 temp)93 static void it8516e_set_fan_temperature(const u8 idx, const u16 temp)
94 {
95 	if (send_ec_command(IT8516E_CMD_SET_FAN_TEMP))
96 		return;
97 	if (send_ec_data(idx))
98 		return;
99 	if (send_ec_data(temp & 0xff))
100 		return;
101 	send_ec_data(temp >> 8);
102 }
103 
104 /**
105  * Sets the minimum and maximum PWM rate of a fan in IT8516E_MODE_THERMAL
106  *
107  * @param idx Selects the fan; 0: CPU, 1: System
108  * @param min Minimum PWM rate in %
109  * @param max Maximum PWM rate in %
110  */
it8516e_set_fan_limits(const u8 idx,const u8 min,const u8 max)111 static void it8516e_set_fan_limits(const u8 idx, const u8 min, const u8 max)
112 {
113 	if (send_ec_command(IT8516E_CMD_SET_FAN_LIMITS))
114 		return;
115 	if (send_ec_data(idx))
116 		return;
117 	if (send_ec_data(min))
118 		return;
119 	send_ec_data(max);
120 }
121 
it8516e_set_fan_from_options(const config_t * const config,const u8 fan_idx)122 static void it8516e_set_fan_from_options(const config_t *const config,
123 					 const u8 fan_idx)
124 {
125 	static char fanX_mode[]		= "fanX_mode";
126 	static char fanX_target[]	= "fanX_target";
127 	static char fanX_min[]		= "fanX_min";
128 	static char fanX_max[]		= "fanX_max";
129 
130 	u8 fan_mode	= config->default_fan_mode[fan_idx];
131 	u16 fan_target	= config->default_fan_target[fan_idx];
132 	u8 fan_min	= config->default_fan_min[fan_idx];
133 	u8 fan_max	= config->default_fan_max[fan_idx];
134 
135 	fanX_mode[3] = '1' + fan_idx;
136 	fan_mode = get_uint_option(fanX_mode, fan_mode);
137 	if (!fan_mode)
138 		fan_mode = IT8516E_MODE_AUTO;
139 	it8516e_set_fan_mode(fan_idx, fan_mode);
140 
141 	fanX_target[3] = '1' + fan_idx;
142 	fan_target = get_uint_option(fanX_target, fan_target);
143 	switch (fan_mode) {
144 	case IT8516E_MODE_AUTO:
145 		printk(BIOS_DEBUG,
146 		       "Setting it8516e fan%d "
147 		       "control to auto.\n",
148 		       fan_idx + 1);
149 		break;
150 	case IT8516E_MODE_PWM:
151 		printk(BIOS_DEBUG,
152 		       "Setting it8516e fan%d "
153 		       "control to %d%% PWM.\n",
154 		       fan_idx + 1, fan_target);
155 		if (fan_target > 100)		/* Constrain to 100% */
156 			fan_target = 100;
157 		it8516e_set_fan_pwm(fan_idx, (fan_target * 255) / 100);
158 		break;
159 	case IT8516E_MODE_SPEED:
160 		printk(BIOS_DEBUG,
161 		       "Setting it8516e fan%d "
162 		       "control to %d RPMs.\n",
163 		       fan_idx + 1, fan_target);
164 		it8516e_set_fan_speed(fan_idx, fan_target);
165 		break;
166 	case IT8516E_MODE_THERMAL:
167 		printk(BIOS_DEBUG,
168 		       "Setting it8516e fan%d control to %d C.\n",
169 		       fan_idx + 1, fan_target);
170 		if (fan_target > 1024)		/* Constrain to 1K */
171 			fan_target = 1024;
172 		it8516e_set_fan_temperature(fan_idx, fan_target * 64);
173 
174 		fanX_min[3] = '1' + fan_idx;
175 		fanX_max[3] = '1' + fan_idx;
176 		fan_min = get_uint_option(fanX_min, fan_min);
177 		fan_max = get_uint_option(fanX_max, fan_max);
178 
179 		if (!fan_max || fan_max > 100)	/* Constrain fan_max to 100% */
180 			fan_max = 100;
181 		if (fan_min >= 100)		/* Constrain fan_min to  99% */
182 			fan_min = 99;
183 		if (fan_max <= fan_min)	/* If fan_min is the higher of the two,
184 					   it's safer for the hardware to keep
185 					   its value. Therefore, update fan_max. */
186 			fan_max = fan_min + 1;
187 
188 		printk(BIOS_DEBUG,
189 		       "Setting it8516e fan%d limits to %d%% - %d%% PWM.\n",
190 		       fan_idx + 1, fan_min, fan_max);
191 		it8516e_set_fan_limits(fan_idx, fan_min, fan_max);
192 		break;
193 	}
194 }
195 
it8516e_pm2_init(struct device * dev)196 static void it8516e_pm2_init(struct device *dev)
197 {
198 	const config_t *const config = dev->chip_info;
199 
200 	/* TODO: Set frequency / divider? */
201 
202 	ec_set_ports(find_resource(dev, PNP_IDX_IO1)->base,
203 		     find_resource(dev, PNP_IDX_IO0)->base);
204 
205 	u8 systemp_type = get_uint_option("systemp_type", config->default_systemp);
206 	if (systemp_type >= IT8516E_SYSTEMP_LASTPLUSONE)
207 		systemp_type = IT8516E_SYSTEMP_NONE;
208 	it8516e_set_systemp_type(systemp_type);
209 
210 	it8516e_set_fan_from_options(config, 0);
211 	it8516e_set_fan_from_options(config, 1);
212 }
213 
214 static struct device_operations it8516e_pm2_ops = {
215 	.read_resources   = pnp_read_resources,
216 	.set_resources    = pnp_set_resources,
217 	.enable_resources = pnp_enable_resources,
218 	.enable           = pnp_enable,
219 	.init             = it8516e_pm2_init
220 };
221 
222 static struct pnp_info it8516e_dev_infos[] = {
223 	{ NULL,             IT8516E_LDN_UART1, PNP_IO0 | PNP_IRQ0, 0x07f8, },
224 	{ NULL,             IT8516E_LDN_UART2, PNP_IO0 | PNP_IRQ0, 0x07f8, },
225 	{ NULL,             IT8516E_LDN_SWUC,  PNP_IO0 | PNP_IRQ0, 0xffe0, },
226 	{ NULL,             IT8516E_LDN_MOUSE, PNP_IRQ0, },
227 	{ NULL,             IT8516E_LDN_KBD,   PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
228 	{ NULL,             IT8516E_LDN_SMFI,  PNP_IO0 | PNP_IRQ0, 0xfff0, },
229 	{ NULL,             IT8516E_LDN_BRAM,  PNP_IO0 | PNP_IO1, 0xfffe, 0xfffe, },
230 	{ NULL,             IT8516E_LDN_PM1,   PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
231 	{ &it8516e_pm2_ops, IT8516E_LDN_PM2,   PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
232 	{ NULL,             IT8516E_LDN_PM3,   PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
233 };
234 
it8516e_enable(struct device * dev)235 static void it8516e_enable(struct device *dev)
236 {
237 	pnp_enable_devices(dev, &pnp_ops,
238 			   ARRAY_SIZE(it8516e_dev_infos), it8516e_dev_infos);
239 }
240 
241 const struct chip_operations ec_kontron_it8516e_ops = {
242 	.name = "Kontron (Fintec/ITE) IT8516E EC",
243 	.enable_dev = it8516e_enable
244 };
245