X-Git-Url: https://git.kernelconcepts.de/?a=blobdiff_plain;f=common%2Fcmd_gpio.c;h=65d6df451c6d4f4c5dc9c26a94a02a536b42386e;hb=46eeb1405961f091aac2c7ccef593075c1e4d137;hp=47eee89221fff34bb704a5fc5b6269f72234da19;hpb=4c12eeb8b55483e48ef98b8a480e2bbacc9f210d;p=karo-tx-uboot.git diff --git a/common/cmd_gpio.c b/common/cmd_gpio.c index 47eee89221..65d6df451c 100644 --- a/common/cmd_gpio.c +++ b/common/cmd_gpio.c @@ -8,12 +8,14 @@ #include #include - +#include +#include #include -#ifndef name_to_gpio -#define name_to_gpio(name) simple_strtoul(name, NULL, 10) -#endif +__weak int name_to_gpio(const char *name) +{ + return simple_strtoul(name, NULL, 10); +} enum gpio_cmd { GPIO_INPUT, @@ -22,25 +24,137 @@ enum gpio_cmd { GPIO_TOGGLE, }; +#if defined(CONFIG_DM_GPIO) && !defined(gpio_status) + +/* A few flags used by show_gpio() */ +enum { + FLAG_SHOW_ALL = 1 << 0, + FLAG_SHOW_BANK = 1 << 1, + FLAG_SHOW_NEWLINE = 1 << 2, +}; + +static void gpio_get_description(struct udevice *dev, const char *bank_name, + int offset, int *flagsp) +{ + char buf[80]; + int ret; + + ret = gpio_get_function(dev, offset, NULL); + if (ret < 0) + goto err; + if (!(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED) + return; + if ((*flagsp & FLAG_SHOW_BANK) && bank_name) { + if (*flagsp & FLAG_SHOW_NEWLINE) { + putc('\n'); + *flagsp &= ~FLAG_SHOW_NEWLINE; + } + printf("Bank %s:\n", bank_name); + *flagsp &= ~FLAG_SHOW_BANK; + } + + ret = gpio_get_status(dev, offset, buf, sizeof(buf)); + if (ret) + goto err; + + printf("%s\n", buf); + return; +err: + printf("Error %d\n", ret); +} + +static int do_gpio_status(bool all, const char *gpio_name) +{ + struct udevice *dev; + int banklen; + int flags; + int ret; + + flags = 0; + if (gpio_name && !*gpio_name) + gpio_name = NULL; + for (ret = uclass_first_device(UCLASS_GPIO, &dev); + dev; + ret = uclass_next_device(&dev)) { + const char *bank_name; + int num_bits; + + flags |= FLAG_SHOW_BANK; + if (all) + flags |= FLAG_SHOW_ALL; + bank_name = gpio_get_bank_info(dev, &num_bits); + if (!num_bits) { + debug("GPIO device %s has no bits\n", dev->name); + continue; + } + banklen = bank_name ? strlen(bank_name) : 0; + + if (!gpio_name || !bank_name || + !strncmp(gpio_name, bank_name, banklen)) { + const char *p = NULL; + int offset; + + p = gpio_name + banklen; + if (gpio_name && *p) { + offset = simple_strtoul(p, NULL, 10); + gpio_get_description(dev, bank_name, offset, + &flags); + } else { + for (offset = 0; offset < num_bits; offset++) { + gpio_get_description(dev, bank_name, + offset, &flags); + } + } + } + /* Add a newline between bank names */ + if (!(flags & FLAG_SHOW_BANK)) + flags |= FLAG_SHOW_NEWLINE; + } + + return ret; +} +#endif + static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { - int gpio; + unsigned int gpio; enum gpio_cmd sub_cmd; ulong value; - const char *str_cmd, *str_gpio; + const char *str_cmd, *str_gpio = NULL; + int ret; +#ifdef CONFIG_DM_GPIO + bool all = false; +#endif + if (argc < 2) + show_usage: + return CMD_RET_USAGE; + str_cmd = argv[1]; + argc -= 2; + argv += 2; +#ifdef CONFIG_DM_GPIO + if (argc > 0 && !strcmp(*argv, "-a")) { + all = true; + argc--; + argv++; + } +#endif + if (argc > 0) + str_gpio = *argv; + if (!strcmp(str_cmd, "status")) { + /* Support deprecated gpio_status() */ #ifdef gpio_status - if (argc == 2 && !strcmp(argv[1], "status")) { gpio_status(); return 0; - } +#elif defined(CONFIG_DM_GPIO) + return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio)); +#else + goto show_usage; #endif + } - if (argc != 3) - show_usage: - return CMD_RET_USAGE; - str_cmd = argv[1]; - str_gpio = argv[2]; + if (!str_gpio) + goto show_usage; /* parse the behavior */ switch (*str_cmd) { @@ -51,13 +165,26 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) default: goto show_usage; } +#if defined(CONFIG_DM_GPIO) + /* + * TODO(sjg@chromium.org): For now we must fit into the existing GPIO + * framework, so we look up the name here and convert it to a GPIO number. + * Once all GPIO drivers are converted to driver model, we can change the + * code here to use the GPIO uclass interface instead of the numbered + * GPIO compatibility layer. + */ + ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio); + if (ret) + return cmd_process_error(cmdtp, ret); +#else /* turn the gpio name into a gpio number */ gpio = name_to_gpio(str_gpio); if (gpio < 0) goto show_usage; - +#endif /* grab the pin before we tweak it */ - if (gpio_request(gpio, "cmd_gpio")) { + ret = gpio_request(gpio, "cmd_gpio"); + if (ret && ret != -EBUSY) { printf("gpio: requesting pin %u failed\n", gpio); return -1; } @@ -78,12 +205,14 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) printf("gpio: pin %s (gpio %i) value is %lu\n", str_gpio, gpio, value); - gpio_free(gpio); + if (ret != -EBUSY) + gpio_free(gpio); return value; } -U_BOOT_CMD(gpio, 3, 0, do_gpio, - "input/set/clear/toggle gpio pins", - " \n" - " - input/set/clear/toggle the specified pin"); +U_BOOT_CMD(gpio, 4, 0, do_gpio, + "query and control gpio pins", + " \n" + " - input/set/clear/toggle the specified pin\n" + "gpio status [-a] [ | ] - show [all/claimed] GPIOs");