summaryrefslogtreecommitdiff
path: root/zephyr/emul/emul_charger.c
diff options
context:
space:
mode:
authorAseda Aboagye <aaboagye@google.com>2022-01-10 17:26:56 -0600
committerAseda Aboagye <aaboagye@google.com>2022-01-10 17:26:56 -0600
commitdc11829e169a9c425860ec5cca949ef80df9e0b7 (patch)
tree0517b0831c6e52b347926a1b727741df380e908c /zephyr/emul/emul_charger.c
parentc5bd23a4b204565dab616f7fa4ee8a0b7b433d4c (diff)
parentb44d10f8f79cadb259cc7ab79714a0919fc0c4c8 (diff)
downloadchrome-ec-firmware-keeby-14119.B-main.tar.gz
Merge remote-tracking branch cros/main into firmware-keeby-14119.B-mainfirmware-keeby-14119.B-main
Relevant changes: git log --oneline c5bd23a4b..b44d10f8f -- baseboard/dedede board/cappy2 board/corori board/driblee board/gooey board/haboki board/lalala board/waddledoo2 common/charge_state_v2.c common/mkbp_* common/ocpc.c common/usbc/usb_tc_drp_acc_trysrc_sm.c common/usbc/usb_sm.c common/usbc/*_pd_* common/usbc/dp_alt_mode.c common/usbc/usb_prl_sm.c common/usbc/usb_pe_drp_sm.c common/usb_charger.c common/usb_common.c common/usbc_ocp.c driver/charger/sm5803.* driver/charger/isl923x.* driver/tcpm/raa489000.* driver/tcpm/it83* include/power/icelake.h include/intel_x86.h power/icelake.c power/intel_x86.c util/getversion.sh 42d03a001 config: change temp_sensor_power from config to gpio e296efb28 usb_common: Fix CONFIG_USB_PD_DISCHARGE_TCPC typo c346481f4 atomic: cast to unsigned when shifting 9b972a0f2 driver/tcpm/it83xx, it8xxx2: ITE inactive port return from HOOK a499d8fd4 driver/tcpm/it83xx, it8xxx2: set sleep mask for mixed TCPC case ed62e2583 TCPMv2: don't set the sleep mask for TCPC embedded in EC c962696e8 motion_sensor: Remove |int_signal| field 86b216794 ocpc: modify pre-charge target condition 6f8336eb4 dedede: Set MKBP event wake mask to 0 02d034df0 dedede: add stylus fw_config 4f7cd7509 atomic: use atomic_t where it is possible e3ffa0519 mkbp: change the type fifo_entries to atomic_t bb4c47af0 usb: use atomic_t where possible c6e513ee2 power/icelake: Add SLP_S5 as a watched power signal d89e49b20 power: Introduce S4 as a real power state ba8a3c9c0 chgstv2: Use chipset_in_state instead of naming states 23a975d12 i2c: Use declared initializers for i2c_ports: boards a-l 35865dbec TCPMv2: Guard DATA_RESET using CONFIG_USB_PD_DATA_RESET_MSG d4d8243ed i2c: Use declared initializers for i2c_ports: baseboards eba8d0305 RAA489000: Fixed RAA489000 max charging current e78b83e0f TCPMv2: Delay Data Reset until mode entry request 6230e60fc TCPMv2: Support Data Reset as DFP, initiator 412246836 intel_x86: Apply chipset resume init and suspend complete hooks f2809b72c config: rename CONFIG_HOSTCMD_ESPI to CONFIG_HOST_INTERFACE_ESPI BRANCH=None BUG=b:202796060 b:207805856 b:167983049 b:208318528 b:181983966 BUG=b:207328258 b:195416058 b:205285137 b:199919093 b:207055975 BUG=b:129159505 b:204947672 b:141363146 b:207082842 b:205675485 TEST=`make -j buildall` Signed-off-by: Aseda Aboagye <aaboagye@google.com> Change-Id: I980351977e11088a130e478df0701be4715f049b
Diffstat (limited to 'zephyr/emul/emul_charger.c')
-rw-r--r--zephyr/emul/emul_charger.c355
1 files changed, 0 insertions, 355 deletions
diff --git a/zephyr/emul/emul_charger.c b/zephyr/emul/emul_charger.c
deleted file mode 100644
index d584ab882b..0000000000
--- a/zephyr/emul/emul_charger.c
+++ /dev/null
@@ -1,355 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <logging/log.h>
-LOG_MODULE_REGISTER(charger_emul, CONFIG_TCPCI_EMUL_LOG_LEVEL);
-
-#include <zephyr.h>
-
-#include "common.h"
-#include "emul/emul_charger.h"
-#include "emul/emul_tcpci.h"
-#include "usb_pd.h"
-
-/** Structure of message used by USB-C charger emulator */
-struct charger_emul_msg {
- /** Reserved for k_fifo_* usage */
- void *fifo_reserved;
- /** TCPCI emulator message */
- struct tcpci_emul_msg msg;
- /** Time when message should be sent if message is delayed */
- uint64_t time;
-};
-
-/**
- * @brief Allocate message
- *
- * @param size Size of message buffer
- *
- * @return Pointer to new message on success
- * @return NULL on error
- */
-static struct charger_emul_msg *charger_emul_alloc_msg(size_t size)
-{
- struct charger_emul_msg *new_msg;
-
- new_msg = k_malloc(sizeof(struct charger_emul_msg));
- if (new_msg == NULL) {
- return NULL;
- }
-
- new_msg->msg.buf = k_malloc(size);
- if (new_msg->msg.buf == NULL) {
- k_free(new_msg);
- return NULL;
- }
-
- /* TCPCI message size count include type byte */
- new_msg->msg.cnt = size + 1;
-
- return new_msg;
-}
-
-/**
- * @brief Free message's memory
- *
- * @param msg Pointer to message
- */
-static void charger_emul_free_msg(struct charger_emul_msg *msg)
-{
- k_free(msg->msg.buf);
- k_free(msg);
-}
-
-/**
- * @brief Set header of the message
- *
- * @param data Pointer to USB-C charger emulator
- * @param msg Pointer to message
- * @param type Type of message
- * @param cnt Number of data objects
- */
-static void charger_emul_set_header(struct charger_emul_data *data,
- struct charger_emul_msg *msg,
- int type, int cnt)
-{
- /* Header msg id has only 3 bits and wraps around after 8 messages */
- uint16_t msg_id = data->msg_id & 0x7;
- uint16_t header = PD_HEADER(type, PD_ROLE_SOURCE, PD_ROLE_UFP, msg_id,
- cnt, PD_REV20, 0 /* ext */);
- data->msg_id++;
-
- msg->msg.buf[1] = (header >> 8) & 0xff;
- msg->msg.buf[0] = header & 0xff;
-}
-
-/**
- * @brief Work function which sends delayed messages
- *
- * @param work Pointer to work structure
- */
-static void charger_emul_delayed_send(struct k_work *work)
-{
- struct k_work_delayable *kwd = k_work_delayable_from_work(work);
- struct charger_emul_data *data = CONTAINER_OF(kwd,
- struct charger_emul_data,
- delayed_send);
- struct charger_emul_msg *msg;
- uint64_t now;
- int ec;
-
- while (!k_fifo_is_empty(&data->to_send)) {
- /*
- * It is safe to not check msg == NULL, because this thread is
- * the only one consumer
- */
- msg = k_fifo_peek_head(&data->to_send);
-
- now = k_uptime_get();
- if (now >= msg->time) {
- k_fifo_get(&data->to_send, K_FOREVER);
- ec = tcpci_emul_add_rx_msg(data->tcpci_emul, &msg->msg,
- true /* send alert */);
- if (ec) {
- charger_emul_free_msg(msg);
- }
- } else {
- k_work_reschedule(kwd, K_MSEC(msg->time - now));
- break;
- }
- }
-}
-
-/**
- * @brief Send message to TCPCI emulator or schedule message
- *
- * @param data Pointer to USB-C charger emulator
- * @param msg Pointer to message to send
- * @param delay Optional delay
- *
- * @return 0 on success
- * @return -EINVAL on TCPCI emulator add RX message error
- */
-static int charger_emul_send_msg(struct charger_emul_data *data,
- struct charger_emul_msg *msg, uint64_t delay)
-{
- uint64_t now;
- int ec;
-
- if (delay == 0) {
- ec = tcpci_emul_add_rx_msg(data->tcpci_emul, &msg->msg, true);
- if (ec) {
- charger_emul_free_msg(msg);
- }
-
- return ec;
- }
-
- now = k_uptime_get();
- msg->time = now + delay;
- k_fifo_put(&data->to_send, msg);
- /*
- * This will change execution time of delayed_send only if it is not
- * already scheduled
- */
- k_work_schedule(&data->delayed_send, K_MSEC(delay));
-
- return 0;
-}
-
-/**
- * @brief Send capability message which for now is hardcoded
- *
- * @param data Pointer to USB-C charger emulator
- * @param delay Optional delay
- *
- * @return 0 on success
- * @return -ENOMEM when there is no free memory for message
- * @return -EINVAL on TCPCI emulator add RX message error
- */
-static int charger_emul_send_capability_msg(struct charger_emul_data *data,
- uint64_t delay)
-{
- struct charger_emul_msg *msg;
-
- msg = charger_emul_alloc_msg(6);
- if (msg == NULL) {
- return -ENOMEM;
- }
-
- /* Capability with 5v@3A */
- charger_emul_set_header(data, msg, PD_DATA_SOURCE_CAP, 1);
-
- /* Fixed supply (type of supply) 0xc0 */
- msg->msg.buf[5] = 0x00;
- /* Dual role capable 0x20 */
- msg->msg.buf[5] |= 0x00;
- /* Unconstrained power 0x08 */
- msg->msg.buf[5] |= 0x08;
-
- /* 5V on bits 19-10 */
- msg->msg.buf[4] = 0x1;
- msg->msg.buf[3] = 0x90;
- /* 3A on bits 9-0 */
- msg->msg.buf[3] |= 0x1;
- msg->msg.buf[2] = 0x2c;
-
- /* Fill tcpci message structure */
- msg->msg.type = TCPCI_MSG_SOP;
-
- return charger_emul_send_msg(data, msg, delay);
-}
-
-/**
- * @brief Send control message with optional delay
- *
- * @param data Pointer to USB-C charger emulator
- * @param type Type of message
- * @param delay Optional delay
- *
- * @return 0 on success
- * @return -ENOMEM when there is no free memory for message
- * @return -EINVAL on TCPCI emulator add RX message error
- */
-static int charger_emul_send_control_msg(struct charger_emul_data *data,
- enum pd_ctrl_msg_type type,
- uint64_t delay)
-{
- struct charger_emul_msg *msg;
-
- msg = charger_emul_alloc_msg(2);
- if (msg == NULL) {
- return -ENOMEM;
- }
-
- charger_emul_set_header(data, msg, type, 0);
-
- /* Fill tcpci message structure */
- msg->msg.type = TCPCI_MSG_SOP;
-
- return charger_emul_send_msg(data, msg, delay);
-}
-
-/**
- * @brief Function called when TCPM wants to transmit message. Accept received
- * message and generate response.
- *
- * @param emul Pointer to TCPCI emulator
- * @param ops Pointer to partner operations structure
- * @param tx_msg Pointer to TX message buffer
- * @param type Type of message
- * @param retry Count of retries
- */
-static void charger_emul_transmit_op(const struct emul *emul,
- const struct tcpci_emul_partner_ops *ops,
- const struct tcpci_emul_msg *tx_msg,
- enum tcpci_msg_type type,
- int retry)
-{
- struct charger_emul_data *data = CONTAINER_OF(ops,
- struct charger_emul_data,
- ops);
- uint16_t header;
-
- /* Acknowledge that message was sent successfully */
- tcpci_emul_partner_msg_status(emul, TCPCI_EMUL_TX_SUCCESS);
-
- /* Handle only SOP messages */
- if (type != TCPCI_MSG_SOP) {
- return;
- }
-
- LOG_HEXDUMP_DBG(tx_msg->buf, tx_msg->cnt, "Charger received message");
-
- header = (tx_msg->buf[1] << 8) | tx_msg->buf[0];
-
- if (PD_HEADER_CNT(header)) {
- /* Handle data message */
- switch (PD_HEADER_TYPE(header)) {
- case PD_DATA_REQUEST:
- charger_emul_send_control_msg(data, PD_CTRL_ACCEPT, 0);
- /* PS ready after 15 ms */
- charger_emul_send_control_msg(data, PD_CTRL_PS_RDY, 15);
- break;
- case PD_DATA_VENDOR_DEF:
- /* VDM (vendor defined message) - ignore */
- break;
- default:
- charger_emul_send_control_msg(data, PD_CTRL_REJECT, 0);
- break;
- }
- } else {
- /* Handle control message */
- switch (PD_HEADER_TYPE(header)) {
- case PD_CTRL_GET_SOURCE_CAP:
- charger_emul_send_capability_msg(data, 0);
- break;
- case PD_CTRL_GET_SINK_CAP:
- charger_emul_send_control_msg(data, PD_CTRL_REJECT, 0);
- break;
- case PD_CTRL_DR_SWAP:
- charger_emul_send_control_msg(data, PD_CTRL_REJECT, 0);
- break;
- case PD_CTRL_SOFT_RESET:
- data->msg_id = 0;
- charger_emul_send_control_msg(data, PD_CTRL_ACCEPT, 0);
- /* Send capability after 15 ms to establish PD again */
- charger_emul_send_capability_msg(data, 15);
- break;
- default:
- charger_emul_send_control_msg(data, PD_CTRL_REJECT, 0);
- break;
- }
- }
-}
-
-/**
- * @brief Function called when TCPM consumes message. Free message that is no
- * longer needed.
- *
- * @param emul Pointer to TCPCI emulator
- * @param ops Pointer to partner operations structure
- * @param rx_msg Message that was consumed by TCPM
- */
-static void charger_emul_rx_consumed_op(
- const struct emul *emul,
- const struct tcpci_emul_partner_ops *ops,
- const struct tcpci_emul_msg *rx_msg)
-{
- struct charger_emul_msg *msg = CONTAINER_OF(rx_msg,
- struct charger_emul_msg,
- msg);
-
- charger_emul_free_msg(msg);
-}
-
-/** Check description in emul_charger.h */
-int charger_emul_connect_to_tcpci(struct charger_emul_data *data,
- const struct emul *tcpci_emul)
-{
- int ec;
-
- tcpci_emul_set_partner_ops(tcpci_emul, &data->ops);
- ec = tcpci_emul_connect_partner(tcpci_emul, PD_ROLE_SOURCE,
- TYPEC_CC_VOLT_RP_3_0,
- TYPEC_CC_VOLT_OPEN, POLARITY_CC1);
- if (ec) {
- return ec;
- }
-
- data->tcpci_emul = tcpci_emul;
-
- return charger_emul_send_capability_msg(data, 0);
-}
-
-/** Check description in emul_charger.h */
-void charger_emul_init(struct charger_emul_data *data)
-{
- k_work_init_delayable(&data->delayed_send, charger_emul_delayed_send);
- k_fifo_init(&data->to_send);
-
- data->ops.transmit = charger_emul_transmit_op;
- data->ops.rx_consumed = charger_emul_rx_consumed_op;
-}