1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pnp.h>
6 #include <ec/acpi/ec.h>
12 typedef struct ec_kontron_it8516e_config config_t
;
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,
29 * Sets the type of the external temperature sensor used
31 * @param type Type of sensor to set
33 static void it8516e_set_systemp_type(const u8 type
)
35 if (send_ec_command(IT8516E_CMD_SET_SYSTEMP_TYPE
))
41 * Sets the operating mode of a fan
43 * @param idx Selects the fan; 0: CPU, 1: System
44 * @param mode Mode to set
46 static void it8516e_set_fan_mode(const u8 idx
, const u8 mode
)
48 if (send_ec_command(IT8516E_CMD_SET_FAN_MODE
))
50 if (send_ec_data(idx
))
56 * Sets the PWM rate of a fan in IT8516E_MODE_PWM
58 * @param idx Selects the fan; 0: CPU, 1: System
59 * @param pwm PWM rate measured in 255ths
61 static void it8516e_set_fan_pwm(const u8 idx
, const u8 pwm
)
63 if (send_ec_command(IT8516E_CMD_SET_FAN_PWM
))
65 if (send_ec_data(idx
))
71 * Sets the target speed in RPM for a fan in IT8516E_MODE_SPEED
73 * @param idx Selects the fan; 0: CPU, 1: System
74 * @param speed Speed in RPM
76 static void it8516e_set_fan_speed(const u8 idx
, const u16 speed
)
78 if (send_ec_command(IT8516E_CMD_SET_FAN_SPEED
))
80 if (send_ec_data(idx
))
82 if (send_ec_data(speed
& 0xff))
84 send_ec_data(speed
>> 8);
88 * Sets the target temperature for a fan in IT8516E_MODE_THERMAL
90 * @param idx Selects the fan; 0: CPU, 1: System
91 * @param temp Temperature in 64ths degree C
93 static void it8516e_set_fan_temperature(const u8 idx
, const u16 temp
)
95 if (send_ec_command(IT8516E_CMD_SET_FAN_TEMP
))
97 if (send_ec_data(idx
))
99 if (send_ec_data(temp
& 0xff))
101 send_ec_data(temp
>> 8);
105 * Sets the minimum and maximum PWM rate of a fan in IT8516E_MODE_THERMAL
107 * @param idx Selects the fan; 0: CPU, 1: System
108 * @param min Minimum PWM rate in %
109 * @param max Maximum PWM rate in %
111 static void it8516e_set_fan_limits(const u8 idx
, const u8 min
, const u8 max
)
113 if (send_ec_command(IT8516E_CMD_SET_FAN_LIMITS
))
115 if (send_ec_data(idx
))
117 if (send_ec_data(min
))
122 static void it8516e_set_fan_from_options(const config_t
*const config
,
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";
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
];
135 fanX_mode
[3] = '1' + fan_idx
;
136 fan_mode
= get_uint_option(fanX_mode
, fan_mode
);
138 fan_mode
= IT8516E_MODE_AUTO
;
139 it8516e_set_fan_mode(fan_idx
, fan_mode
);
141 fanX_target
[3] = '1' + fan_idx
;
142 fan_target
= get_uint_option(fanX_target
, fan_target
);
144 case IT8516E_MODE_AUTO
:
146 "Setting it8516e fan%d "
147 "control to auto.\n",
150 case IT8516E_MODE_PWM
:
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% */
157 it8516e_set_fan_pwm(fan_idx
, (fan_target
* 255) / 100);
159 case IT8516E_MODE_SPEED
:
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
);
166 case IT8516E_MODE_THERMAL
:
168 "Setting it8516e fan%d control to %d C.\n",
169 fan_idx
+ 1, fan_target
);
170 if (fan_target
> 1024) /* Constrain to 1K */
172 it8516e_set_fan_temperature(fan_idx
, fan_target
* 64);
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
);
179 if (!fan_max
|| fan_max
> 100) /* Constrain fan_max to 100% */
181 if (fan_min
>= 100) /* Constrain fan_min to 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;
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
);
196 static void it8516e_pm2_init(struct device
*dev
)
198 const config_t
*const config
= dev
->chip_info
;
200 /* TODO: Set frequency / divider? */
202 ec_set_ports(find_resource(dev
, PNP_IDX_IO1
)->base
,
203 find_resource(dev
, PNP_IDX_IO0
)->base
);
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
);
210 it8516e_set_fan_from_options(config
, 0);
211 it8516e_set_fan_from_options(config
, 1);
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
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, },
235 static void it8516e_enable(struct device
*dev
)
237 pnp_enable_devices(dev
, &pnp_ops
,
238 ARRAY_SIZE(it8516e_dev_infos
), it8516e_dev_infos
);
241 const struct chip_operations ec_kontron_it8516e_ops
= {
242 .name
= "Kontron (Fintec/ITE) IT8516E EC",
243 .enable_dev
= it8516e_enable