summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMulin Chao <mlchao@nuvoton.com>2021-04-07 20:38:00 -0700
committerCommit Bot <commit-bot@chromium.org>2021-04-23 17:09:31 +0000
commitf80d511d1059665c5b55e30c7623508dfb938cac (patch)
tree3f835daeaa1a7291af99afbd174c78bf5d3871b7
parentdb3d48e0025c9b33eca7f3ad5b075d837a202819 (diff)
downloadchrome-ec-f80d511d1059665c5b55e30c7623508dfb938cac.tar.gz
zephyr: Add unused GPIOs implementation in shimmed gpio driver
This CL adds ununed GPIOs configuration support in shimmed gpio driver. By adding IO phandles in 'unused-gpios' prop of board device-tree file, the shimmed driver will configure them one by one (depends on board circuit or io-pad design in chip) to prevent leakage current and get better power consumption in the lowest power state. BUG=b:184783076 BRANCH=none TEST=zmake testall Signed-off-by: Mulin Chao <mlchao@nuvoton.com> Signed-off-by: Wealian Liao <whliao@nuvoton.corp-partner.google.com> Change-Id: Ia4fe29cc244e27855faa1b57c2d1f8319b1246c3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2817141 Reviewed-by: Simon Glass <sjg@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Keith Short <keithshort@chromium.org>
-rw-r--r--zephyr/dts/bindings/gpio/unused-gpios.yaml11
-rw-r--r--zephyr/shim/chip/npcx/CMakeLists.txt1
-rw-r--r--zephyr/shim/chip/npcx/gpio.c55
-rw-r--r--zephyr/shim/include/gpio/gpio.h95
-rw-r--r--zephyr/shim/src/gpio.c11
5 files changed, 173 insertions, 0 deletions
diff --git a/zephyr/dts/bindings/gpio/unused-gpios.yaml b/zephyr/dts/bindings/gpio/unused-gpios.yaml
new file mode 100644
index 0000000000..99f649e25e
--- /dev/null
+++ b/zephyr/dts/bindings/gpio/unused-gpios.yaml
@@ -0,0 +1,11 @@
+description: Unused GPIOs node
+
+compatible: "unused-gpios"
+
+properties:
+ unused-gpios:
+ type: phandle-array
+ required: true
+ description: |
+ A list contains unused GPIOs. The chip vendor needs to configure them for
+ better power consumption in the lowest power state.
diff --git a/zephyr/shim/chip/npcx/CMakeLists.txt b/zephyr/shim/chip/npcx/CMakeLists.txt
index 39a56170d4..86b7a9ae2f 100644
--- a/zephyr/shim/chip/npcx/CMakeLists.txt
+++ b/zephyr/shim/chip/npcx/CMakeLists.txt
@@ -5,6 +5,7 @@
zephyr_library_include_directories(include)
zephyr_library_sources(clock.c)
+zephyr_library_sources(gpio.c)
zephyr_library_sources_ifdef(CONFIG_CROS_KB_RAW_NPCX keyboard_raw.c)
zephyr_library_sources_ifdef(CONFIG_CROS_SHI_NPCX shi.c)
zephyr_library_sources_ifdef(CONFIG_CROS_EC system.c)
diff --git a/zephyr/shim/chip/npcx/gpio.c b/zephyr/shim/chip/npcx/gpio.c
new file mode 100644
index 0000000000..148e1a97c9
--- /dev/null
+++ b/zephyr/shim/chip/npcx/gpio.c
@@ -0,0 +1,55 @@
+/* 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 <device.h>
+#include <kernel.h>
+
+#include <logging/log.h>
+
+#include "gpio.h"
+#include "gpio/gpio.h"
+
+LOG_MODULE_REGISTER(shim_cros_gpio, LOG_LEVEL_ERR);
+
+static const struct unused_pin_config unused_pin_configs[] = {
+ UNUSED_GPIO_CONFIG_LIST
+};
+
+int gpio_config_unused_pins(void)
+{
+ for (size_t i = 0; i < ARRAY_SIZE(unused_pin_configs); ++i) {
+ int rv;
+ int flags;
+ const struct device *dev =
+ device_get_binding(unused_pin_configs[i].dev_name);
+
+ if (dev == NULL) {
+ LOG_ERR("Not found (%s)",
+ unused_pin_configs[i].dev_name);
+ return -ENOTSUP;
+ }
+
+ /*
+ * Set the default setting for the floating IOs. The floating
+ * IOs cause the leakage current. Set unused pins as input with
+ * internal PU to prevent extra power consumption.
+ */
+ if (unused_pin_configs[i].flags == 0)
+ flags = GPIO_INPUT | GPIO_PULL_UP;
+ else
+ flags = unused_pin_configs[i].flags;
+
+ rv = gpio_pin_configure(dev, unused_pin_configs[i].pin, flags);
+
+ if (rv < 0) {
+ LOG_ERR("Config failed %s-%d (%d)",
+ unused_pin_configs[i].dev_name,
+ unused_pin_configs[i].pin, rv);
+ return rv;
+ }
+ }
+
+ return 0;
+}
diff --git a/zephyr/shim/include/gpio/gpio.h b/zephyr/shim/include/gpio/gpio.h
new file mode 100644
index 0000000000..18089e8a8e
--- /dev/null
+++ b/zephyr/shim/include/gpio/gpio.h
@@ -0,0 +1,95 @@
+/* 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.
+ */
+
+#ifndef ZEPHYR_SHIM_INCLUDE_GPIO_GPIO_H_
+#define ZEPHYR_SHIM_INCLUDE_GPIO_GPIO_H_
+
+#include <device.h>
+#include <devicetree.h>
+
+/* Information about each unused pin in the 'unused-pins' device tree node. */
+struct unused_pin_config {
+ /* Device name of a unused gpio pin */
+ const char *dev_name;
+ /* Bit number of pin within a unused gpio pin */
+ gpio_pin_t pin;
+ /* Config flags of unused gpio pin */
+ gpio_flags_t flags;
+};
+
+/**
+ * @brief Set proper configuration for all unused pins.
+ *
+ * This function loops through all unused GPIOs in the node of "unused-gpios"
+ * in the device tree file to set proper configuration. If the GPIO flag is 0,
+ * set the GPIOs default setting for floating IOs to improve the power
+ * consumption.
+ *
+ * @return 0 If successful.
+ * @retval -ENOTSUP Not supported gpio device.
+ * @retval -EIO I/O error when accessing an external GPIO chip.
+ */
+int gpio_config_unused_pins(void) __attribute__((weak));
+
+#if DT_NODE_EXISTS(DT_PATH(unused_pins))
+/**
+ * @brief Get a node from path '/unused-pins' which has a prop 'unused-gpios'.
+ * It contains unused GPIOs and chip vendor needs to configure them for
+ * better power consumption in the lowest power state.
+ *
+ * @return node identifier with that path.
+ */
+#define UNUSED_PINS_LIST DT_PATH(unused_pins)
+
+/**
+ * @brief Length of 'unused-gpios' property
+ *
+ * @return length of 'unused-gpios' prop which type is 'phandle-array'
+ */
+#define UNUSED_GPIOS_LIST_LEN DT_PROP_LEN(UNUSED_PINS_LIST, unused_gpios)
+
+/**
+ * @brief Construct a unused_pin_config structure from 'unused-gpios' property
+ * at index 'i'
+ *
+ * @param i index of 'unused-gpios' prop which type is 'phandles-array'
+ * @return unused_pin_config item at index 'i'
+ */
+#define UNUSED_GPIO_CONFIG_BY_IDX(i, _) \
+ { \
+ .dev_name = DT_GPIO_LABEL_BY_IDX(UNUSED_PINS_LIST, \
+ unused_gpios, i), \
+ .pin = DT_GPIO_PIN_BY_IDX(UNUSED_PINS_LIST, unused_gpios, i), \
+ .flags = DT_GPIO_FLAGS_BY_IDX(UNUSED_PINS_LIST, unused_gpios, \
+ i), \
+ },
+
+/**
+ * @brief Macro function to construct a list of unused_pin_config items by
+ * UTIL_LISTIFY func.
+ *
+ * Example devicetree fragment:
+ * / {
+ * unused-pins {
+ * compatible = "unused-gpios";
+ * unused-gpios = <&gpio5 1 0>,
+ * <&gpiod 0 0>,
+ * <&gpiof 3 0>;
+ * };
+ *
+ * Example usage:
+ * static const struct unused_pin_config unused_pin_configs[] = {
+ * UNUSED_GPIO_CONFIG_LIST
+ * };
+ *
+ * @return a list of unused_pin_config items
+ */
+#define UNUSED_GPIO_CONFIG_LIST \
+ UTIL_LISTIFY(UNUSED_GPIOS_LIST_LEN, UNUSED_GPIO_CONFIG_BY_IDX, _)
+
+#else
+#define UNUSED_GPIO_CONFIG_LIST /* Nothing if no 'unused-pins' node */
+#endif /* unused_pins */
+#endif /* ZEPHYR_SHIM_INCLUDE_GPIO_GPIO_H_ */
diff --git a/zephyr/shim/src/gpio.c b/zephyr/shim/src/gpio.c
index 30a074b9fd..a3e1dfbc1e 100644
--- a/zephyr/shim/src/gpio.c
+++ b/zephyr/shim/src/gpio.c
@@ -9,6 +9,7 @@
#include <logging/log.h>
#include "gpio.h"
+#include "gpio/gpio.h"
LOG_MODULE_REGISTER(gpio_shim, LOG_LEVEL_ERR);
@@ -283,6 +284,16 @@ static int init_gpios(const struct device *unused)
}
}
+ /* Configure unused pins in chip driver for better power consumption */
+ if (gpio_config_unused_pins) {
+ int rv;
+
+ rv = gpio_config_unused_pins();
+ if (rv < 0) {
+ return rv;
+ }
+ }
+
return 0;
}
#if CONFIG_PLATFORM_EC_GPIO_INIT_PRIORITY <= CONFIG_KERNEL_INIT_PRIORITY_DEFAULT