summaryrefslogtreecommitdiff
path: root/zephyr/emul/emul_pi3usb9201.c
diff options
context:
space:
mode:
Diffstat (limited to 'zephyr/emul/emul_pi3usb9201.c')
-rw-r--r--zephyr/emul/emul_pi3usb9201.c76
1 files changed, 31 insertions, 45 deletions
diff --git a/zephyr/emul/emul_pi3usb9201.c b/zephyr/emul/emul_pi3usb9201.c
index 9115a84515..3b1193d9b1 100644
--- a/zephyr/emul/emul_pi3usb9201.c
+++ b/zephyr/emul/emul_pi3usb9201.c
@@ -1,4 +1,4 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
+/* Copyright 2021 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
@@ -11,6 +11,8 @@
#include <zephyr/drivers/i2c_emul.h>
#include "emul/emul_pi3usb9201.h"
+#include "emul/emul_stub_device.h"
+#include "emul/emul_common_i2c.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(emul_pi3usb9201, LOG_LEVEL_DBG);
@@ -20,6 +22,8 @@ LOG_MODULE_REGISTER(emul_pi3usb9201, LOG_LEVEL_DBG);
/** Run-time data used by the emulator */
struct pi3usb9201_emul_data {
+ /** Common I2C data */
+ struct i2c_common_emul_data common;
/** I2C emulator detail */
struct i2c_emul emul;
/** pi3usb9201 device being emulated */
@@ -32,50 +36,48 @@ struct pi3usb9201_emul_data {
/** Static configuration for the emulator */
struct pi3usb9201_emul_cfg {
- /** Label of the I2C bus this emulator connects to */
- const char *i2c_label;
/** Pointer to run-time data */
struct pi3usb9201_emul_data *data;
/** Address of pi3usb9201 on i2c bus */
uint16_t addr;
};
-int pi3usb9201_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val)
+int pi3usb9201_emul_set_reg(const struct emul *emul, int reg, uint8_t val)
{
struct pi3usb9201_emul_data *data;
if (!EMUL_REG_IS_VALID(reg))
return -EIO;
- data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul);
+ data = emul->data;
data->reg[reg] = val;
return 0;
}
-int pi3usb9201_emul_get_reg(struct i2c_emul *emul, int reg, uint8_t *val)
+int pi3usb9201_emul_get_reg(const struct emul *emul, int reg, uint8_t *val)
{
struct pi3usb9201_emul_data *data;
if (!EMUL_REG_IS_VALID(reg))
return -EIO;
- data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul);
+ data = emul->data;
*val = data->reg[reg];
return 0;
}
-static void pi3usb9201_emul_reset(struct i2c_emul *emul)
+static void pi3usb9201_emul_reset(const struct emul *emul)
{
struct pi3usb9201_emul_data *data;
- data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul);
+ data = emul->data;
- data->reg[PI3USB9201_REG_CTRL_1] = 0;
- data->reg[PI3USB9201_REG_CTRL_2] = 0;
+ data->reg[PI3USB9201_REG_CTRL_1] = 0;
+ data->reg[PI3USB9201_REG_CTRL_2] = 0;
data->reg[PI3USB9201_REG_CLIENT_STS] = 0;
- data->reg[PI3USB9201_REG_HOST_STS] = 0;
+ data->reg[PI3USB9201_REG_HOST_STS] = 0;
}
/**
@@ -91,13 +93,14 @@ static void pi3usb9201_emul_reset(struct i2c_emul *emul)
* @retval 0 If successful
* @retval -EIO General input / output error
*/
-static int pi3usb9201_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs,
- int num_msgs, int addr)
+static int pi3usb9201_emul_transfer(const struct emul *emul,
+ struct i2c_msg *msgs, int num_msgs,
+ int addr)
{
const struct pi3usb9201_emul_cfg *cfg;
struct pi3usb9201_emul_data *data;
- data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul);
+ data = emul->data;
cfg = data->cfg;
if (cfg->addr != addr) {
@@ -109,18 +112,18 @@ static int pi3usb9201_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs,
i2c_dump_msgs("emul", msgs, num_msgs, addr);
if (num_msgs == 1) {
- if (!(((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE)
- && (msgs[0].len == 2))) {
+ if (!(((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE) &&
+ (msgs[0].len == 2))) {
LOG_ERR("Unexpected write msgs");
return -EIO;
}
return pi3usb9201_emul_set_reg(emul, msgs[0].buf[0],
msgs[0].buf[1]);
} else if (num_msgs == 2) {
- if (!(((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE)
- && (msgs[0].len == 1)
- && ((msgs[1].flags & I2C_MSG_RW_MASK) == I2C_MSG_READ)
- && (msgs[1].len == 1))) {
+ if (!(((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE) &&
+ (msgs[0].len == 1) &&
+ ((msgs[1].flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) &&
+ (msgs[1].len == 1))) {
LOG_ERR("Unexpected read msgs");
return -EIO;
}
@@ -130,7 +133,6 @@ static int pi3usb9201_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs,
LOG_ERR("Unexpected num_msgs");
return -EIO;
}
-
}
/* Device instantiation */
@@ -151,45 +153,29 @@ static struct i2c_emul_api pi3usb9201_emul_api = {
* @return 0 indicating success (always)
*/
static int pi3usb9201_emul_init(const struct emul *emul,
- const struct device *parent)
+ const struct device *parent)
{
const struct pi3usb9201_emul_cfg *cfg = emul->cfg;
struct pi3usb9201_emul_data *data = cfg->data;
- int ret;
- data->emul.api = &pi3usb9201_emul_api;
- data->emul.addr = cfg->addr;
data->i2c = parent;
data->cfg = cfg;
- ret = i2c_emul_register(parent, emul->dev_label, &data->emul);
-
- pi3usb9201_emul_reset(&data->emul);
+ pi3usb9201_emul_reset(emul);
- return ret;
+ return 0;
}
#define PI3USB9201_EMUL(n) \
static struct pi3usb9201_emul_data pi3usb9201_emul_data_##n = {}; \
static const struct pi3usb9201_emul_cfg pi3usb9201_emul_cfg_##n = { \
- .i2c_label = DT_INST_BUS_LABEL(n), \
.data = &pi3usb9201_emul_data_##n, \
.addr = DT_INST_REG_ADDR(n), \
}; \
- EMUL_DEFINE(pi3usb9201_emul_init, DT_DRV_INST(n), \
- &pi3usb9201_emul_cfg_##n, &pi3usb9201_emul_data_##n)
+ EMUL_DT_INST_DEFINE(n, pi3usb9201_emul_init, \
+ &pi3usb9201_emul_data_##n, \
+ &pi3usb9201_emul_cfg_##n, &pi3usb9201_emul_api)
DT_INST_FOREACH_STATUS_OKAY(PI3USB9201_EMUL)
-#define PI3USB9201_EMUL_CASE(n) \
- case DT_INST_DEP_ORD(n): return &pi3usb9201_emul_data_##n.emul;
-
-struct i2c_emul *pi3usb9201_emul_get(int ord)
-{
- switch (ord) {
- DT_INST_FOREACH_STATUS_OKAY(PI3USB9201_EMUL_CASE)
-
- default:
- return NULL;
- }
-}
+DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE);