summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/gpio_id.c
diff options
context:
space:
mode:
authorMichał Barnaś <mb@semihalf.com>2021-08-06 14:16:34 +0200
committerCommit Bot <commit-bot@chromium.org>2021-08-18 01:33:10 +0000
commitdac94834ba1fe8a60ec6e6b0f3884782dd95a270 (patch)
tree67e734e4917024406fa7a72f222e2bab4a4771e0 /zephyr/shim/src/gpio_id.c
parent1f723e545eafada64da2ce0bd9b5b9e5d183d5aa (diff)
downloadchrome-ec-dac94834ba1fe8a60ec6e6b0f3884782dd95a270.tar.gz
zephyr: provide unified method to get sku id and board version
Previously, functions for reading board version and sku id were defined in board.c files which are not compiled in Zephyr builds. Logic from board.c files should be moved to the DeviceTree files. This commit adds support for defining board version and sku id pins and numeral system used to decode them. BRANCH=main BUG=b:194136536 TEST=Call system_get_sku_id and system_get_board_version on CrOS EC and Zephyr, values should be correct and the same on both versions Change-Id: I61b5e205cb2a2299ad86c5dff38c05a9659eb2d3 Signed-off-by: Michał Barnaś <mb@semihalf.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3048102 Reviewed-by: Wai-Hong Tam <waihong@google.com> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Wai-Hong Tam <waihong@google.com> Tested-by: Wai-Hong Tam <waihong@google.com>
Diffstat (limited to 'zephyr/shim/src/gpio_id.c')
-rw-r--r--zephyr/shim/src/gpio_id.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/zephyr/shim/src/gpio_id.c b/zephyr/shim/src/gpio_id.c
new file mode 100644
index 0000000000..f07ea8bd22
--- /dev/null
+++ b/zephyr/shim/src/gpio_id.c
@@ -0,0 +1,75 @@
+/* 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 <devicetree.h>
+#include "gpio.h"
+#include "util.h"
+
+#define IS_BOARD_COMPATIBLE \
+ DT_NODE_HAS_COMPAT(DT_PATH(board), cros_ec_gpio_id)
+#define IS_SKU_COMPATIBLE \
+ DT_NODE_HAS_COMPAT(DT_PATH(sku), cros_ec_gpio_id)
+
+#define CONVERT_NUMERAL_SYSTEM_EVAL(system, bits, nbits) \
+ system##_from_bits(bits, nbits)
+#define CONVERT_NUMERAL_SYSTEM(system, bits, nbits) \
+ CONVERT_NUMERAL_SYSTEM_EVAL(system, bits, nbits)
+
+#define READ_PIN_FROM_PHANDLE(node_id, prop, idx) \
+ gpio_get_ternary(GPIO_SIGNAL(DT_PHANDLE_BY_IDX(node_id, prop, idx))),
+
+#if DT_NODE_EXISTS(DT_PATH(sku)) && IS_SKU_COMPATIBLE
+
+__override uint32_t board_get_sku_id(void)
+{
+ static uint32_t sku_id = (uint32_t)-1;
+
+ if (sku_id == (uint32_t)-1) {
+ int bits[] = {
+ DT_FOREACH_PROP_ELEM(DT_PATH(sku),
+ bits,
+ READ_PIN_FROM_PHANDLE)
+ };
+
+ if (sizeof(bits) == 0)
+ return (uint32_t)-1;
+
+ sku_id = CONVERT_NUMERAL_SYSTEM(
+ DT_ENUM_TOKEN(DT_PATH(sku), system),
+ bits,
+ ARRAY_SIZE(bits));
+ }
+
+ return sku_id;
+}
+
+#endif
+
+#if DT_NODE_EXISTS(DT_PATH(board)) && IS_BOARD_COMPATIBLE
+
+__override int board_get_version(void)
+{
+ static int board_version = -1;
+
+ if (board_version == -1) {
+ int bits[] = {
+ DT_FOREACH_PROP_ELEM(DT_PATH(board),
+ bits,
+ READ_PIN_FROM_PHANDLE)
+ };
+
+ if (sizeof(bits) == 0)
+ return -1;
+
+ board_version = CONVERT_NUMERAL_SYSTEM(
+ DT_ENUM_TOKEN(DT_PATH(board), system),
+ bits,
+ ARRAY_SIZE(bits));
+ }
+
+ return board_version;
+}
+
+#endif