summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCHLin <CHLIN56@nuvoton.com>2019-06-13 11:01:18 +0800
committerCommit Bot <commit-bot@chromium.org>2019-07-24 05:50:26 +0000
commit26d7adb2fd72635cde56199b025c200a125dcb78 (patch)
tree0b6c70777fc40dea00884ada9ccfebaf9b8692ca
parentb872f26ce6b97f62175c835871fbb7ead6b8455b (diff)
downloadchrome-ec-26d7adb2fd72635cde56199b025c200a125dcb78.tar.gz
IO_Expander: introduce the common interface of IO expander
Implement the common code to provide a friendly interface to control the IOs of IO expander. It adopts a similar concept to GPIO. 1. Define the IO expander IO in gpio.inc by the format: IOEX(name, EXPIN(ioex, port, offset), flags) - name: the name of this IO pin - EXPIN(ioex, port, offset) - ioex: the IO expander port (defined in board.c) this IO pin belongs to. - port: the port number in the IO expander chip. - offset: the bit offset in the port above. - flags: the same as the flags of GPIO. 2. The following APIs are supported: 1. ioex_get_flags_by_mask 2. ioex_set_flags_by_mask 3. ioex_get_flags 4. ioex_set_flags 5. ioex_get_level 6. ioex_set_level 7. ioex_init 3. The following console commands are supported: 1. ioexget [IO_EXPANDER_PIN_NAME] 2. ioexset IO_EXPANDER_PIN_NAME 0/1 BRANCH=none BUG=none TEST=No error for "make buildall" TEST=Apply this and related CLs, manually test each API, make sure each function works correctly with IO expander chip (NCT3807/NCT3808.) Change-Id: I79c9813abccc67d5554e2ceb5c119dcf549b7dce Signed-off-by: CHLin <CHLIN56@nuvoton.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1657858 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Aseda Aboagye <aaboagye@chromium.org> Commit-Queue: CH Lin <chlin56@nuvoton.com> Commit-Queue: Aseda Aboagye <aaboagye@chromium.org> Tested-by: CH Lin <chlin56@nuvoton.com>
-rw-r--r--common/build.mk1
-rw-r--r--common/ioexpander.c221
-rw-r--r--include/config.h19
-rw-r--r--include/gpio.wrap6
-rw-r--r--include/gpio_list.h36
-rw-r--r--include/gpio_signal.h7
-rw-r--r--include/ioexpander.h137
7 files changed, 427 insertions, 0 deletions
diff --git a/common/build.mk b/common/build.mk
index a7b93361a3..41c2a2b213 100644
--- a/common/build.mk
+++ b/common/build.mk
@@ -47,6 +47,7 @@ common-$(CONFIG_CHARGER_PROFILE_OVERRIDE_COMMON)+=charger_profile_override.o
common-$(CONFIG_CHARGER_V2)+=charge_state_v2.o
common-$(CONFIG_CMD_I2CWEDGE)+=i2c_wedge.o
common-$(CONFIG_COMMON_GPIO)+=gpio.o gpio_commands.o
+common-$(CONFIG_IO_EXPANDER)+=ioexpander.o
common-$(CONFIG_COMMON_PANIC_OUTPUT)+=panic_output.o
common-$(CONFIG_COMMON_RUNTIME)+=hooks.o main.o system.o peripheral.o
common-$(CONFIG_COMMON_TIMER)+=timer.o
diff --git a/common/ioexpander.c b/common/ioexpander.c
new file mode 100644
index 0000000000..8e00f9337a
--- /dev/null
+++ b/common/ioexpander.c
@@ -0,0 +1,221 @@
+/* Copyright 2019 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.
+ */
+
+/* IO Expander Controller Common Code */
+
+#include "console.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "ioexpander.h"
+#include "util.h"
+
+#define CPRINTF(format, args...) cprintf(CC_GPIO, format, ## args)
+#define CPRINTS(format, args...) cprints(CC_GPIO, format, ## args)
+
+static uint8_t last_val[(IOEX_COUNT + 7) / 8];
+
+static int last_val_changed(int i, int v)
+{
+ if (v && !(last_val[i / 8] & (BIT(i % 8)))) {
+ last_val[i / 8] |= BIT(i % 8);
+ return 1;
+ } else if (!v && last_val[i / 8] & (BIT(i % 8))) {
+ last_val[i / 8] &= ~(BIT(i % 8));
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+int ioex_get_flags_by_mask(int ioex, int port, int mask, int *flags)
+{
+ return ioex_config[ioex].drv->get_flags_by_mask(ioex, port, mask,
+ flags);
+}
+
+int ioex_set_flags_by_mask(int ioex, int port, int mask, int flags)
+{
+ return ioex_config[ioex].drv->set_flags_by_mask(ioex, port, mask,
+ flags);
+}
+
+int ioex_get_flags(enum ioex_signal signal, int *flags)
+{
+ const struct ioex_info *g = ioex_list + signal;
+
+ return ioex_config[g->ioex].drv->get_flags_by_mask(g->ioex,
+ g->port, g->mask, flags);
+}
+
+int ioex_set_flags(enum ioex_signal signal, int flags)
+{
+ const struct ioex_info *g = ioex_list + signal;
+
+ return ioex_config[g->ioex].drv->set_flags_by_mask(g->ioex,
+ g->port, g->mask, flags);
+}
+
+int ioex_get_level(enum ioex_signal signal, int *val)
+{
+ const struct ioex_info *g = ioex_list + signal;
+
+ return ioex_config[g->ioex].drv->get_level(g->ioex, g->port,
+ g->mask, val);
+}
+
+int ioex_set_level(enum ioex_signal signal, int value)
+{
+ const struct ioex_info *g = ioex_list + signal;
+
+ return ioex_config[g->ioex].drv->set_level(g->ioex, g->port,
+ g->mask, value);
+}
+
+int ioex_init(int ioex)
+{
+ const struct ioexpander_drv *drv = ioex_config[ioex].drv;
+
+ if (drv->init == NULL)
+ return EC_SUCCESS;
+
+ return drv->init(ioex);
+}
+
+static void ioex_init_default(void)
+{
+ const struct ioex_info *g = ioex_list;
+ int i;
+
+ for (i = 0; i < CONFIG_IO_EXPANDER_PORT_COUNT; i++)
+ ioex_init(i);
+ /*
+ * Set all IO expander GPIOs to default flags according to the setting
+ * in gpio.inc
+ */
+ for (i = 0; i < IOEX_COUNT; i++, g++) {
+ if (g->mask && !(g->flags & GPIO_DEFAULT)) {
+ ioex_set_flags_by_mask(g->ioex, g->port,
+ g->mask, g->flags);
+ }
+ }
+
+}
+DECLARE_HOOK(HOOK_INIT, ioex_init_default, HOOK_PRIO_INIT_I2C + 1);
+
+const char *ioex_get_name(enum ioex_signal signal)
+{
+ return ioex_list[signal].name;
+}
+
+static void print_ioex_info(int io)
+{
+ int changed, v, val;
+ int flags = 0;
+
+ v = ioex_get_level(io, &val);
+ if (v) {
+ ccprintf("Fail to get %s level\n", ioex_get_name(io));
+ return;
+ }
+ v = ioex_get_flags(io, &flags);
+ if (v) {
+ ccprintf("Fail to get %s flags\n", ioex_get_name(io));
+ return;
+ }
+
+ changed = last_val_changed(io, val);
+
+ ccprintf(" %d%c %s%s%s%s%s%s\n", val,
+ (changed ? '*' : ' '),
+ (flags & GPIO_INPUT ? "I " : ""),
+ (flags & GPIO_OUTPUT ? "O " : ""),
+ (flags & GPIO_LOW ? "L " : ""),
+ (flags & GPIO_HIGH ? "H " : ""),
+ (flags & GPIO_OPEN_DRAIN ? "ODR " : ""),
+ ioex_get_name(io));
+
+ /* Flush console to avoid truncating output */
+ cflush();
+}
+
+int ioex_get_default_flags(enum ioex_signal signal)
+{
+ return ioex_list[signal].flags;
+}
+
+/* IO expander commands */
+static enum ioex_signal find_ioex_by_name(const char *name)
+{
+ int i;
+
+ if (!name)
+ return IOEX_COUNT;
+
+ for (i = 0; i < IOEX_COUNT; i++) {
+ if (!strcasecmp(name, ioex_get_name(i)))
+ return i;
+ }
+
+ return IOEX_COUNT;
+}
+
+static enum ec_error_list ioex_set(const char *name, int value)
+{
+ enum ioex_signal signal = find_ioex_by_name(name);
+
+ if (signal == IOEX_COUNT)
+ return EC_ERROR_INVAL;
+
+ if (!(ioex_get_default_flags(signal) & GPIO_OUTPUT))
+ return EC_ERROR_INVAL;
+
+ return ioex_set_level(signal, value);
+}
+
+static int command_ioex_set(int argc, char **argv)
+{
+ char *e;
+ int v;
+
+ if (argc < 3)
+ return EC_ERROR_PARAM_COUNT;
+
+ v = strtoi(argv[2], &e, 0);
+ if (*e)
+ return EC_ERROR_PARAM2;
+
+ if (ioex_set(argv[1], v) != EC_SUCCESS)
+ return EC_ERROR_PARAM1;
+
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(ioexset, command_ioex_set,
+ "name <0 | 1>",
+ "Set level of a IO expander IO");
+
+static int command_ioex_get(int argc, char **argv)
+{
+ int i;
+
+ /* If a signal is specified, print only that one */
+ if (argc == 2) {
+ i = find_ioex_by_name(argv[1]);
+ if (i == IOEX_COUNT)
+ return EC_ERROR_PARAM1;
+ print_ioex_info(i);
+
+ return EC_SUCCESS;
+ }
+
+ /* Otherwise print them all */
+ for (i = 0; i < IOEX_COUNT; i++)
+ print_ioex_info(i);
+
+ return EC_SUCCESS;
+}
+DECLARE_SAFE_CONSOLE_COMMAND(ioexget, command_ioex_get,
+ "[name]",
+ "Read level of IO expander pin(s)");
+
diff --git a/include/config.h b/include/config.h
index d1a4dfc8d0..1eae83dc1a 100644
--- a/include/config.h
+++ b/include/config.h
@@ -1260,6 +1260,22 @@
#undef CONFIG_COMMON_GPIO_SHORTNAMES
/*
+ * Control the IO pins of IO expander via IO Expander APIs
+ *
+ * If defined, declare the IOEX pin with macro IOEX. For example:
+ * IOEX(IO_NAME, EXPIN(0, 0, 0), GPIO_OUT_HIGH)
+ * For more details, see gpio_list.h.
+ *
+ * WARNING: make sure none of IOEX IOs are accessed at interrupt level / with
+ * interrupts disabled. Doing so may hang the EC because IO expanders may rely
+ * on I2C interrupts.
+ *
+ * Some reasons that not unify the GPIO and IOEX APIs have been disscussed and
+ * filed in the crbug.com/985540.
+ */
+#undef CONFIG_IO_EXPANDER
+
+/*
* EC's supporting powering down GPIO pins.
* Add flag GPIO_POWER_DOWN and additional API's.
*/
@@ -2259,6 +2275,9 @@
/* Support NXP PCA9534 I/O expander. */
#undef CONFIG_IO_EXPANDER_PCA9534
+/* Number of IO Expander ports */
+#undef CONFIG_IO_EXPANDER_PORT_COUNT
+
/*****************************************************************************/
/* Number of IRQs supported on the EC chip */
diff --git a/include/gpio.wrap b/include/gpio.wrap
index e2bdabda7b..1e018f2ed2 100644
--- a/include/gpio.wrap
+++ b/include/gpio.wrap
@@ -114,6 +114,10 @@
#define UNIMPLEMENTED_RW(name) UNIMPLEMENTED(name)
#endif
+#ifndef IOEX
+#define IOEX(name, expin, flags)
+#endif
+
#include "gpio.inc"
/*
@@ -131,3 +135,5 @@
#undef UNIMPLEMENTED
#undef UNIMPLEMENTED_RO
#undef UNIMPLEMENTED_RW
+
+#undef IOEX
diff --git a/include/gpio_list.h b/include/gpio_list.h
index 70d7bfc4b2..4fed0d60d6 100644
--- a/include/gpio_list.h
+++ b/include/gpio_list.h
@@ -52,3 +52,39 @@ const int gpio_ih_count = ARRAY_SIZE(gpio_irq_handlers);
#define PIN(a, b...) static const int _pin_ ## a ## _ ## b \
__attribute__((unused, section(".unused"))) = __LINE__;
#include "gpio.wrap"
+
+#include "ioexpander.h"
+/*
+ * Define the IO expander IO in gpio.inc by the format:
+ * IOEX(name, EXPIN(ioex_port, port, offset), flags)
+ * - name: the name of this IO pin
+ * - EXPIN(ioex, port, offset)
+ * - ioex: the IO expander port (defined in board.c) this IO
+ * pin belongs to.
+ * - port: the port number in the IO expander chip.
+ * - offset: the bit offset in the port above.
+ * - flags: the same as the flags of GPIO.
+ *
+ */
+#define IOEX_EXPIN(ioex, port, index) (ioex), (port), BIT(index)
+
+#define IOEX(name, expin, flags) {#name, IOEX_##expin, flags},
+
+/* IO expander signal list. */
+const struct ioex_info ioex_list[] = {
+ #include "gpio.wrap"
+};
+BUILD_ASSERT(ARRAY_SIZE(ioex_list) == IOEX_COUNT);
+
+#define IOEX(name, expin, flags) expin
+
+/* The compiler will complain if we use the same name twice or the controller
+ * number declared is greater or equal to CONFIG_IO_EXPANDER_PORT_COUNT.
+ * The linker ignores anything that gets by.
+ */
+#define EXPIN(a, b, c...) \
+ static const int _expin_ ## a ## _ ## b ## _ ## c \
+ __attribute__((unused, section(".unused"))) = __LINE__; \
+ BUILD_ASSERT(a < CONFIG_IO_EXPANDER_PORT_COUNT);
+
+#include "gpio.wrap"
diff --git a/include/gpio_signal.h b/include/gpio_signal.h
index 776e84fdae..da34f07b33 100644
--- a/include/gpio_signal.h
+++ b/include/gpio_signal.h
@@ -15,4 +15,11 @@ enum gpio_signal {
GPIO_COUNT
};
+#define IOEX(name, expin, flags) IOEX_##name,
+
+enum ioex_signal {
+ #include "gpio.wrap"
+ IOEX_COUNT
+};
+
#endif /* __CROS_EC_GPIO_SIGNAL_H */
diff --git a/include/ioexpander.h b/include/ioexpander.h
new file mode 100644
index 0000000000..2a31eb6aaa
--- /dev/null
+++ b/include/ioexpander.h
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2019 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 __CROS_EC_IOEXPANDER_H
+#define __CROS_EC_IOEXPANDER_H
+
+/* IO expander signal definition structure */
+struct ioex_info {
+ /* Signal name */
+ const char *name;
+
+ /* IO expander port number */
+ uint16_t ioex;
+
+ /* IO port number in IO expander */
+ uint16_t port;
+
+ /* Bitmask on that port (1 << N) */
+ uint32_t mask;
+
+ /* Flags - the same as the GPIO flags */
+ uint32_t flags;
+};
+
+/* Signal information from board.c. Must match order from enum ioex_signal. */
+extern const struct ioex_info ioex_list[];
+
+struct ioexpander_drv {
+ /* Initialize IO expander chip/driver */
+ int (*init)(int ioex);
+ /* Get the current level of the IOEX pin */
+ int (*get_level)(int ioex, int port, int mask, int *val);
+ /* Set the level of the IOEX pin */
+ int (*set_level)(int ioex, int port, int mask, int val);
+ /* Get flags for the IOEX pin */
+ int (*get_flags_by_mask)(int ioex, int port, int mask, int *flags);
+ /* Set flags for the IOEX pin */
+ int (*set_flags_by_mask)(int ioex, int port, int mask, int flags);
+};
+
+struct ioexpander_config_t {
+ /* Physical I2C port connects to the IO expander chip. */
+ int i2c_host_port;
+ /* I2C slave address */
+ int i2c_slave_addr;
+ /*
+ * The extra variable used to store information which may be required
+ * by the IO expander chip.
+ */
+ int chip_info;
+ /*
+ * Pointer to the specific IO expander chip's ops defined in
+ * the struct ioexpander_drv.
+ */
+ const struct ioexpander_drv *drv;
+};
+
+extern struct ioexpander_config_t ioex_config[];
+
+/*
+ * Get flags for the IOEX pin by mask
+ *
+ * @param ioex IO expander chip's port number
+ * @param port IO port in the IO expander chip
+ * @param mask Bitmask of the pin on the port above
+ * @param flags Pointer to the keep the flags read
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int ioex_get_flags_by_mask(int ioex, int port, int mask, int *flags);
+
+/*
+ * Set flags for the IOEX pin by mask
+ *
+ * @param ioex IO expander chip's port number
+ * @param port IO port in the IO expander chip
+ * @param mask Bitmask of the pin on the port above
+ * @param flags flags to set
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int ioex_set_flags_by_mask(int ioex, int port, int mask, int flags);
+
+/*
+ * Get flags for the IOEX signal
+ *
+ * @param signal IOEX signal to get flags for
+ * @param flags Pointer to the keep the flags read
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int ioex_get_flags(enum ioex_signal signal, int *flags);
+
+/*
+ * Set flags for the IOEX signal
+ *
+ * @param signal IOEX signal to set flags for
+ * @param flags New flags for the IOEX signal
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int ioex_set_flags(enum ioex_signal signal, int flags);
+
+/*
+ * Get the current level of the IOEX signal
+ *
+ * @param signal IOEX signal to get the level
+ * @param val Pointer to the keep the level read
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int ioex_get_level(enum ioex_signal signal, int *val);
+
+/*
+ * Set the level of the IOEX signal
+ *
+ * @param signal IOEX signal to set the level
+ * @param value New level for the IOEX signal
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int ioex_set_level(enum ioex_signal signal, int value);
+
+/*
+ * Initialize IO expander chip/driver
+ *
+ * @param ioex IO expander chip's port number
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int ioex_init(int ioex);
+
+/*
+ * Get the name for the IOEX signal
+ *
+ * @param signal IOEX signal to get the name
+ * @returns name of the given IOEX signal
+ */
+const char *ioex_get_name(enum ioex_signal signal);
+#endif /* __CROS_EC_IOEXPANDER_H */
+