summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Kiryanov <nikita@compulab.co.il>2016-04-16 17:55:11 +0300
committerTom Rini <trini@konsulko.com>2016-05-20 17:02:09 -0400
commitc40f03723fba2f77bccd92dd87fcd48c37d13cdc (patch)
tree819007ffd5a820911bb96df0d18b7d53e326be8b
parent2d9a76b614c33e660b5556b5f96bd5ee4810953e (diff)
downloadu-boot-imx-c40f03723fba2f77bccd92dd87fcd48c37d13cdc.zip
u-boot-imx-c40f03723fba2f77bccd92dd87fcd48c37d13cdc.tar.gz
u-boot-imx-c40f03723fba2f77bccd92dd87fcd48c37d13cdc.tar.bz2
eeprom: refactor i2c bus and devaddr parsing
Introduce parse_i2c_bus_addr() to generalize the parsing of i2c bus number and i2c device address. This is done in preparation for merging layout aware and layout unaware command parsing into one function. No functional changes. Cc: Heiko Schocher <hs@denx.de> Cc: Marek Vasut <marex@denx.de> Cc: Simon Glass <sjg@chromium.org> Cc: Igor Grinberg <grinberg@compulab.co.il> Cc: Tom Rini <trini@konsulko.com> Signed-off-by: Nikita Kiryanov <nikita@compulab.co.il>
-rw-r--r--cmd/eeprom.c79
1 files changed, 53 insertions, 26 deletions
diff --git a/cmd/eeprom.c b/cmd/eeprom.c
index 39ebee8..54bf06c 100644
--- a/cmd/eeprom.c
+++ b/cmd/eeprom.c
@@ -207,6 +207,56 @@ int eeprom_write(unsigned dev_addr, unsigned offset,
return ret;
}
+static int parse_numeric_param(char *str)
+{
+ char *endptr;
+ int value = simple_strtol(str, &endptr, 16);
+
+ return (*endptr != '\0') ? -1 : value;
+}
+
+/**
+ * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
+ *
+ * @i2c_bus: address to store the i2c bus
+ * @i2c_addr: address to store the device i2c address
+ * @argc: count of command line arguments left to parse
+ * @argv: command line arguments left to parse
+ * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
+ *
+ * @returns: number of arguments parsed or CMD_RET_USAGE if error
+ */
+static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
+ char * const argv[], int argc_no_bus_addr)
+{
+ int argc_no_bus = argc_no_bus_addr + 1;
+ int argc_bus_addr = argc_no_bus_addr + 2;
+
+#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
+ if (argc == argc_no_bus_addr) {
+ *i2c_bus = -1;
+ *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
+
+ return 0;
+ }
+#endif
+ if (argc == argc_no_bus) {
+ *i2c_bus = -1;
+ *i2c_addr = parse_numeric_param(argv[0]);
+
+ return 1;
+ }
+
+ if (argc == argc_bus_addr) {
+ *i2c_bus = parse_numeric_param(argv[0]);
+ *i2c_addr = parse_numeric_param(argv[1]);
+
+ return 2;
+ }
+
+ return CMD_RET_USAGE;
+}
+
#ifdef CONFIG_CMD_EEPROM_LAYOUT
#include <eeprom_layout.h>
@@ -237,14 +287,6 @@ static enum eeprom_action parse_action(char *cmd)
return EEPROM_ACTION_INVALID;
}
-static int parse_numeric_param(char *str)
-{
- char *endptr;
- int value = simple_strtol(str, &endptr, 16);
-
- return (*endptr != '\0') ? -1 : value;
-}
-
static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
int i2c_addr, int layout_ver, char *key,
char *value)
@@ -348,24 +390,9 @@ static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
}
#endif
- switch (argc) {
-#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
- case 5:
- bus_addr = -1;
- dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
- break;
-#endif
- case 6:
- bus_addr = -1;
- dev_addr = simple_strtoul(*args++, NULL, 16);
- break;
- case 7:
- bus_addr = simple_strtoul(*args++, NULL, 16);
- dev_addr = simple_strtoul(*args++, NULL, 16);
- break;
- default:
- return CMD_RET_USAGE;
- }
+ rcode = parse_i2c_bus_addr(&bus_addr, &dev_addr, argc - 2, argv + 2, 3);
+ if (rcode == CMD_RET_USAGE)
+ return rcode;
addr = simple_strtoul(*args++, NULL, 16);
off = simple_strtoul(*args++, NULL, 16);