summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabio Baltieri <fabiobaltieri@google.com>2022-12-15 12:30:24 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-12-19 10:51:48 +0000
commitf0aafaa71d024630dbf3528cb79563baa8933a45 (patch)
treeb3048f0b5111fd79a6ef3aced30cd0e54219d253
parent8d4c7c9afe575ff8bdffb170a6802aec134e1350 (diff)
downloadchrome-ec-f0aafaa71d024630dbf3528cb79563baa8933a45.tar.gz
zephyr: emul: add a voltage comparator mock driver
Add a voltage comparator mock driver, this is meant to initially be used to test the adc power sequencing code. BRANCH=none BUG=none TEST=cq dry run TEST=./twister Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com> Change-Id: I3e9d9099d265b265486d1c9b9d60e618bb44ff31 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4111306 Reviewed-by: Al Semjonovs <asemjonovs@google.com> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--zephyr/dts/bindings/emul/cros,vcmp-mock.yaml17
-rw-r--r--zephyr/emul/CMakeLists.txt1
-rw-r--r--zephyr/emul/Kconfig8
-rw-r--r--zephyr/emul/include/vcmp_mock.h18
-rw-r--r--zephyr/emul/vcmp_mock.c122
5 files changed, 166 insertions, 0 deletions
diff --git a/zephyr/dts/bindings/emul/cros,vcmp-mock.yaml b/zephyr/dts/bindings/emul/cros,vcmp-mock.yaml
new file mode 100644
index 0000000000..1a3f14c875
--- /dev/null
+++ b/zephyr/dts/bindings/emul/cros,vcmp-mock.yaml
@@ -0,0 +1,17 @@
+# Copyright Google LLC
+# SPDX-License-Identifier: Apache-2.0
+
+description: Generic voltage comparator device mock implementation
+
+compatible: "cros,vcmp-mock"
+
+include: sensor-device.yaml
+
+properties:
+ io-channels:
+ type: phandle-array
+ required: true
+
+ threshold-mv:
+ type: int
+ required: true
diff --git a/zephyr/emul/CMakeLists.txt b/zephyr/emul/CMakeLists.txt
index 10a7be345a..6404fad247 100644
--- a/zephyr/emul/CMakeLists.txt
+++ b/zephyr/emul/CMakeLists.txt
@@ -29,3 +29,4 @@ zephyr_library_sources_ifdef(CONFIG_EMUL_TCS3400 emul_tcs3400.c)
zephyr_library_sources_ifdef(CONFIG_EMUL_TUSB1064 emul_tusb1064.c)
zephyr_library_sources_ifdef(CONFIG_I2C_MOCK i2c_mock.c)
zephyr_library_sources_ifdef(CONFIG_PWM_MOCK pwm_mock.c)
+zephyr_library_sources_ifdef(CONFIG_VCMP_MOCK vcmp_mock.c)
diff --git a/zephyr/emul/Kconfig b/zephyr/emul/Kconfig
index 44d53179c3..cae2972858 100644
--- a/zephyr/emul/Kconfig
+++ b/zephyr/emul/Kconfig
@@ -132,6 +132,14 @@ config PWM_MOCK
Enable the PWM mock. This driver is a pure mock and does nothing by
default.
+config VCMP_MOCK
+ bool "Generic voltage comparator device mock implementation"
+ default y
+ depends on DT_HAS_CROS_VCMP_MOCK_ENABLED
+ select ADC
+ help
+ Enable the generic voltage comparator mock driver.
+
config EMUL_PCT2075
bool "PCT2075 emulator"
default y
diff --git a/zephyr/emul/include/vcmp_mock.h b/zephyr/emul/include/vcmp_mock.h
new file mode 100644
index 0000000000..5444b0aeab
--- /dev/null
+++ b/zephyr/emul/include/vcmp_mock.h
@@ -0,0 +1,18 @@
+/* Copyright 2022 Google LLC
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef __EMUL_INCLUDE_VCMP_MOCK_H
+#define __EMUL_INCLUDE_VCMP_MOCK_H
+
+#include <zephyr/device.h>
+
+/*
+ * Manually trigger the vcmp handler
+ *
+ * @param dev pointer to the vcmp emulator device
+ */
+void vcmp_mock_trigger(const struct device *dev);
+
+#endif /*__EMUL_INCLUDE_VCMP_MOCK_H */
diff --git a/zephyr/emul/vcmp_mock.c b/zephyr/emul/vcmp_mock.c
new file mode 100644
index 0000000000..9dffdd606b
--- /dev/null
+++ b/zephyr/emul/vcmp_mock.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2022 Google LLC
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#define DT_DRV_COMPAT cros_vcmp_mock
+
+#include "vcmp_mock.h"
+
+#include <zephyr/device.h>
+#include <zephyr/devicetree.h>
+#include <zephyr/drivers/sensor.h>
+#include <zephyr/logging/log.h>
+
+LOG_MODULE_REGISTER(SENSOR_GENERIC_EMUL, CONFIG_SENSOR_LOG_LEVEL);
+
+struct vcmp_mock_data {
+ sensor_trigger_handler_t handler;
+ const struct sensor_trigger *trigger;
+ bool alert_enabled;
+};
+
+void vcmp_mock_trigger(const struct device *dev)
+{
+ struct vcmp_mock_data *data = dev->data;
+
+ if (data->handler) {
+ data->handler(dev, data->trigger);
+ }
+}
+
+static int vcmp_mock_attr_set(const struct device *dev,
+ enum sensor_channel chan,
+ enum sensor_attribute attr,
+ const struct sensor_value *val)
+{
+ struct vcmp_mock_data *data = dev->data;
+
+ if (chan != SENSOR_CHAN_VOLTAGE) {
+ return -ENOTSUP;
+ }
+
+ switch (attr) {
+ case SENSOR_ATTR_ALERT:
+ data->alert_enabled = val->val1;
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ return 0;
+}
+
+static int vcmp_mock_attr_get(const struct device *dev,
+ enum sensor_channel chan,
+ enum sensor_attribute attr,
+ struct sensor_value *val)
+{
+ struct vcmp_mock_data *data = dev->data;
+
+ if (chan != SENSOR_CHAN_VOLTAGE) {
+ return -ENOTSUP;
+ }
+
+ switch (attr) {
+ case SENSOR_ATTR_ALERT:
+ val->val1 = data->alert_enabled;
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ return 0;
+}
+
+static int vcmp_mock_trigger_set(const struct device *dev,
+ const struct sensor_trigger *trig,
+ sensor_trigger_handler_t handler)
+{
+ struct vcmp_mock_data *data = dev->data;
+
+ data->handler = handler;
+ data->trigger = trig;
+
+ return -ENOTSUP;
+}
+
+static int vcmp_mock_sample_fetch(const struct device *dev,
+ enum sensor_channel chan)
+{
+ return -ENOTSUP;
+}
+
+static int vcmp_mock_channel_get(const struct device *dev,
+ enum sensor_channel chan,
+ struct sensor_value *val)
+{
+ return -ENOTSUP;
+}
+
+static const struct sensor_driver_api vcmp_mock_driver_api = {
+ .attr_set = vcmp_mock_attr_set,
+ .channel_get = vcmp_mock_channel_get,
+ .sample_fetch = vcmp_mock_sample_fetch,
+ .attr_get = vcmp_mock_attr_get,
+ .trigger_set = vcmp_mock_trigger_set,
+};
+
+int vcmp_mock_init(const struct device *dev)
+{
+ return 0;
+}
+
+#define VCMP_MOCK_INST(inst) \
+ static struct vcmp_mock_data vcmp_mock_data_##inst; \
+ SENSOR_DEVICE_DT_INST_DEFINE(inst, vcmp_mock_init, NULL, \
+ &vcmp_mock_data_##inst, NULL, \
+ POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
+ &vcmp_mock_driver_api);
+
+DT_INST_FOREACH_STATUS_OKAY(VCMP_MOCK_INST)