Jonathon Hall has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/74364 )
Change subject: src/mb/purism/librem_cnl: Enable Librem 14 jack detect with fixed EC ......................................................................
src/mb/purism/librem_cnl: Enable Librem 14 jack detect with fixed EC
Use verbs enabling jack detect if the EC firmware has been updated with fixed jack detection.
Provide system76_ec_cmd() to send arbitrary commands to the EC.
Provide librem_ec_has_jack_detect() to probe for the jack detect fix.
Test: Build Librem 14 and boot with latest EC, test headset jack detection.
Change-Id: I57a27b1d51e4f6c7c712bcb2823d21692b9c5ce6 Signed-off-by: Jonathon Hall jonathon.hall@puri.sm --- M src/ec/purism/librem-ec/Makefile.inc A src/ec/purism/librem-ec/librem_ec.c A src/ec/purism/librem-ec/librem_ec.h M src/ec/system76/ec/system76_ec.c A src/ec/system76/ec/system76_ec.h M src/mainboard/purism/librem_cnl/variants/librem_14/hda_verb.c 6 files changed, 147 insertions(+), 2 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/64/74364/1
diff --git a/src/ec/purism/librem-ec/Makefile.inc b/src/ec/purism/librem-ec/Makefile.inc index 5e2b2df..464371f 100644 --- a/src/ec/purism/librem-ec/Makefile.inc +++ b/src/ec/purism/librem-ec/Makefile.inc @@ -2,6 +2,7 @@ ifeq ($(CONFIG_EC_LIBREM_EC),y)
all-y += ../../system76/ec/system76_ec.c +all-y += librem_ec.c smm-$(CONFIG_DEBUG_SMI) += ../../system76/ec/system76_ec.c
endif diff --git a/src/ec/purism/librem-ec/librem_ec.c b/src/ec/purism/librem-ec/librem_ec.c new file mode 100644 index 0000000..93cc2fb --- /dev/null +++ b/src/ec/purism/librem-ec/librem_ec.c @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include "librem_ec.h" +#include "../../system76/ec/system76_ec.h" +#include <stddef.h> + +#define CMD_PROBE 1 + +bool librem_ec_has_jack_detect(void) +{ + /* The 'flags' field in the probe command reply was added in an update. + * Send 4 bytes of zeroes in the "request" to zero out the field if the + * EC does not set it for its reply. */ + const uint8_t request_data[4] = {0}; + uint8_t reply_data[4] = {0}; + if (!system76_ec_cmd(CMD_PROBE, request_data, ARRAY_SIZE(request_data), + reply_data, ARRAY_SIZE(reply_data))) + return false; + /* Byte 3 is flags, bit 0 is the jack detect flag */ + return reply_data[3] & 0x01; +} diff --git a/src/ec/purism/librem-ec/librem_ec.h b/src/ec/purism/librem-ec/librem_ec.h new file mode 100644 index 0000000..90548c6 --- /dev/null +++ b/src/ec/purism/librem-ec/librem_ec.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef LIBREM_EC_H +#define LIBREM_EC_H + +#include <stdbool.h> + +/* Check whether librem-ec has working jack detect. This was fixed in an + * update, so we only use the verbs with jack detect if the EC has been updated. + */ +bool librem_ec_has_jack_detect(void); + +#endif diff --git a/src/ec/system76/ec/system76_ec.c b/src/ec/system76/ec/system76_ec.c index ddcb602..b1a053d 100644 --- a/src/ec/system76/ec/system76_ec.c +++ b/src/ec/system76/ec/system76_ec.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0-only */
+#include "system76_ec.h" #include <arch/io.h> #include <console/system76_ec.h> +#include <console/console.h> #include <timer.h>
// This is the command region for System76 EC firmware. It must be @@ -11,10 +13,13 @@
#define REG_CMD 0 #define REG_RESULT 1 +#define REG_DATA 2 // Start of command data
// When command register is 0, command is complete #define CMD_FINISHED 0
+#define RESULT_OK 0 + // Print command. Registers are unique for each command #define CMD_PRINT 4 #define CMD_PRINT_REG_FLAGS 2 @@ -59,3 +64,47 @@ if (byte == '\n' || len >= (SYSTEM76_EC_SIZE - CMD_PRINT_REG_DATA)) system76_ec_flush(); } + +bool system76_ec_cmd(uint8_t cmd, const uint8_t *request_data, + uint8_t request_size, uint8_t *reply_data, uint8_t reply_size) +{ + if (request_size > SYSTEM76_EC_SIZE - REG_DATA || + reply_size > SYSTEM76_EC_SIZE - REG_DATA) { + printk(BIOS_ERR, "EC command %d too long - request size %d, reply size %d\n", + cmd, request_size, reply_size); + return false; + } + + /* If any data were buffered by system76_ec_print(), flush it first */ + uint8_t buffered_len = system76_ec_read(CMD_PRINT_REG_LEN); + if (buffered_len > 0) + system76_ec_flush(); + + /* Write the data */ + uint8_t i; + for (i = 0; i < request_size; ++i) + system76_ec_write(REG_DATA+i, request_data[i]); + + /* Write the command */ + system76_ec_write(REG_CMD, cmd); + + /* Wait for the command to complete */ + bool ret = true; + int elapsed = wait_ms(1000, system76_ec_read(REG_CMD) == CMD_FINISHED); + if (elapsed == 0) { + /* Timed out: fail the command, don't attempt to read a reply. */ + ret = false; + } else { + /* Read the reply */ + for (i = 0; i < reply_size; ++i) + reply_data[i] = system76_ec_read(REG_DATA+i); + /* Check the reply status */ + ret = (system76_ec_read(REG_RESULT) == RESULT_OK); + } + + /* Reset the flags and length so we can buffer console prints again */ + system76_ec_write(CMD_PRINT_REG_FLAGS, 0); + system76_ec_write(CMD_PRINT_REG_LEN, 0); + + return ret; +} diff --git a/src/ec/system76/ec/system76_ec.h b/src/ec/system76/ec/system76_ec.h new file mode 100644 index 0000000..aea7bf1 --- /dev/null +++ b/src/ec/system76/ec/system76_ec.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SYSTEM76_EC_H +#define SYSTEM76_EC_H + +#include <stdbool.h> +#include <stdint.h> + +/* Send a command to the EC. request_data/request_size are the request payload, + * request_data can be NULL if request_size is 0. reply_data/reply_size are + * the reply payload, reply_data can be NULL if reply_size is 0. */ +bool system76_ec_cmd(uint8_t cmd, const uint8_t *request_data, + uint8_t request_size, uint8_t *reply_data, uint8_t reply_size); + +#endif diff --git a/src/mainboard/purism/librem_cnl/variants/librem_14/hda_verb.c b/src/mainboard/purism/librem_cnl/variants/librem_14/hda_verb.c index 1256c17..b9aecf0 100644 --- a/src/mainboard/purism/librem_cnl/variants/librem_14/hda_verb.c +++ b/src/mainboard/purism/librem_cnl/variants/librem_14/hda_verb.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-only */
#include <device/azalia_device.h> +#include <ec/purism/librem-ec/librem_ec.h> +#include <console/console.h>
const u32 cim_verb_data[] = { 0x10ec0256, /* Codec Vendor/Device ID: Realtek ALC256 */ @@ -14,12 +16,12 @@ AZALIA_PIN_CFG(0, 0x13, 0x411111f0), /* NC */ AZALIA_PIN_CFG(0, 0x14, 0x90170110), /* Internal speakers */ AZALIA_PIN_CFG(0, 0x18, 0x411111f0), /* NC */ - AZALIA_PIN_CFG(0, 0x19, 0x04a11130), /* Jack analog mic */ + AZALIA_PIN_CFG(0, 0x19, 0x02a11030), /* Jack analog mic */ AZALIA_PIN_CFG(0, 0x1a, 0x411111f0), /* NC */ AZALIA_PIN_CFG(0, 0x1b, 0x411111f0), /* NC */ AZALIA_PIN_CFG(0, 0x1d, 0x411111f0), /* NC */ AZALIA_PIN_CFG(0, 0x1e, 0x411111f0), /* NC */ - AZALIA_PIN_CFG(0, 0x21, 0x04211120), /* Jack analog out */ + AZALIA_PIN_CFG(0, 0x21, 0x02211020), /* Jack analog out */
/* Hidden SW reset */ 0x0205001a, @@ -58,3 +60,27 @@ const u32 pc_beep_verbs[] = {};
AZALIA_ARRAY_SIZES; + +/* Older verbs with no jack detect - needed if an older Librem EC is in use that + * lacks jack detect. Headphones can be selected manually. */ +static const u32 no_jack_detect_verbs[] = { + AZALIA_PIN_CFG(0, 0x19, 0x04a11130), /* Jack analog mic */ + AZALIA_PIN_CFG(0, 0x21, 0x04211120), /* Jack analog out */ +}; + +void mainboard_azalia_program_runtime_verbs(u8 *base, u32 viddid) +{ + if (viddid == 0x10ec0256) { + /* Now that the codec is configured, we can check if the EC has + * jack detect. */ + if (librem_ec_has_jack_detect()) { + printk(BIOS_INFO, "EC jack detect enabled\n"); + } else { + printk(BIOS_WARNING, "EC firmware lacks jack detect, applying workaround\n"); + /* The EC firmware lacks jack detect. Program the + * older workaround verbs with no jack detect. */ + azalia_program_verb_table(base, no_jack_detect_verbs, + ARRAY_SIZE(no_jack_detect_verbs)); + } + } +}