From f80d511d1059665c5b55e30c7623508dfb938cac Mon Sep 17 00:00:00 2001 From: Mulin Chao Date: Wed, 7 Apr 2021 20:38:00 -0700 Subject: 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 Signed-off-by: Wealian Liao Change-Id: Ia4fe29cc244e27855faa1b57c2d1f8319b1246c3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2817141 Reviewed-by: Simon Glass Reviewed-by: Keith Short Commit-Queue: Keith Short --- zephyr/dts/bindings/gpio/unused-gpios.yaml | 11 ++++ zephyr/shim/chip/npcx/CMakeLists.txt | 1 + zephyr/shim/chip/npcx/gpio.c | 55 +++++++++++++++++ zephyr/shim/include/gpio/gpio.h | 95 ++++++++++++++++++++++++++++++ zephyr/shim/src/gpio.c | 11 ++++ 5 files changed, 173 insertions(+) create mode 100644 zephyr/dts/bindings/gpio/unused-gpios.yaml create mode 100644 zephyr/shim/chip/npcx/gpio.c create mode 100644 zephyr/shim/include/gpio/gpio.h 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 +#include + +#include + +#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 +#include + +/* 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 #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 -- cgit v1.2.1