soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / ec / lenovo / h8 / h8.c
blob3df20c14f3b9cea90d8bec4cce18aa4416430a8d
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpi.h>
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/pnp.h>
7 #include <ec/acpi/ec.h>
8 #include <string.h>
9 #include <smbios.h>
10 #include <option.h>
11 #include <pc80/keyboard.h>
12 #include <types.h>
14 #include "h8.h"
15 #include "chip.h"
17 void h8_trackpoint_enable(int on)
19 ec_write(H8_TRACKPOINT_CTRL,
20 on ? H8_TRACKPOINT_ON : H8_TRACKPOINT_OFF);
23 /* Controls radio-off pin in WLAN MiniPCIe slot. */
24 void h8_wlan_enable(int on)
26 if (on)
27 ec_set_bit(0x3a, 5);
28 else
29 ec_clr_bit(0x3a, 5);
32 /* Controls radio-off pin in UWB MiniPCIe slot. */
33 static void h8_uwb_enable(int on)
35 if (on)
36 ec_set_bit(0x31, 2);
37 else
38 ec_clr_bit(0x31, 2);
41 static void h8_fn_ctrl_swap(int on)
43 if (on)
44 ec_set_bit(0xce, 4);
45 else
46 ec_clr_bit(0xce, 4);
49 enum battery {
50 SECONDARY_BATTERY = 0,
51 PRIMARY_BATTERY = 1,
54 /* h8 charge priority. Defines if primary or secondary
55 * battery is charged first.
56 * Because NVRAM is complete the otherway around as this register,
57 * it's inverted by if
59 static void h8_charge_priority(enum battery battery)
61 if (battery == PRIMARY_BATTERY)
62 ec_clr_bit(0x0, 4);
63 else
64 ec_set_bit(0x0, 4);
67 static void h8_sticky_fn(int on)
69 if (on)
70 ec_set_bit(0x0, 3);
71 else
72 ec_clr_bit(0x0, 3);
75 static void f1_to_f12_as_primary(int on)
77 if (on)
78 ec_set_bit(0x3b, 3);
79 else
80 ec_clr_bit(0x3b, 3);
83 static void h8_log_ec_version(void)
85 char ecfw[17];
86 u8 len;
87 u16 fwvh, fwvl;
89 len = h8_build_id_and_function_spec_version(ecfw, sizeof ecfw - 1);
90 ecfw[len] = 0;
92 fwvh = ec_read(0xe9);
93 fwvl = ec_read(0xe8);
95 printk(BIOS_INFO, "H8: EC Firmware ID %s, Version %d.%d%d%c\n", ecfw,
96 fwvh >> 4, fwvh & 0x0f, fwvl >> 4, 0x41 + (fwvl & 0xf));
99 void h8_set_audio_mute(int mute)
101 if (mute)
102 ec_set_bit(0x3a, 0);
103 else
104 ec_clr_bit(0x3a, 0);
107 void h8_enable_event(int event)
109 if (event < 0 || event > 127)
110 return;
112 ec_set_bit(0x10 + (event >> 3), event & 7);
115 void h8_disable_event(int event)
117 if (event < 0 || event > 127)
118 return;
120 ec_clr_bit(0x10 + (event >> 3), event & 7);
123 void h8_usb_always_on_enable(enum usb_always_on on)
125 u8 val;
127 switch (on) {
128 case UAO_OFF:
129 val = ec_read(H8_USB_ALWAYS_ON);
130 // Clear bits 0,2,3
131 val &= ~(H8_USB_ALWAYS_ON_ENABLE | H8_USB_ALWAYS_ON_AC_ONLY);
132 ec_write(H8_USB_ALWAYS_ON, val);
133 break;
135 case UAO_AC_AND_BATTERY:
136 val = ec_read(H8_USB_ALWAYS_ON);
137 val |= H8_USB_ALWAYS_ON_ENABLE; // Set bit 0
138 val &= ~H8_USB_ALWAYS_ON_AC_ONLY; // Clear bits 2 and 3
139 ec_write(H8_USB_ALWAYS_ON, val);
140 break;
142 case UAO_AC_ONLY:
143 val = ec_read(H8_USB_ALWAYS_ON);
144 // Set bits 0,2,3
145 val |= (H8_USB_ALWAYS_ON_ENABLE | H8_USB_ALWAYS_ON_AC_ONLY);
146 ec_write(H8_USB_ALWAYS_ON, val);
147 break;
151 void h8_usb_power_enable(int onoff)
153 if (onoff)
154 ec_set_bit(0x3b, 4);
155 else
156 ec_clr_bit(0x3b, 4);
159 int h8_ultrabay_device_present(void)
161 return ec_read(H8_STATUS1) & 0x5 ? 0 : 1;
164 u8 h8_build_id_and_function_spec_version(char *buf, u8 buf_len)
166 u8 i, c;
167 char str[16 + 1]; /* 16 ASCII chars + \0 */
169 /* Build ID */
170 for (i = 0; i < 8; i++) {
171 c = ec_read(0xf0 + i);
172 if (c < 0x20 || c > 0x7f) {
173 i = snprintf(str, sizeof(str), "*INVALID");
174 break;
176 str[i] = c;
179 /* EC firmware function specification version */
180 i += snprintf(str + i, sizeof(str) - i, "-%u.%u", ec_read(0xef), ec_read(0xeb));
182 i = MIN(buf_len, i);
183 memcpy(buf, str, i);
185 return i;
188 #if CONFIG(GENERATE_SMBIOS_TABLES)
189 static void h8_smbios_strings(struct device *dev, struct smbios_type11 *t)
191 char tpec[] = "IBM ThinkPad Embedded Controller -[ ]-";
193 h8_build_id_and_function_spec_version(tpec + 35, 17);
195 t->count = smbios_add_string(t->eos, tpec);
197 #endif
199 static void h8_init(struct device *dev)
201 pc_keyboard_init(NO_AUX_DEVICE);
204 #if CONFIG(HAVE_ACPI_TABLES)
205 static const char *h8_acpi_name(const struct device *dev)
207 return "EC";
209 #endif
211 struct device_operations h8_dev_ops = {
212 #if CONFIG(GENERATE_SMBIOS_TABLES)
213 .get_smbios_strings = h8_smbios_strings,
214 #endif
215 #if CONFIG(HAVE_ACPI_TABLES)
216 .acpi_fill_ssdt = h8_ssdt_generator,
217 .acpi_name = h8_acpi_name,
218 #endif
219 .init = h8_init,
222 void __weak h8_mb_init(void){ /* NOOP */ }
224 static void h8_enable(struct device *dev)
226 struct ec_lenovo_h8_config *conf = dev->chip_info;
227 u8 val;
228 u8 beepmask0, beepmask1, reg8;
230 dev->ops = &h8_dev_ops;
232 ec_clear_out_queue();
233 h8_log_ec_version();
235 /* Always enable I/O range 0x1600-0x160f and thermal management */
236 reg8 = conf->config0;
237 reg8 |= H8_CONFIG0_SMM_H8_ENABLE;
238 reg8 |= H8_CONFIG0_TC_ENABLE;
239 ec_write(H8_CONFIG0, reg8);
241 reg8 = conf->config1;
242 if (conf->has_keyboard_backlight) {
243 /* Default to both backlights */
244 reg8 = (reg8 & 0xf3) | ((get_uint_option("backlight", 0) & 0x3) << 2);
246 ec_write(H8_CONFIG1, reg8);
247 ec_write(H8_CONFIG2, conf->config2);
248 ec_write(H8_CONFIG3, conf->config3);
250 beepmask0 = conf->beepmask0;
251 beepmask1 = conf->beepmask1;
253 if (conf->has_power_management_beeps) {
254 if (get_uint_option("power_management_beeps", 1) == 0) {
255 beepmask0 = 0x00;
256 beepmask1 = 0x00;
260 if (conf->has_power_management_beeps) {
261 if (get_uint_option("low_battery_beep", 1))
262 beepmask0 |= 2;
263 else
264 beepmask0 &= ~2;
267 ec_write(H8_SOUND_ENABLE0, beepmask0);
268 ec_write(H8_SOUND_ENABLE1, beepmask1);
270 /* silence sounds in queue */
271 ec_write(H8_SOUND_REPEAT, 0x00);
272 ec_write(H8_SOUND_REG, 0x00);
274 ec_write(0x10, conf->event0_enable);
275 ec_write(0x11, conf->event1_enable);
276 ec_write(0x12, conf->event2_enable);
277 ec_write(0x13, conf->event3_enable);
278 ec_write(0x14, conf->event4_enable);
279 ec_write(0x15, conf->event5_enable);
280 ec_write(0x16, conf->event6_enable);
281 ec_write(0x17, conf->event7_enable);
282 ec_write(0x18, conf->event8_enable);
283 ec_write(0x19, conf->event9_enable);
284 ec_write(0x1a, conf->eventa_enable);
285 ec_write(0x1b, conf->eventb_enable);
286 ec_write(0x1c, conf->eventc_enable);
287 ec_write(0x1d, conf->eventd_enable);
288 ec_write(0x1e, conf->evente_enable);
289 ec_write(0x1f, conf->eventf_enable);
291 ec_write(H8_FAN_CONTROL, H8_FAN_CONTROL_AUTO);
293 h8_usb_always_on_enable(get_uint_option("usb_always_on", 0));
295 h8_wlan_enable(get_uint_option("wlan", 1));
297 h8_trackpoint_enable(1);
298 h8_usb_power_enable(1);
300 unsigned int volume = get_uint_option("volume", ~0);
301 if (volume <= 0xff && !acpi_is_wakeup_s3())
302 ec_write(H8_VOLUME_CONTROL, volume);
304 val = (CONFIG(H8_SUPPORT_BT_ON_WIFI) || h8_has_bdc(dev)) &&
305 h8_bluetooth_nv_enable();
306 h8_bluetooth_enable(val);
308 val = h8_has_wwan(dev) && h8_wwan_nv_enable();
309 h8_wwan_enable(val);
311 if (conf->has_uwb)
312 h8_uwb_enable(get_uint_option("uwb", 1));
314 h8_fn_ctrl_swap(get_uint_option("fn_ctrl_swap", CONFIG(H8_FN_CTRL_SWAP)));
316 h8_sticky_fn(get_uint_option("sticky_fn", 0));
318 if (CONFIG(H8_HAS_PRIMARY_FN_KEYS))
319 f1_to_f12_as_primary(get_uint_option("f1_to_f12_as_primary", 1));
321 h8_charge_priority(get_uint_option("first_battery", PRIMARY_BATTERY));
323 h8_set_audio_mute(0);
324 h8_mb_init();
327 struct chip_operations ec_lenovo_h8_ops = {
328 CHIP_NAME("Lenovo H8 EC")
329 .enable_dev = h8_enable,