+int cros_ec_check_feature(struct udevice *dev, int feature)
+{
+ struct ec_response_get_features r;
+ int rv;
+
+ rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
+ if (rv)
+ return rv;
+
+ if (feature >= 8 * sizeof(r.flags))
+ return -1;
+
+ return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
+}
+
+/*
+ * Query the EC for specified mask indicating enabled events.
+ * The EC maintains separate event masks for SMI, SCI and WAKE.
+ */
+static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
+ uint64_t *value)
+{
+ int ret;
+ struct ec_params_host_event req;
+ struct ec_response_host_event rsp;
+
+ req.action = action;
+ req.mask_type = mask;
+ if (action != EC_HOST_EVENT_GET)
+ req.value = *value;
+ else
+ *value = 0;
+ ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
+ sizeof(rsp));
+
+ if (action != EC_HOST_EVENT_GET)
+ return ret;
+ if (ret == 0)
+ *value = rsp.value;
+
+ return ret;
+}
+
+static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
+ uint action, uint64_t *value)
+{
+ int ret = -1;
+ struct ec_params_host_event_mask req;
+ struct ec_response_host_event_mask rsp;
+
+ if (hcmd == INVALID_HCMD)
+ return ret;
+
+ if (action != EC_HOST_EVENT_GET)
+ req.mask = (uint32_t)*value;
+ else
+ *value = 0;
+
+ ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
+ if (action != EC_HOST_EVENT_GET)
+ return ret;
+ if (ret == 0)
+ *value = rsp.mask;
+
+ return ret;
+}
+
+bool cros_ec_is_uhepi_supported(struct udevice *dev)
+{
+#define UHEPI_SUPPORTED 1
+#define UHEPI_NOT_SUPPORTED 2
+ static int uhepi_support;
+
+ if (!uhepi_support) {
+ uhepi_support = cros_ec_check_feature(dev,
+ EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
+ UHEPI_NOT_SUPPORTED;
+ log_debug("Chrome EC: UHEPI %s\n",
+ uhepi_support == UHEPI_SUPPORTED ? "supported" :
+ "not supported");
+ }
+ return uhepi_support == UHEPI_SUPPORTED;
+}
+
+static int cros_ec_get_mask(struct udevice *dev, uint type)
+{
+ u64 value = 0;
+
+ if (cros_ec_is_uhepi_supported(dev)) {
+ cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
+ } else {
+ assert(type < ARRAY_SIZE(event_map));
+ cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
+ EC_HOST_EVENT_GET, &value);
+ }
+ return value;
+}
+
+static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
+{
+ if (cros_ec_is_uhepi_supported(dev))
+ return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
+
+ assert(type < ARRAY_SIZE(event_map));
+
+ return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
+ EC_HOST_EVENT_CLEAR, &mask);
+}
+
+uint64_t cros_ec_get_events_b(struct udevice *dev)
+{
+ return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
+}
+
+int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
+{
+ log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
+
+ return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
+}
+
+int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
+{
+ struct ec_params_charge_state p;
+ struct ec_response_charge_state r;
+ int ret;
+
+ p.cmd = CHARGE_STATE_CMD_GET_PARAM;
+ p.get_param.param = CS_PARAM_LIMIT_POWER;
+ ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
+ &r, sizeof(r));
+
+ /*
+ * If our EC doesn't support the LIMIT_POWER parameter, assume that
+ * LIMIT_POWER is not requested.
+ */
+ if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
+ log_warning("PARAM_LIMIT_POWER not supported by EC\n");
+ return -ENOSYS;
+ }
+
+ if (ret != sizeof(r.get_param))
+ return -EINVAL;
+
+ *limit_powerp = r.get_param.value;
+ return 0;
+}
+
+int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
+{
+ struct ec_params_config_power_button params;
+ int ret;
+
+ params.flags = flags;
+ ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
+ ¶ms, sizeof(params), NULL, 0);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
+{
+ u32 mask;
+ int ret;
+
+ ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
+ &mask);
+ if (ret < 0)
+ return ret;
+
+ return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
+}
+
+int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
+{
+ u32 mask;
+ int ret;
+
+ ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
+ &mask);
+ if (ret < 0)
+ return ret;
+
+ // Set lid close event state in the EC SMI event mask
+ if (enable)
+ mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
+ else
+ mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
+
+ ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
+ if (ret < 0)
+ return ret;
+
+ printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
+ return 0;
+}
+