blob: 1d6780885fbec4ddc57bb0bed33bb62f68a3f6fa [file] [log] [blame]
Angel Pons210a0082020-04-02 23:48:24 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Nico Hubera53266b2013-05-02 15:26:08 +02002
Nico Hubera53266b2013-05-02 15:26:08 +02003#include <console/console.h>
4#include <device/device.h>
5#include <device/pnp.h>
6#include <ec/acpi/ec.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +02007#include <option.h>
Nico Hubera53266b2013-05-02 15:26:08 +02008
9#include "ec.h"
10#include "chip.h"
11
12typedef struct ec_kontron_it8516e_config config_t;
13
14enum { /* EC commands */
Nico Huber6d6a2ac2013-07-12 14:35:00 +020015 IT8516E_CMD_SET_SYSTEMP_TYPE = 0x06,
16 IT8516E_CMD_GET_SYSTEMP_TYPE = 0x07,
Nico Hubera53266b2013-05-02 15:26:08 +020017 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,
Nico Huber9ce71b32013-07-12 14:43:11 +020025 IT8516E_CMD_SET_FAN_LIMITS = 0x1a,
Nico Hubera53266b2013-05-02 15:26:08 +020026};
27
Nico Huber1f9f6782013-07-18 11:50:59 +020028/**
29 * Sets the type of the external temperature sensor used
30 *
31 * @param type Type of sensor to set
32 */
Nico Huber6d6a2ac2013-07-12 14:35:00 +020033static 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
Nico Huber1f9f6782013-07-18 11:50:59 +020040/**
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 */
Nico Hubera53266b2013-05-02 15:26:08 +020046static 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
Nico Huber1f9f6782013-07-18 11:50:59 +020055/**
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 */
Nico Hubera53266b2013-05-02 15:26:08 +020061static 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
Nico Huber1f9f6782013-07-18 11:50:59 +020070/**
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 */
Nico Hubera53266b2013-05-02 15:26:08 +020076static 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
Nico Huber1f9f6782013-07-18 11:50:59 +020087/**
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 */
Nico Huber260c33b2013-07-18 11:27:30 +020093static void it8516e_set_fan_temperature(const u8 idx, const u16 temp)
Nico Hubera53266b2013-05-02 15:26:08 +020094{
95 if (send_ec_command(IT8516E_CMD_SET_FAN_TEMP))
96 return;
97 if (send_ec_data(idx))
98 return;
Nico Huber260c33b2013-07-18 11:27:30 +020099 if (send_ec_data(temp & 0xff))
Nico Hubera53266b2013-05-02 15:26:08 +0200100 return;
Nico Huber260c33b2013-07-18 11:27:30 +0200101 send_ec_data(temp >> 8);
Nico Hubera53266b2013-05-02 15:26:08 +0200102}
103
Nico Huber1f9f6782013-07-18 11:50:59 +0200104/**
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 */
Nico Huber9ce71b32013-07-12 14:43:11 +0200111static 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
Nico Hubera53266b2013-05-02 15:26:08 +0200122static 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";
Nico Huber9ce71b32013-07-12 14:43:11 +0200127 static char fanX_min[] = "fanX_min";
128 static char fanX_max[] = "fanX_max";
Nico Hubera53266b2013-05-02 15:26:08 +0200129
Nico Huber9ce71b32013-07-12 14:43:11 +0200130 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];
Nico Hubera53266b2013-05-02 15:26:08 +0200134
135 fanX_mode[3] = '1' + fan_idx;
Angel Pons88dcb312021-04-26 17:10:28 +0200136 fan_mode = get_uint_option(fanX_mode, fan_mode);
Nico Hubera53266b2013-05-02 15:26:08 +0200137 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;
Angel Pons88dcb312021-04-26 17:10:28 +0200142 fan_target = get_uint_option(fanX_target, fan_target);
Nico Hubera53266b2013-05-02 15:26:08 +0200143 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);
Nico Huberca4f0732013-07-18 12:27:00 +0200155 if (fan_target > 100) /* Constrain to 100% */
156 fan_target = 100;
Nico Huber942b6c22013-07-12 14:40:23 +0200157 it8516e_set_fan_pwm(fan_idx, (fan_target * 255) / 100);
Nico Hubera53266b2013-05-02 15:26:08 +0200158 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,
Nico Huber9ce71b32013-07-12 14:43:11 +0200168 "Setting it8516e fan%d control to %d C.\n",
Nico Hubera53266b2013-05-02 15:26:08 +0200169 fan_idx + 1, fan_target);
Nico Huberca4f0732013-07-18 12:27:00 +0200170 if (fan_target > 1024) /* Constrain to 1K */
171 fan_target = 1024;
Nico Huber260c33b2013-07-18 11:27:30 +0200172 it8516e_set_fan_temperature(fan_idx, fan_target * 64);
Nico Huber9ce71b32013-07-12 14:43:11 +0200173
174 fanX_min[3] = '1' + fan_idx;
175 fanX_max[3] = '1' + fan_idx;
Angel Pons88dcb312021-04-26 17:10:28 +0200176 fan_min = get_uint_option(fanX_min, fan_min);
177 fan_max = get_uint_option(fanX_max, fan_max);
Nico Huber9ce71b32013-07-12 14:43:11 +0200178
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);
Nico Hubera53266b2013-05-02 15:26:08 +0200192 break;
193 }
194}
195
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100196static void it8516e_pm2_init(struct device *dev)
Nico Hubera53266b2013-05-02 15:26:08 +0200197{
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
Angel Pons88dcb312021-04-26 17:10:28 +0200205 u8 systemp_type = get_uint_option("systemp_type", config->default_systemp);
Nico Huber6d6a2ac2013-07-12 14:35:00 +0200206 if (systemp_type >= IT8516E_SYSTEMP_LASTPLUSONE)
207 systemp_type = IT8516E_SYSTEMP_NONE;
208 it8516e_set_systemp_type(systemp_type);
209
Nico Hubera53266b2013-05-02 15:26:08 +0200210 it8516e_set_fan_from_options(config, 0);
211 it8516e_set_fan_from_options(config, 1);
212}
213
214static struct device_operations it8516e_pm2_ops = {
Nico Huber9cb09412013-06-15 15:30:19 +0200215 .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
Nico Hubera53266b2013-05-02 15:26:08 +0200220};
221
222static struct pnp_info it8516e_dev_infos[] = {
Samuel Holland7daac912017-06-06 22:55:01 -0500223 { 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, },
Nico Hubera53266b2013-05-02 15:26:08 +0200226 { NULL, IT8516E_LDN_MOUSE, PNP_IRQ0, },
Samuel Holland7daac912017-06-06 22:55:01 -0500227 { 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, },
Nico Hubera53266b2013-05-02 15:26:08 +0200233};
234
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100235static void it8516e_enable(struct device *dev)
Nico Hubera53266b2013-05-02 15:26:08 +0200236{
237 pnp_enable_devices(dev, &pnp_ops,
238 ARRAY_SIZE(it8516e_dev_infos), it8516e_dev_infos);
239}
240
241const struct chip_operations ec_kontron_it8516e_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900242 .name = "Kontron (Fintec/ITE) IT8516E EC",
Nico Hubera53266b2013-05-02 15:26:08 +0200243 .enable_dev = it8516e_enable
244};