diff options
author | Michał Barnaś <mb@semihalf.com> | 2021-10-14 19:15:29 +0200 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-11-05 11:45:58 +0000 |
commit | f4fd96d6bdfae2fcffd2e395b5d87e64bd6c9f2d (patch) | |
tree | aa2f6453e3935718794d6e25cdc6e434915c17fb | |
parent | 1c93a65042bd50027a67f812c38c1556363b1aa1 (diff) | |
download | chrome-ec-f4fd96d6bdfae2fcffd2e395b5d87e64bd6c9f2d.tar.gz |
ioex: extract shell commands from ioexpander.c
This commit extracts shell commands to manipulate ioex to different
file to split generic ioex logic from shell commands.
BRANCH=main
BUG=b:202701452
TEST=After flashing, ioexget and ioexset should work correctly
Change-Id: I8707d1dd32324ca6e7b9163b800e32ded67fdf38
Signed-off-by: Michał Barnaś <mb@semihalf.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3262093
Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
-rw-r--r-- | common/build.mk | 2 | ||||
-rw-r--r-- | common/ioexpander.c | 154 | ||||
-rw-r--r-- | common/ioexpander_commands.c | 145 | ||||
-rw-r--r-- | include/ioexpander.h | 10 |
4 files changed, 168 insertions, 143 deletions
diff --git a/common/build.mk b/common/build.mk index 67217be550..3405a34a2b 100644 --- a/common/build.mk +++ b/common/build.mk @@ -68,7 +68,7 @@ common-$(CONFIG_CHARGER)+=charger.o charge_state_v2.o common-$(CONFIG_CHARGER_PROFILE_OVERRIDE_COMMON)+=charger_profile_override.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_IO_EXPANDER)+=ioexpander.o ioexpander_commands.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 index 06ca94b8bc..6caa7f4bc1 100644 --- a/common/ioexpander.c +++ b/common/ioexpander.c @@ -5,7 +5,6 @@ /* IO Expander Controller Common Code */ -#include "console.h" #include "gpio.h" #include "hooks.h" #include "ioexpander.h" @@ -15,25 +14,6 @@ #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(enum ioex_signal signal, int v) -{ - const int i = signal - IOEX_SIGNAL_START; - - ASSERT(signal_is_ioex(signal)); - - 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 signal_is_ioex(int signal) { return ((signal >= IOEX_SIGNAL_START) && (signal < IOEX_SIGNAL_END)); @@ -108,6 +88,18 @@ int ioex_disable_interrupt(enum ioex_signal signal) return drv->enable_interrupt(g->ioex, g->port, g->mask, 0); } +int ioex_get_ioex_flags(enum ioex_signal signal, int *val) +{ + const struct ioex_info *g = ioex_get_signal_info(signal); + + if (g == NULL) + return EC_ERROR_BUSY; + + *val = ioex_config[g->ioex].flags; + + return EC_SUCCESS; +} + int ioex_get_flags(enum ioex_signal signal, int *flags) { const struct ioex_info *g = ioex_get_signal_info(signal); @@ -222,125 +214,3 @@ const char *ioex_get_name(enum ioex_signal signal) return g->name; } - -static void print_ioex_info(enum ioex_signal signal) -{ - int changed, v, val; - int flags = 0; - const struct ioex_info *g = ioex_list + signal - IOEX_SIGNAL_START; - - if (!(ioex_config[g->ioex].flags & IOEX_FLAGS_INITIALIZED)) { - ccprintf(" DISABLED %s\n", ioex_get_name(signal)); - return; - } - - - v = ioex_get_level(signal, &val); - if (v) { - ccprintf("Fail to get %s level\n", ioex_get_name(signal)); - return; - } - v = ioex_get_flags(signal, &flags); - if (v) { - ccprintf("Fail to get %s flags\n", ioex_get_name(signal)); - return; - } - - changed = last_val_changed(signal, 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(signal)); - - /* Flush console to avoid truncating output */ - cflush(); -} - -static int ioex_get_default_flags(enum ioex_signal signal) -{ - const struct ioex_info *g = ioex_get_signal_info(signal); - - if (g == NULL) - return 0; - - return g->flags; -} - -/* IO expander commands */ -static enum ioex_signal find_ioex_by_name(const char *name) -{ - enum ioex_signal signal; - - if (!name) - return IOEX_SIGNAL_END; - - for (signal = IOEX_SIGNAL_START; signal < IOEX_SIGNAL_END; signal++) { - if (!strcasecmp(name, ioex_get_name(signal))) - return signal; - } - - return IOEX_SIGNAL_END; -} - -static enum ec_error_list ioex_set(const char *name, int value) -{ - enum ioex_signal signal = find_ioex_by_name(name); - - if (!signal_is_ioex(signal)) - 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) -{ - enum ioex_signal signal; - - /* If a signal is specified, print only that one */ - if (argc == 2) { - signal = find_ioex_by_name(argv[1]); - if (!signal_is_ioex(signal)) - return EC_ERROR_PARAM1; - print_ioex_info(signal); - - return EC_SUCCESS; - } - - /* Otherwise print them all */ - for (signal = IOEX_SIGNAL_START; signal < IOEX_SIGNAL_END; signal++) - print_ioex_info(signal); - - return EC_SUCCESS; -} -DECLARE_SAFE_CONSOLE_COMMAND(ioexget, command_ioex_get, - "[name]", - "Read level of IO expander pin(s)"); diff --git a/common/ioexpander_commands.c b/common/ioexpander_commands.c new file mode 100644 index 0000000000..a09337ea88 --- /dev/null +++ b/common/ioexpander_commands.c @@ -0,0 +1,145 @@ +/* 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 "console.h" +#include "gpio.h" +#include "ioexpander.h" +#include "util.h" + +static uint8_t last_val[(IOEX_COUNT + 7) / 8]; + +static int last_val_changed(enum ioex_signal signal, int v) +{ + const int i = signal - IOEX_SIGNAL_START; + + ASSERT(signal_is_ioex(signal)); + + 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; + } +} + +static enum ioex_signal find_ioex_by_name(const char *name) +{ + enum ioex_signal signal; + + if (!name) + return IOEX_SIGNAL_END; + + for (signal = IOEX_SIGNAL_START; signal < IOEX_SIGNAL_END; signal++) { + if (!strcasecmp(name, ioex_get_name(signal))) + return signal; + } + + return IOEX_SIGNAL_END; +} + +static void print_ioex_info(enum ioex_signal signal) +{ + int changed, v, val; + int flags = 0; + + if (ioex_get_ioex_flags(signal, &flags)) { + ccprintf(" ERROR getting flags\n"); + return; + } + + if (!(flags & IOEX_FLAGS_INITIALIZED)) { + ccprintf(" DISABLED %s\n", ioex_get_name(signal)); + return; + } + + v = ioex_get_level(signal, &val); + if (v) { + ccprintf("Fail to get %s level\n", ioex_get_name(signal)); + return; + } + v = ioex_get_flags(signal, &flags); + if (v) { + ccprintf("Fail to get %s flags\n", ioex_get_name(signal)); + return; + } + + changed = last_val_changed(signal, 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(signal)); + + /* Flush console to avoid truncating output */ + cflush(); +} + +static enum ec_error_list ioex_set(const char *name, int value) +{ + enum ioex_signal signal = find_ioex_by_name(name); + int flags; + + if (!signal_is_ioex(signal)) + return EC_ERROR_INVAL; + + if (ioex_get_flags(signal, &flags)) + return EC_ERROR_INVAL; + + if (!(flags & 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 pin"); + +static int command_ioex_get(int argc, char **argv) +{ + enum ioex_signal signal; + + /* If a signal is specified, print only that one */ + if (argc == 2) { + signal = find_ioex_by_name(argv[1]); + if (!signal_is_ioex(signal)) + return EC_ERROR_PARAM1; + print_ioex_info(signal); + + return EC_SUCCESS; + } + + /* Otherwise print them all */ + for (signal = IOEX_SIGNAL_START; signal < IOEX_SIGNAL_END; signal++) + print_ioex_info(signal); + + return EC_SUCCESS; +} +DECLARE_SAFE_CONSOLE_COMMAND(ioexget, command_ioex_get, + "[name]", + "Read level of IO expander pin(s)"); diff --git a/include/ioexpander.h b/include/ioexpander.h index fba1f331fe..4024a5a267 100644 --- a/include/ioexpander.h +++ b/include/ioexpander.h @@ -98,6 +98,16 @@ int ioex_enable_interrupt(enum ioex_signal signal); int ioex_disable_interrupt(enum ioex_signal signal); /* + * Get io expander flags (IOEX_FLAGS_*) for chip that specified IOEX signal + * belongs to. They contain information if port was disabled or initialized. + * + * @param signal IOEX signal that belongs to chip which flags will be returned + * @param val Pointer to memory where flags will be stored + * @return EC_SUCCESS if successful, non-zero if error. + */ +int ioex_get_ioex_flags(enum ioex_signal signal, int *val); + +/* * Get flags for the IOEX signal * * @param signal IOEX signal to get flags for |