1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/pnp.h>
7 #include <ec/acpi/ec.h>
12 #include <pc80/keyboard.h>
18 void h8_trackpoint_enable(int on
)
20 ec_write(H8_TRACKPOINT_CTRL
,
21 on
? H8_TRACKPOINT_ON
: H8_TRACKPOINT_OFF
);
24 /* Controls radio-off pin in WLAN MiniPCIe slot. */
25 void h8_wlan_enable(int on
)
33 /* Controls radio-off pin in UWB MiniPCIe slot. */
34 static void h8_uwb_enable(int on
)
42 static void h8_fn_ctrl_swap(int on
)
51 SECONDARY_BATTERY
= 0,
55 /* h8 charge priority. Defines if primary or secondary
56 * battery is charged first.
57 * Because NVRAM is complete the other way around as this register,
60 static void h8_charge_priority(enum battery battery
)
62 if (battery
== PRIMARY_BATTERY
)
68 static void h8_sticky_fn(int on
)
76 static void f1_to_f12_as_primary(int on
)
84 static void h8_log_ec_version(void)
90 len
= h8_build_id_and_function_spec_version(ecfw
, sizeof(ecfw
) - 1);
96 printk(BIOS_INFO
, "H8: EC Firmware ID %s, Version %d.%d%d%c\n", ecfw
,
97 fwvh
>> 4, fwvh
& 0x0f, fwvl
>> 4, 0x41 + (fwvl
& 0xf));
100 void h8_set_audio_mute(int mute
)
108 void h8_enable_event(int event
)
110 if (event
< 0 || event
> 127)
113 ec_set_bit(0x10 + (event
>> 3), event
& 7);
116 void h8_disable_event(int event
)
118 if (event
< 0 || event
> 127)
121 ec_clr_bit(0x10 + (event
>> 3), event
& 7);
124 void h8_usb_always_on_enable(enum usb_always_on on
)
130 val
= ec_read(H8_USB_ALWAYS_ON
);
132 val
&= ~(H8_USB_ALWAYS_ON_ENABLE
| H8_USB_ALWAYS_ON_AC_ONLY
);
133 ec_write(H8_USB_ALWAYS_ON
, val
);
136 case UAO_AC_AND_BATTERY
:
137 val
= ec_read(H8_USB_ALWAYS_ON
);
138 val
|= H8_USB_ALWAYS_ON_ENABLE
; // Set bit 0
139 val
&= ~H8_USB_ALWAYS_ON_AC_ONLY
; // Clear bits 2 and 3
140 ec_write(H8_USB_ALWAYS_ON
, val
);
144 val
= ec_read(H8_USB_ALWAYS_ON
);
146 val
|= (H8_USB_ALWAYS_ON_ENABLE
| H8_USB_ALWAYS_ON_AC_ONLY
);
147 ec_write(H8_USB_ALWAYS_ON
, val
);
152 void h8_usb_power_enable(int onoff
)
160 int h8_ultrabay_device_present(void)
162 return ec_read(H8_STATUS1
) & 0x5 ? 0 : 1;
165 u8
h8_build_id_and_function_spec_version(char *buf
, u8 buf_len
)
168 char str
[16 + 1]; /* 16 ASCII chars + \0 */
171 for (i
= 0; i
< 8; i
++) {
172 c
= ec_read(0xf0 + i
);
173 if (c
< 0x20 || c
> 0x7f) {
174 i
= snprintf(str
, sizeof(str
), "*INVALID");
180 /* EC firmware function specification version */
181 i
+= snprintf(str
+ i
, sizeof(str
) - i
, "-%u.%u", ec_read(0xef), ec_read(0xeb));
189 #if CONFIG(GENERATE_SMBIOS_TABLES)
190 static void h8_smbios_strings(struct device
*dev
, struct smbios_type11
*t
)
192 char tpec
[] = "IBM ThinkPad Embedded Controller -[ ]-";
194 h8_build_id_and_function_spec_version(tpec
+ 35, 17);
196 t
->count
= smbios_add_string(t
->eos
, tpec
);
200 static void h8_init(struct device
*dev
)
202 pc_keyboard_init(NO_AUX_DEVICE
);
205 #if CONFIG(HAVE_ACPI_TABLES)
206 static const char *h8_acpi_name(const struct device
*dev
)
212 struct device_operations h8_dev_ops
= {
213 #if CONFIG(GENERATE_SMBIOS_TABLES)
214 .get_smbios_strings
= h8_smbios_strings
,
216 #if CONFIG(HAVE_ACPI_TABLES)
217 .acpi_fill_ssdt
= h8_ssdt_generator
,
218 .acpi_name
= h8_acpi_name
,
223 void __weak
h8_mb_init(void){ /* NOOP */ }
225 static void h8_enable(struct device
*dev
)
227 struct ec_lenovo_h8_config
*conf
= dev
->chip_info
;
229 u8 beepmask0
, beepmask1
, reg8
;
231 dev
->ops
= &h8_dev_ops
;
233 ec_clear_out_queue();
236 /* Always enable I/O range 0x1600-0x160f and thermal management */
237 reg8
= conf
->config0
;
238 reg8
|= H8_CONFIG0_SMM_H8_ENABLE
;
239 reg8
|= H8_CONFIG0_TC_ENABLE
;
240 ec_write(H8_CONFIG0
, reg8
);
242 reg8
= conf
->config1
;
243 if (conf
->has_keyboard_backlight
) {
244 /* Default to both backlights */
245 reg8
= (reg8
& 0xf3) | ((get_uint_option("backlight", 0) & 0x3) << 2);
247 ec_write(H8_CONFIG1
, reg8
);
248 ec_write(H8_CONFIG2
, conf
->config2
);
249 ec_write(H8_CONFIG3
, conf
->config3
);
251 beepmask0
= conf
->beepmask0
;
252 beepmask1
= conf
->beepmask1
;
254 if (conf
->has_power_management_beeps
) {
255 if (get_uint_option("power_management_beeps", 1) == 0) {
261 if (conf
->has_power_management_beeps
) {
262 if (get_uint_option("low_battery_beep", 1))
268 ec_write(H8_SOUND_ENABLE0
, beepmask0
);
269 ec_write(H8_SOUND_ENABLE1
, beepmask1
);
271 /* silence sounds in queue */
272 ec_write(H8_SOUND_REPEAT
, 0x00);
273 ec_write(H8_SOUND_REG
, 0x00);
275 ec_write(0x10, conf
->event0_enable
);
276 ec_write(0x11, conf
->event1_enable
);
277 ec_write(0x12, conf
->event2_enable
);
278 ec_write(0x13, conf
->event3_enable
);
279 ec_write(0x14, conf
->event4_enable
);
280 ec_write(0x15, conf
->event5_enable
);
281 ec_write(0x16, conf
->event6_enable
);
282 ec_write(0x17, conf
->event7_enable
);
283 ec_write(0x18, conf
->event8_enable
);
284 ec_write(0x19, conf
->event9_enable
);
285 ec_write(0x1a, conf
->eventa_enable
);
286 ec_write(0x1b, conf
->eventb_enable
);
287 ec_write(0x1c, conf
->eventc_enable
);
288 ec_write(0x1d, conf
->eventd_enable
);
289 ec_write(0x1e, conf
->evente_enable
);
290 ec_write(0x1f, conf
->eventf_enable
);
292 ec_write(H8_FAN_CONTROL
, H8_FAN_CONTROL_AUTO
);
294 h8_usb_always_on_enable(get_uint_option("usb_always_on", 0));
296 h8_wlan_enable(get_uint_option("wlan", 1));
298 h8_trackpoint_enable(1);
299 h8_usb_power_enable(1);
301 unsigned int volume
= get_uint_option("volume", ~0);
302 if (volume
<= 0xff && !acpi_is_wakeup_s3())
303 ec_write(H8_VOLUME_CONTROL
, volume
);
305 val
= (CONFIG(H8_SUPPORT_BT_ON_WIFI
) || h8_has_bdc(dev
)) &&
306 h8_bluetooth_nv_enable();
307 h8_bluetooth_enable(val
);
309 val
= h8_has_wwan(dev
) && h8_wwan_nv_enable();
313 h8_uwb_enable(get_uint_option("uwb", 1));
315 h8_fn_ctrl_swap(get_uint_option("fn_ctrl_swap", CONFIG(H8_FN_CTRL_SWAP
)));
317 h8_sticky_fn(get_uint_option("sticky_fn", 0));
319 if (CONFIG(H8_HAS_PRIMARY_FN_KEYS
))
320 f1_to_f12_as_primary(get_uint_option("f1_to_f12_as_primary", 1));
322 h8_charge_priority(get_uint_option("first_battery", PRIMARY_BATTERY
));
324 h8_set_audio_mute(0);
328 struct chip_operations ec_lenovo_h8_ops
= {
329 .name
= "Lenovo H8 EC",
330 .enable_dev
= h8_enable
,