blob: e17f0fb2d413f6ba6346c9716b659c1c0d9b7b37 [file] [log] [blame]
Angel Pons210a0082020-04-02 23:48:24 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Sven Schnelleffcd1432011-04-11 19:43:32 +00002
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi.h>
Sven Schnelleffcd1432011-04-11 19:43:32 +00004#include <console/console.h>
5#include <device/device.h>
Sven Schnelleffcd1432011-04-11 19:43:32 +00006#include <device/pnp.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10007#include <ec/acpi/ec.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10008#include <string.h>
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +02009#include <smbios.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020010#include <option.h>
Vladimir Serbinenko852014c2015-05-27 08:30:47 +020011#include <pc80/keyboard.h>
Elyes HAOUAS5fd93e02019-05-15 21:07:30 +020012#include <types.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100013
Sven Schnelleffcd1432011-04-11 19:43:32 +000014#include "h8.h"
15#include "chip.h"
Vladimir Serbinenkoe2b67952013-11-14 19:09:42 +010016
Sven Schnelleffcd1432011-04-11 19:43:32 +000017void h8_trackpoint_enable(int on)
18{
Peter Stuged0b04002013-07-06 20:20:45 +020019 ec_write(H8_TRACKPOINT_CTRL,
20 on ? H8_TRACKPOINT_ON : H8_TRACKPOINT_OFF);
Sven Schnelleffcd1432011-04-11 19:43:32 +000021
22}
23
Vladimir Serbinenkof422a442013-11-13 21:53:23 +010024/* Controls radio-off pin in WLAN MiniPCIe slot. */
Sven Schnelleffcd1432011-04-11 19:43:32 +000025void h8_wlan_enable(int on)
26{
27 if (on)
28 ec_set_bit(0x3a, 5);
29 else
30 ec_clr_bit(0x3a, 5);
31}
32
Vladimir Serbinenko883e7ac2014-08-13 01:22:13 +020033/* Controls radio-off pin in UWB MiniPCIe slot. */
34static void h8_uwb_enable(int on)
35{
36 if (on)
37 ec_set_bit(0x31, 2);
38 else
39 ec_clr_bit(0x31, 2);
40}
41
Vladimir Serbinenkofc7090b2014-01-11 10:29:14 +010042static void h8_fn_ctrl_swap(int on)
43{
44 if (on)
45 ec_set_bit(0xce, 4);
46 else
47 ec_clr_bit(0xce, 4);
48}
49
Alexander Couzensc5a6fb82016-10-16 06:55:19 +020050enum battery {
51 SECONDARY_BATTERY = 0,
52 PRIMARY_BATTERY = 1,
53};
54
55/* h8 charge priority. Defines if primary or secondary
56 * battery is charged first.
57 * Because NVRAM is complete the otherway around as this register,
58 * it's inverted by if
59 */
60static void h8_charge_priority(enum battery battery)
61{
62 if (battery == PRIMARY_BATTERY)
63 ec_clr_bit(0x0, 4);
64 else
65 ec_set_bit(0x0, 4);
66}
67
Vladimir Serbinenko11a7c842014-01-11 10:55:31 +010068static void h8_sticky_fn(int on)
69{
70 if (on)
71 ec_set_bit(0x0, 3);
72 else
73 ec_clr_bit(0x0, 3);
74}
75
Iru Cai7e8eb6b2019-07-31 21:01:52 +080076static void f1_to_f12_as_primary(int on)
77{
78 if (on)
79 ec_set_bit(0x3b, 3);
80 else
81 ec_clr_bit(0x3b, 3);
82}
83
Sven Schnelleffcd1432011-04-11 19:43:32 +000084static void h8_log_ec_version(void)
85{
Peter Stuge77a5abe2013-07-06 19:44:47 +020086 char ecfw[17];
87 u8 len;
Sven Schnelleffcd1432011-04-11 19:43:32 +000088 u16 fwvh, fwvl;
Sven Schnelleffcd1432011-04-11 19:43:32 +000089
Peter Stuge77a5abe2013-07-06 19:44:47 +020090 len = h8_build_id_and_function_spec_version(ecfw, sizeof ecfw - 1);
91 ecfw[len] = 0;
Sven Schnelleffcd1432011-04-11 19:43:32 +000092
93 fwvh = ec_read(0xe9);
94 fwvl = ec_read(0xe8);
95
Paul Menzel58ecefb2020-01-09 22:35:50 +010096 printk(BIOS_INFO, "H8: EC Firmware ID %s, Version %d.%d%d%c\n", ecfw,
Sven Schnelleffcd1432011-04-11 19:43:32 +000097 fwvh >> 4, fwvh & 0x0f, fwvl >> 4, 0x41 + (fwvl & 0xf));
98}
99
Sven Schnelled0ea6782011-10-25 15:29:47 +0200100void h8_set_audio_mute(int mute)
Sven Schnelleffcd1432011-04-11 19:43:32 +0000101{
Sven Schnelled0ea6782011-10-25 15:29:47 +0200102 if (mute)
103 ec_set_bit(0x3a, 0);
Sven Schnelleffcd1432011-04-11 19:43:32 +0000104 else
Sven Schnelled0ea6782011-10-25 15:29:47 +0200105 ec_clr_bit(0x3a, 0);
Sven Schnelleffcd1432011-04-11 19:43:32 +0000106}
107
108void h8_enable_event(int event)
109{
110 if (event < 0 || event > 127)
111 return;
112
113 ec_set_bit(0x10 + (event >> 3), event & 7);
114}
115
116void h8_disable_event(int event)
117{
118 if (event < 0 || event > 127)
119 return;
120
121 ec_clr_bit(0x10 + (event >> 3), event & 7);
122
123}
124
Nathaniel Roach4f4322d2018-11-10 08:34:44 +0800125void h8_usb_always_on_enable(enum usb_always_on on)
126{
127 u8 val;
128
129 switch (on) {
130 case UAO_OFF:
131 val = ec_read(H8_USB_ALWAYS_ON);
132 // Clear bits 0,2,3
133 val &= ~(H8_USB_ALWAYS_ON_ENABLE | H8_USB_ALWAYS_ON_AC_ONLY);
134 ec_write(H8_USB_ALWAYS_ON, val);
135 break;
136
137 case UAO_AC_AND_BATTERY:
138 val = ec_read(H8_USB_ALWAYS_ON);
139 val |= H8_USB_ALWAYS_ON_ENABLE; // Set bit 0
140 val &= ~H8_USB_ALWAYS_ON_AC_ONLY; // Clear bits 2 and 3
141 ec_write(H8_USB_ALWAYS_ON, val);
142 break;
143
144 case UAO_AC_ONLY:
145 val = ec_read(H8_USB_ALWAYS_ON);
146 // Set bits 0,2,3
147 val |= (H8_USB_ALWAYS_ON_ENABLE | H8_USB_ALWAYS_ON_AC_ONLY);
148 ec_write(H8_USB_ALWAYS_ON, val);
149 break;
150 }
151}
152
Sven Schnelle86e1aea2011-06-16 16:43:04 +0200153void h8_usb_power_enable(int onoff)
154{
155 if (onoff)
156 ec_set_bit(0x3b, 4);
157 else
158 ec_clr_bit(0x3b, 4);
159}
160
Sven Schnellecf7dffe2011-04-27 19:47:28 +0000161int h8_ultrabay_device_present(void)
162{
163 return ec_read(H8_STATUS1) & 0x5 ? 0 : 1;
164}
165
Peter Stuge77a5abe2013-07-06 19:44:47 +0200166u8 h8_build_id_and_function_spec_version(char *buf, u8 buf_len)
167{
168 u8 i, c;
169 char str[16 + 1]; /* 16 ASCII chars + \0 */
170
171 /* Build ID */
172 for (i = 0; i < 8; i++) {
173 c = ec_read(0xf0 + i);
174 if (c < 0x20 || c > 0x7f) {
Elyes HAOUASaf56a772020-07-22 20:36:20 +0200175 i = snprintf(str, sizeof(str), "*INVALID");
Peter Stuge77a5abe2013-07-06 19:44:47 +0200176 break;
177 }
178 str[i] = c;
179 }
180
181 /* EC firmware function specification version */
Elyes HAOUASaf56a772020-07-22 20:36:20 +0200182 i += snprintf(str + i, sizeof(str) - i, "-%u.%u", ec_read(0xef), ec_read(0xeb));
Peter Stuge77a5abe2013-07-06 19:44:47 +0200183
184 i = MIN(buf_len, i);
185 memcpy(buf, str, i);
186
187 return i;
188}
189
Julius Wernercd49cce2019-03-05 16:53:33 -0800190#if CONFIG(GENERATE_SMBIOS_TABLES)
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100191static void h8_smbios_strings(struct device *dev, struct smbios_type11 *t)
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200192{
193 char tpec[] = "IBM ThinkPad Embedded Controller -[ ]-";
194
195 h8_build_id_and_function_spec_version(tpec + 35, 17);
196
197 t->count = smbios_add_string(t->eos, tpec);
198}
Paul Menzel2a4a4522016-12-08 00:03:38 +0100199#endif
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200200
Elyes HAOUAS6572bdd2018-05-04 18:32:11 +0200201static void h8_init(struct device *dev)
Vladimir Serbinenko852014c2015-05-27 08:30:47 +0200202{
Timothy Pearson448e3862015-11-24 14:12:01 -0600203 pc_keyboard_init(NO_AUX_DEVICE);
Vladimir Serbinenko852014c2015-05-27 08:30:47 +0200204}
205
Julius Wernercd49cce2019-03-05 16:53:33 -0800206#if CONFIG(HAVE_ACPI_TABLES)
Patrick Rudolphf1114d82017-11-14 19:00:20 +0100207static const char *h8_acpi_name(const struct device *dev)
208{
209 return "EC";
210}
211#endif
212
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200213struct device_operations h8_dev_ops = {
Julius Wernercd49cce2019-03-05 16:53:33 -0800214#if CONFIG(GENERATE_SMBIOS_TABLES)
Vladimir Serbinenko852014c2015-05-27 08:30:47 +0200215 .get_smbios_strings = h8_smbios_strings,
Paul Menzel2a4a4522016-12-08 00:03:38 +0100216#endif
Julius Wernercd49cce2019-03-05 16:53:33 -0800217#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +0200218 .acpi_fill_ssdt = h8_ssdt_generator,
Patrick Rudolphf1114d82017-11-14 19:00:20 +0100219 .acpi_name = h8_acpi_name,
220#endif
Vladimir Serbinenko852014c2015-05-27 08:30:47 +0200221 .init = h8_init,
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200222};
223
Bill XIE4611ad82020-03-21 02:07:41 +0800224void __weak h8_mb_init(void){ /* NOOP */ }
225
Nico Huber6444b522016-11-14 00:49:29 +0200226static void h8_enable(struct device *dev)
Sven Schnelleffcd1432011-04-11 19:43:32 +0000227{
Nico Huber6444b522016-11-14 00:49:29 +0200228 struct ec_lenovo_h8_config *conf = dev->chip_info;
Alexander Couzensc5a6fb82016-10-16 06:55:19 +0200229 u8 val;
Patrick Rudolpha959a142017-05-24 18:14:43 +0200230 u8 beepmask0, beepmask1, reg8;
Sven Schnelle1b8068e2011-06-05 20:47:49 +0200231
Nico Huber6444b522016-11-14 00:49:29 +0200232 dev->ops = &h8_dev_ops;
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200233
Bill XIE54f45c62017-10-26 11:55:34 +0800234 ec_clear_out_queue();
Nico Huber6444b522016-11-14 00:49:29 +0200235 h8_log_ec_version();
Sven Schnelleffcd1432011-04-11 19:43:32 +0000236
Patrick Rudolpha959a142017-05-24 18:14:43 +0200237 /* Always enable I/O range 0x1600-0x160f and thermal management */
238 reg8 = conf->config0;
239 reg8 |= H8_CONFIG0_SMM_H8_ENABLE;
240 reg8 |= H8_CONFIG0_TC_ENABLE;
241 ec_write(H8_CONFIG0, reg8);
Vladimir Serbinenkof3c3dae2014-01-12 15:19:50 +0100242
Patrick Rudolpha959a142017-05-24 18:14:43 +0200243 reg8 = conf->config1;
Nico Huber6444b522016-11-14 00:49:29 +0200244 if (conf->has_keyboard_backlight) {
Angel Pons9dc1c512020-11-02 20:59:32 +0100245 /* Default to both backlights */
246 reg8 = (reg8 & 0xf3) | ((get_int_option("backlight", 0) & 0x3) << 2);
Vladimir Serbinenkof3c3dae2014-01-12 15:19:50 +0100247 }
Patrick Rudolpha959a142017-05-24 18:14:43 +0200248 ec_write(H8_CONFIG1, reg8);
Nico Huber6444b522016-11-14 00:49:29 +0200249 ec_write(H8_CONFIG2, conf->config2);
250 ec_write(H8_CONFIG3, conf->config3);
Sven Schnelleffcd1432011-04-11 19:43:32 +0000251
Vladimir Serbinenko9a3b9c42014-01-11 20:56:47 +0100252 beepmask0 = conf->beepmask0;
253 beepmask1 = conf->beepmask1;
254
Angel Pons9dc1c512020-11-02 20:59:32 +0100255 if (conf->has_power_management_beeps) {
256 if (get_int_option("power_management_beeps", 1) == 0) {
257 beepmask0 = 0x00;
258 beepmask1 = 0x00;
259 }
Vladimir Serbinenko9a3b9c42014-01-11 20:56:47 +0100260 }
Vladimir Serbinenkoadd3f7f2014-01-11 20:58:20 +0100261
262 if (conf->has_power_management_beeps) {
Angel Pons9dc1c512020-11-02 20:59:32 +0100263 if (get_int_option("low_battery_beep", 1))
Vladimir Serbinenkoadd3f7f2014-01-11 20:58:20 +0100264 beepmask0 |= 2;
265 else
266 beepmask0 &= ~2;
267 }
Nico Huber6444b522016-11-14 00:49:29 +0200268
Vladimir Serbinenko9a3b9c42014-01-11 20:56:47 +0100269 ec_write(H8_SOUND_ENABLE0, beepmask0);
270 ec_write(H8_SOUND_ENABLE1, beepmask1);
Sven Schnelleffcd1432011-04-11 19:43:32 +0000271
Alexander Couzensf6dde952015-05-24 03:17:42 +0200272 /* silence sounds in queue */
Alexander Couzens318ed6f2016-10-16 07:44:26 +0200273 ec_write(H8_SOUND_REPEAT, 0x00);
Alexander Couzensf6dde952015-05-24 03:17:42 +0200274 ec_write(H8_SOUND_REG, 0x00);
275
Sven Schnelleffcd1432011-04-11 19:43:32 +0000276 ec_write(0x10, conf->event0_enable);
277 ec_write(0x11, conf->event1_enable);
278 ec_write(0x12, conf->event2_enable);
279 ec_write(0x13, conf->event3_enable);
280 ec_write(0x14, conf->event4_enable);
281 ec_write(0x15, conf->event5_enable);
282 ec_write(0x16, conf->event6_enable);
283 ec_write(0x17, conf->event7_enable);
284 ec_write(0x18, conf->event8_enable);
285 ec_write(0x19, conf->event9_enable);
286 ec_write(0x1a, conf->eventa_enable);
287 ec_write(0x1b, conf->eventb_enable);
288 ec_write(0x1c, conf->eventc_enable);
289 ec_write(0x1d, conf->eventd_enable);
290 ec_write(0x1e, conf->evente_enable);
291 ec_write(0x1f, conf->eventf_enable);
292
Sven Schnelle3e2f6792011-04-12 18:18:24 +0000293 ec_write(H8_FAN_CONTROL, H8_FAN_CONTROL_AUTO);
Nathaniel Roach4f4322d2018-11-10 08:34:44 +0800294
Angel Pons9dc1c512020-11-02 20:59:32 +0100295 h8_usb_always_on_enable(get_int_option("usb_always_on", 0));
Vladimir Serbinenko8e7e5252014-01-11 03:45:53 +0100296
Angel Pons9dc1c512020-11-02 20:59:32 +0100297 h8_wlan_enable(get_int_option("wlan", 1));
Nico Huber6444b522016-11-14 00:49:29 +0200298
Vladimir Serbinenko1eda31c2014-01-11 04:18:52 +0100299 h8_trackpoint_enable(1);
Sven Schnelle86e1aea2011-06-16 16:43:04 +0200300 h8_usb_power_enable(1);
Sven Schnelleffcd1432011-04-11 19:43:32 +0000301
Angel Pons9157ccb2021-04-26 18:05:42 +0200302 unsigned int volume = get_int_option("volume", ~0);
303 if (volume <= 0xff && !acpi_is_wakeup_s3())
Angel Pons9dc1c512020-11-02 20:59:32 +0100304 ec_write(H8_VOLUME_CONTROL, volume);
Nico Huber6444b522016-11-14 00:49:29 +0200305
Julius Wernercd49cce2019-03-05 16:53:33 -0800306 val = (CONFIG(H8_SUPPORT_BT_ON_WIFI) || h8_has_bdc(dev)) &&
Patrick Rudolph7c2a6f92017-09-17 12:17:13 +0200307 h8_bluetooth_nv_enable();
Nico Huber6444b522016-11-14 00:49:29 +0200308 h8_bluetooth_enable(val);
309
Patrick Rudolphb8e325a2017-08-13 12:09:54 +0200310 val = h8_has_wwan(dev) && h8_wwan_nv_enable();
Nico Huber6444b522016-11-14 00:49:29 +0200311 h8_wwan_enable(val);
312
Angel Pons9dc1c512020-11-02 20:59:32 +0100313 if (conf->has_uwb)
314 h8_uwb_enable(get_int_option("uwb", 1));
Nico Huber6444b522016-11-14 00:49:29 +0200315
Angel Pons9dc1c512020-11-02 20:59:32 +0100316 h8_fn_ctrl_swap(get_int_option("fn_ctrl_swap", 0));
Nico Huber6444b522016-11-14 00:49:29 +0200317
Angel Pons9dc1c512020-11-02 20:59:32 +0100318 h8_sticky_fn(get_int_option("sticky_fn", 0));
Nico Huber6444b522016-11-14 00:49:29 +0200319
Angel Pons9dc1c512020-11-02 20:59:32 +0100320 if (CONFIG(H8_HAS_PRIMARY_FN_KEYS))
321 f1_to_f12_as_primary(get_int_option("f1_to_f12_as_primary", 1));
Nico Huber6444b522016-11-14 00:49:29 +0200322
Angel Pons9dc1c512020-11-02 20:59:32 +0100323 h8_charge_priority(get_int_option("first_battery", PRIMARY_BATTERY));
Nico Huber6444b522016-11-14 00:49:29 +0200324
325 h8_set_audio_mute(0);
Bill XIE4611ad82020-03-21 02:07:41 +0800326 h8_mb_init();
Sven Schnelleffcd1432011-04-11 19:43:32 +0000327}
328
329struct chip_operations ec_lenovo_h8_ops = {
330 CHIP_NAME("Lenovo H8 EC")
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200331 .enable_dev = h8_enable,
Sven Schnelleffcd1432011-04-11 19:43:32 +0000332};