* Licensed under the GPL-2 or later.
*/
-#include <common.h>
#include <command.h>
#include <errno.h>
#include <dm.h>
+#include <log.h>
#include <malloc.h>
+#ifdef CONFIG_CMD_GPIO_READ
+#include <env.h>
+#endif
#include <asm/gpio.h>
#include <linux/err.h>
+#include <dm/device_compat.h>
__weak int name_to_gpio(const char *name)
{
- return simple_strtoul(name, NULL, 10);
+ return dectoul(name, NULL);
}
enum gpio_cmd {
GPIOC_SET,
GPIOC_CLEAR,
GPIOC_TOGGLE,
+#ifdef CONFIG_CMD_GPIO_READ
+ GPIOC_READ,
+#endif
};
#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
printf("%s\n", buf);
return;
err:
- printf("Error %d\n", ret);
+ if (ret != -ENOENT)
+ 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;
+ int ret, err = 0;
flags = 0;
if (gpio_name && !*gpio_name)
gpio_name = NULL;
- for (ret = uclass_first_device(UCLASS_GPIO, &dev);
+ for (ret = uclass_first_device_check(UCLASS_GPIO, &dev);
dev;
- ret = uclass_next_device(&dev)) {
+ ret = uclass_next_device_check(&dev)) {
const char *bank_name;
int num_bits;
+ if (ret) {
+ printf("GPIO device %s probe error %i\n",
+ dev->name, ret);
+ err = ret;
+ continue;
+ }
+
flags |= FLAG_SHOW_BANK;
if (all)
flags |= FLAG_SHOW_ALL;
p = gpio_name + banklen;
if (gpio_name && *p) {
- offset = simple_strtoul(p, NULL, 10);
+ offset = dectoul(p, NULL);
gpio_get_description(dev, bank_name, offset,
&flags, true);
} else {
flags |= FLAG_SHOW_NEWLINE;
}
- return ret;
+ return err;
}
#endif
enum gpio_cmd sub_cmd;
int value;
const char *str_cmd, *str_gpio = NULL;
+#ifdef CONFIG_CMD_GPIO_READ
+ const char *str_var = NULL;
+#endif
int ret;
#ifdef CONFIG_DM_GPIO
bool all = false;
argc -= 2;
argv += 2;
#ifdef CONFIG_DM_GPIO
- if (argc > 0 && !strcmp(*argv, "-a")) {
+ if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
all = true;
argc--;
argv++;
}
+#endif
+#ifdef CONFIG_CMD_GPIO_READ
+ if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
+ if (argc < 2)
+ goto show_usage;
+ str_var = *argv;
+ argc--;
+ argv++;
+ }
#endif
if (argc > 0)
str_gpio = *argv;
case 't':
sub_cmd = GPIOC_TOGGLE;
break;
+#ifdef CONFIG_CMD_GPIO_READ
+ case 'r':
+ sub_cmd = GPIOC_READ;
+ break;
+#endif
default:
goto show_usage;
}
}
/* finally, let's do it: set direction and exec command */
- if (sub_cmd == GPIOC_INPUT) {
+ if (sub_cmd == GPIOC_INPUT
+#ifdef CONFIG_CMD_GPIO_READ
+ || sub_cmd == GPIOC_READ
+#endif
+ ) {
gpio_direction_input(gpio);
value = gpio_get_value(gpio);
} else {
goto err;
} else {
printf("%d\n", value);
+#ifdef CONFIG_CMD_GPIO_READ
+ if (sub_cmd == GPIOC_READ)
+ env_set_ulong(str_var, (ulong)value);
+#endif
}
- if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
+ if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
+#ifdef CONFIG_CMD_GPIO_READ
+ && sub_cmd != GPIOC_READ
+#endif
+ ) {
int nval = gpio_get_value(gpio);
if (IS_ERR_VALUE(nval)) {
"query and control gpio pins",
"<input|set|clear|toggle> <pin>\n"
" - input/set/clear/toggle the specified pin\n"
+#ifdef CONFIG_CMD_GPIO_READ
+ "gpio read <name> <pin>\n"
+ " - set environment variable 'name' to the specified pin\n"
+#endif
"gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");