From 6493af0aeb66c95fb3204ae68b46aa62ec85262f Mon Sep 17 00:00:00 2001 From: Ting Shen Date: Tue, 6 Sep 2022 18:01:37 +0800 Subject: zephyr/test: add rt9490 charger emulator Implemented a rt9490 charger emulator with basic i2c read/write, and extra peek/reset reg functions BUG=b:244233556,b:243841599 TEST=tested with CL:3873241 BRANCH=none Signed-off-by: Ting Shen Change-Id: I87b77cf2b0bc0a0d89f2f76353e16b9296dc5596 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3873240 Tested-by: Ting Shen Commit-Queue: Ting Shen Reviewed-by: Jeremy Bettis --- zephyr/emul/emul_rt9490.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 zephyr/emul/emul_rt9490.c (limited to 'zephyr/emul/emul_rt9490.c') diff --git a/zephyr/emul/emul_rt9490.c b/zephyr/emul/emul_rt9490.c new file mode 100644 index 0000000000..7c13cbe9e4 --- /dev/null +++ b/zephyr/emul/emul_rt9490.c @@ -0,0 +1,105 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include + +#include "driver/charger/rt9490.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_rt9490.h" +#include "emul/emul_stub_device.h" +#include "util.h" + +#define DT_DRV_COMPAT zephyr_rt9490_emul + +#define RT9490_REG_MAX 255 + +struct rt9490_data { + struct i2c_common_emul_data common; + uint8_t regs[RT9490_REG_MAX + 1]; +}; + +static const uint8_t default_values[RT9490_REG_MAX + 1] = { + [RT9490_REG_SAFETY_TMR_CTRL] = 0x3D, + [RT9490_REG_ADD_CTRL0] = 0x76, +}; + +void rt9490_emul_reset_regs(const struct emul *emul) +{ + struct rt9490_data *data = emul->data; + + memcpy(data->regs, default_values, RT9490_REG_MAX + 1); +} + +int rt9490_emul_peek_reg(const struct emul *emul, int reg) +{ + struct rt9490_data *data = emul->data; + uint8_t *regs = data->regs; + + if (!IN_RANGE(reg, 0, RT9490_REG_MAX)) { + return -1; + } + return regs[reg]; +} + +static int rt9490_emul_read(const struct emul *emul, int reg, uint8_t *val, + int bytes, void *unused_data) +{ + struct rt9490_data *data = emul->data; + uint8_t *regs = data->regs; + + if (!IN_RANGE(reg, 0, RT9490_REG_MAX)) { + return -1; + } + *val = regs[reg]; + + return 0; +} + +static int rt9490_emul_write(const struct emul *emul, int reg, uint8_t val, + int bytes, void *unused_data) +{ + struct rt9490_data *data = emul->data; + uint8_t *regs = data->regs; + + if (!IN_RANGE(reg, 0, RT9490_REG_MAX) || !IN_RANGE(val, 0, UINT8_MAX)) { + return -1; + } + regs[reg] = val; + + return 0; +} + +static int rt9490_emul_init(const struct emul *emul, + const struct device *parent) +{ + struct rt9490_data *data = (struct rt9490_data *)emul->data; + struct i2c_common_emul_data *common_data = &data->common; + + i2c_common_emul_init(common_data); + i2c_common_emul_set_read_func(common_data, rt9490_emul_read, NULL); + i2c_common_emul_set_write_func(common_data, rt9490_emul_write, NULL); + + rt9490_emul_reset_regs(emul); + + return 0; +} + +#define INIT_RT9490_EMUL(n) \ + static struct i2c_common_emul_cfg common_cfg_##n; \ + static struct rt9490_data rt9490_data_##n; \ + static struct i2c_common_emul_cfg common_cfg_##n = { \ + .dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)), \ + .data = &rt9490_data_##n.common, \ + .addr = DT_INST_REG_ADDR(n) \ + }; \ + static struct rt9490_data rt9490_data_##n = { \ + .common = { .cfg = &common_cfg_##n } \ + }; \ + EMUL_DT_INST_DEFINE(n, rt9490_emul_init, &rt9490_data_##n, \ + &common_cfg_##n, &i2c_common_emul_api) + +DT_INST_FOREACH_STATUS_OKAY(INIT_RT9490_EMUL) + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); -- cgit v1.2.1 From 71b2ef709dcb14260f5fdaa3ab4ced005a29fb46 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 12 Sep 2022 14:54:36 -0400 Subject: Update license boilerplate text in source code files Normally we don't do this, but enough changes have accumulated that we're doing a tree-wide one-off update of the name & style. BRANCH=none BUG=chromium:1098010 TEST=`repo upload` works Change-Id: Icd3a1723c20595356af83d190b2c6a9078b3013b Signed-off-by: Mike Frysinger Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3891203 Reviewed-by: Jeremy Bettis Reviewed-by: Jack Rosenthal --- zephyr/emul/emul_rt9490.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr/emul/emul_rt9490.c') diff --git a/zephyr/emul/emul_rt9490.c b/zephyr/emul/emul_rt9490.c index 7c13cbe9e4..5c0e7bf8b9 100644 --- a/zephyr/emul/emul_rt9490.c +++ b/zephyr/emul/emul_rt9490.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The ChromiumOS Authors. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -- cgit v1.2.1 From 82f49ef34c349d4b6ddd71d75019fa2636f4537f Mon Sep 17 00:00:00 2001 From: Ting Shen Date: Wed, 14 Sep 2022 18:07:22 +0800 Subject: zephyr: emul_rt9490: fix incorrect multibyte IO implementation When performing multibyte IO, the `reg` parameter means the first byte of this IO operation, it won't increase during consecutive read/write operations. The actual byte to access is offsetted by the `bytes` parameter. BUG=none TEST=pass all tests BRANCH=none Signed-off-by: Ting Shen Change-Id: I15c046fcfb8dbcc05f260bd929da32b7d86a3c90 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3896554 Code-Coverage: Zoss Commit-Queue: Ting Shen Tested-by: Ting Shen Reviewed-by: Eric Yilun Lin --- zephyr/emul/emul_rt9490.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'zephyr/emul/emul_rt9490.c') diff --git a/zephyr/emul/emul_rt9490.c b/zephyr/emul/emul_rt9490.c index 5c0e7bf8b9..dbc2500e90 100644 --- a/zephyr/emul/emul_rt9490.c +++ b/zephyr/emul/emul_rt9490.c @@ -48,11 +48,12 @@ static int rt9490_emul_read(const struct emul *emul, int reg, uint8_t *val, { struct rt9490_data *data = emul->data; uint8_t *regs = data->regs; + int pos = reg + bytes; - if (!IN_RANGE(reg, 0, RT9490_REG_MAX)) { + if (!IN_RANGE(pos, 0, RT9490_REG_MAX)) { return -1; } - *val = regs[reg]; + *val = regs[pos]; return 0; } @@ -62,11 +63,12 @@ static int rt9490_emul_write(const struct emul *emul, int reg, uint8_t val, { struct rt9490_data *data = emul->data; uint8_t *regs = data->regs; + int pos = reg + bytes - 1; - if (!IN_RANGE(reg, 0, RT9490_REG_MAX) || !IN_RANGE(val, 0, UINT8_MAX)) { + if (!IN_RANGE(pos, 0, RT9490_REG_MAX) || !IN_RANGE(val, 0, UINT8_MAX)) { return -1; } - regs[reg] = val; + regs[pos] = val; return 0; } -- cgit v1.2.1