summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/adc.h18
-rw-r--r--zephyr/CMakeLists.txt1
-rw-r--r--zephyr/Kconfig1
-rw-r--r--zephyr/Kconfig.adc41
-rw-r--r--zephyr/dts/bindings/adc/named-adc.yaml21
-rw-r--r--zephyr/shim/include/adc_chip.h16
-rw-r--r--zephyr/shim/include/config_chip.h10
-rw-r--r--zephyr/shim/src/CMakeLists.txt1
-rw-r--r--zephyr/shim/src/adc.c48
9 files changed, 154 insertions, 3 deletions
diff --git a/include/adc.h b/include/adc.h
index b4b4c8cb43..0ed3146635 100644
--- a/include/adc.h
+++ b/include/adc.h
@@ -13,11 +13,23 @@
#define ADC_READ_ERROR -1 /* Value returned by adc_read_channel() on error */
#ifdef CONFIG_ZEPHYR
-/* TODO(b/175881324): Add a shim for ADC */
+#ifdef CONFIG_PLATFORM_EC_ADC
+#define NODE_ID_AND_COMMA(node_id) node_id,
enum adc_channel {
- ADC_NONE,
+#if DT_NODE_EXISTS(DT_INST(0, named_adc_channels))
+ DT_FOREACH_CHILD(DT_INST(0, named_adc_channels), NODE_ID_AND_COMMA)
+#endif /* named_adc_channels */
+ ADC_CH_COUNT
};
-#endif
+
+struct adc_t {
+ const char *name;
+ uint8_t input_ch;
+};
+
+extern const struct adc_t adc_channels[];
+#endif /* CONFIG_PLATFORM_EC_ADC */
+#endif /* CONFIG_ZEPHYR */
/*
* Boards which use the ADC interface must provide enum adc_channel in the
diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt
index bbf493c5cf..4134f54bda 100644
--- a/zephyr/CMakeLists.txt
+++ b/zephyr/CMakeLists.txt
@@ -86,6 +86,7 @@ zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ACCELGYRO_BMI260
"${PLATFORM_EC}/common/math_util.c")
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ACCEL_FIFO
"${PLATFORM_EC}/common/motion_sense_fifo.c")
+zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ADC_CMD "${PLATFORM_EC}/common/adc.c")
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ALS_TCS3400
"${PLATFORM_EC}/driver/als_tcs3400.c")
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ACPI "${PLATFORM_EC}/common/acpi.c"
diff --git a/zephyr/Kconfig b/zephyr/Kconfig
index 3f7a0cf317..910e952887 100644
--- a/zephyr/Kconfig
+++ b/zephyr/Kconfig
@@ -31,6 +31,7 @@ menuconfig PLATFORM_EC
if PLATFORM_EC
+rsource "Kconfig.adc"
rsource "Kconfig.battery"
rsource "Kconfig.header"
rsource "Kconfig.keyboard"
diff --git a/zephyr/Kconfig.adc b/zephyr/Kconfig.adc
new file mode 100644
index 0000000000..f32da5169e
--- /dev/null
+++ b/zephyr/Kconfig.adc
@@ -0,0 +1,41 @@
+# 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.
+
+menuconfig PLATFORM_EC_ADC
+ bool "ADC shim"
+ default n if ARCH_POSIX
+ default y
+ help
+ Enable compilation of the EC ADC module. Once enabled, it is
+ possible to call platform/ec adc_read_channel() function.
+
+if PLATFORM_EC_ADC
+
+config PLATFORM_EC_ADC_CMD
+ bool "ADC host/console command"
+ default y
+ help
+ Enables support for printing ADC channels state with the "adc"
+ console command and reading a state of ADC channel with the
+ EC_CMD_ADC_READ host command. Replaces generic Zephyr "adc"
+ command.
+
+config PLATFORM_EC_ADC_RESOLUTION
+ int "ADC resolution"
+ default 10
+ help
+ The resolution, in bits, to use for the ADC conversion. Determines
+ the sample values range: 0 .. 2^resolution -1. The supported
+ resolution values depend on specific hardware.
+
+config PLATFORM_EC_ADC_OVERSAMPLING
+ int "ADC oversampling"
+ default 0
+ help
+ ADC oversampling to use for the ADC conversion. Each sample is
+ averaged from 2^oversampling conversion results. Oversampling can
+ help in providing more stable readings. The supported oversampling
+ values depend on specific hardware.
+
+endif # PLATFORM_EC_ADC
diff --git a/zephyr/dts/bindings/adc/named-adc.yaml b/zephyr/dts/bindings/adc/named-adc.yaml
new file mode 100644
index 0000000000..8773e34dde
--- /dev/null
+++ b/zephyr/dts/bindings/adc/named-adc.yaml
@@ -0,0 +1,21 @@
+# 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.
+
+description: ADC parent node
+
+compatible: "named-adc-channels"
+
+child-binding:
+ description: Named ADCs child node
+ properties:
+ label:
+ required: true
+ type: string
+ description:
+ Human-readable string describing the device (used as
+ device_get_binding() argument)
+ channel:
+ required: true
+ type: int
+ description: ADC channel used
diff --git a/zephyr/shim/include/adc_chip.h b/zephyr/shim/include/adc_chip.h
new file mode 100644
index 0000000000..c51cdfbb30
--- /dev/null
+++ b/zephyr/shim/include/adc_chip.h
@@ -0,0 +1,16 @@
+/* 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.
+ */
+
+/*
+ * This file is left intentionally blank. It is required since the
+ * platform/ec/common/adc.c file includes it. Normally, this file
+ * would define chip specific ADC configs and would reside under
+ * platform/ec/chip/...
+ */
+
+#ifndef __CROS_EC_ADC_CHIP_H
+#define __CROS_EC_ADC_CHIP_H
+
+#endif /* __CROS_EC_ADC_CHIP_H */
diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h
index 1c3e09707f..bdf258ef81 100644
--- a/zephyr/shim/include/config_chip.h
+++ b/zephyr/shim/include/config_chip.h
@@ -257,6 +257,16 @@ enum battery_type {
#endif /* CONFIG_PLATFORM_EC_FLASH */
+#undef CONFIG_ADC
+#ifdef CONFIG_PLATFORM_EC_ADC
+#define CONFIG_ADC
+#endif
+
+#undef CONFIG_CMD_ADC
+#ifdef CONFIG_PLATFORM_EC_ADC_CMD
+#define CONFIG_CMD_ADC
+#endif
+
#ifdef CONFIG_PLATFORM_EC_I2C
/* Also see shim/include/i2c/i2c.h which defines the ports enum */
#define CONFIG_I2C
diff --git a/zephyr/shim/src/CMakeLists.txt b/zephyr/shim/src/CMakeLists.txt
index 1820104d71..4d3ce6bace 100644
--- a/zephyr/shim/src/CMakeLists.txt
+++ b/zephyr/shim/src/CMakeLists.txt
@@ -15,6 +15,7 @@ else()
endif()
zephyr_sources_ifdef(no_libgcc libgcc_${ARCH}.S)
+zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ADC adc.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ESPI espi.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_FLASH flash.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_HOOKS hooks.c)
diff --git a/zephyr/shim/src/adc.c b/zephyr/shim/src/adc.c
new file mode 100644
index 0000000000..6f5a7d58de
--- /dev/null
+++ b/zephyr/shim/src/adc.c
@@ -0,0 +1,48 @@
+/* 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 "adc.h"
+#include <drivers/adc.h>
+
+#define ADC_DEV DT_LABEL(DT_NODELABEL(adc0))
+const struct device *adc_dev;
+
+#if DT_NODE_EXISTS(DT_INST(0, named_adc_channels))
+#define ADC_CHANNEL_COMMA(node_id) \
+ [node_id] = { \
+ .name = DT_LABEL(node_id), \
+ .input_ch = DT_PROP(node_id, channel), \
+ },
+const struct adc_t adc_channels[] = {
+ DT_FOREACH_CHILD(DT_INST(0, named_adc_channels), ADC_CHANNEL_COMMA)
+ };
+#endif /* named_adc_channels */
+
+static int init_device_bindings(const struct device *device)
+{
+ ARG_UNUSED(device);
+ adc_dev = device_get_binding(ADC_DEV);
+ return 0;
+}
+SYS_INIT(init_device_bindings, POST_KERNEL, 51);
+
+int adc_read_channel(enum adc_channel ch)
+{
+ int ret = 0;
+ struct adc_sequence seq = {
+ .options = NULL,
+ .channels = BIT(adc_channels[ch].input_ch),
+ .buffer = &ret,
+ .buffer_size = sizeof(ret),
+ .resolution = CONFIG_PLATFORM_EC_ADC_RESOLUTION,
+ .oversampling = CONFIG_PLATFORM_EC_ADC_OVERSAMPLING,
+ .calibrate = false,
+ };
+
+ adc_read(adc_dev, &seq);
+ adc_raw_to_millivolts(adc_ref_internal(adc_dev), ADC_GAIN_1,
+ CONFIG_PLATFORM_EC_ADC_RESOLUTION, &ret);
+ return ret;
+}