/* Test that gpio_requestf() works as expected */ static int dm_test_gpio_requestf(struct dm_test_state *dms) { unsigned int offset, gpio; struct udevice *dev; char buf[80]; ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi")); sandbox_gpio_set_direction(dev, offset, 1); sandbox_gpio_set_value(dev, offset, 1); ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf); return 0; }
/* Test that gpio_request() copies its string */ static int dm_test_gpio_copy(struct dm_test_state *dms) { unsigned int offset, gpio; struct udevice *dev; char buf[80], name[10]; ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio)); strcpy(name, "odd_name"); ut_assertok(gpio_request(gpio, name)); sandbox_gpio_set_direction(dev, offset, 1); sandbox_gpio_set_value(dev, offset, 1); ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b6: output: 1 [x] odd_name", buf); strcpy(name, "nothing"); ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b6: output: 1 [x] odd_name", buf); return 0; }
/* Test that sandbox anonymous GPIOs work correctly */ static int dm_test_gpio_anon(struct dm_test_state *dms) { unsigned int offset, gpio; struct udevice *dev; const char *name; int offset_count; /* And the anonymous bank */ ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); ut_asserteq_str(dev->name, "gpio_sandbox"); ut_asserteq(14, offset); ut_asserteq(14, gpio); name = gpio_get_bank_info(dev, &offset_count); ut_asserteq_ptr(NULL, name); ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); return 0; }
/* TODO([email protected]): Remove this function and use device tree */ int sunxi_name_to_gpio(const char *name) { unsigned int gpio; int ret; #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO char lookup[8]; if (strcasecmp(name, "AXP0-VBUS-DETECT") == 0) { sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d", SUNXI_GPIO_AXP0_VBUS_DETECT); name = lookup; } else if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) { sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d", SUNXI_GPIO_AXP0_VBUS_ENABLE); name = lookup; } #endif ret = gpio_lookup_name(name, NULL, NULL, &gpio); return ret ? ret : gpio; }
/* Test if it's possible to access GPIO that should be in pmic */ static int dm_test_spmi_access_peripheral(struct unit_test_state *uts) { struct udevice *dev; unsigned int offset, gpio; const char *name; int offset_count; /* Get second pin of PMIC GPIO */ ut_assertok(gpio_lookup_name("spmi1", &dev, &offset, &gpio)); /* Check if PMIC is parent */ ut_asserteq(device_get_uclass_id(dev->parent), UCLASS_PMIC); /* This should be second gpio */ ut_asserteq(1, offset); name = gpio_get_bank_info(dev, &offset_count); /* Check bank name */ ut_asserteq_str("spmi", name); /* Check pin count */ ut_asserteq(4, offset_count); ut_assertok(gpio_request(gpio, "testing")); /* Try to set/clear gpio */ ut_assertok(gpio_direction_output(gpio, 0)); ut_asserteq(gpio_get_value(gpio), 0); ut_assertok(gpio_direction_output(gpio, 1)); ut_asserteq(gpio_get_value(gpio), 1); ut_assertok(gpio_direction_input(gpio)); ut_asserteq(gpio_get_value(gpio), 1); ut_assertok(gpio_free(gpio)); return 0; }
/* Test that sandbox GPIOs work correctly */ static int dm_test_gpio(struct dm_test_state *dms) { unsigned int offset, gpio; struct dm_gpio_ops *ops; struct udevice *dev; const char *name; int offset_count; char buf[80]; /* * We expect to get 3 banks. One is anonymous (just numbered) and * comes from platdata. The other two are named a (20 gpios) * and b (10 gpios) and come from the device tree. See * test/dm/test.dts. */ ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio)); ut_asserteq_str(dev->name, "extra-gpios"); ut_asserteq(4, offset); ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio); name = gpio_get_bank_info(dev, &offset_count); ut_asserteq_str("b", name); ut_asserteq(10, offset_count); /* Get the operations for this device */ ops = gpio_get_ops(dev); ut_assert(ops->get_state); /* Cannot get a value until it is reserved */ ut_asserteq(-1, ops->get_value(dev, offset)); /* * Now some tests that use the 'sandbox' back door. All GPIOs * should default to input, include b4 that we are using here. */ ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: in: 0 [ ]", buf); /* Change it to an output */ sandbox_gpio_set_direction(dev, offset, 1); ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: out: 0 [ ]", buf); sandbox_gpio_set_value(dev, offset, 1); ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: out: 1 [ ]", buf); ut_assertok(ops->request(dev, offset, "testing")); ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: out: 1 [x] testing", buf); /* Change the value a bit */ ut_asserteq(1, ops->get_value(dev, offset)); ut_assertok(ops->set_value(dev, offset, 0)); ut_asserteq(0, ops->get_value(dev, offset)); ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: out: 0 [x] testing", buf); ut_assertok(ops->set_value(dev, offset, 1)); ut_asserteq(1, ops->get_value(dev, offset)); /* Make it an input */ ut_assertok(ops->direction_input(dev, offset)); ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: in: 1 [x] testing", buf); sandbox_gpio_set_value(dev, offset, 0); ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: in: 0 [x] testing", buf); ut_assertok(ops->free(dev, offset)); ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); ut_asserteq_str("b4: in: 0 [ ]", buf); /* Check the 'a' bank also */ ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio)); ut_asserteq_str(dev->name, "base-gpios"); ut_asserteq(15, offset); ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio); name = gpio_get_bank_info(dev, &offset_count); ut_asserteq_str("a", name); ut_asserteq(20, offset_count); /* And the anonymous bank */ ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); ut_asserteq_str(dev->name, "gpio_sandbox"); ut_asserteq(14, offset); ut_asserteq(14, gpio); name = gpio_get_bank_info(dev, &offset_count); ut_asserteq_ptr(NULL, name); ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); return 0; }
static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { unsigned int gpio; enum gpio_cmd sub_cmd; ulong value; 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 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 (!str_gpio) goto show_usage; /* parse the behavior */ switch (*str_cmd) { case 'i': sub_cmd = GPIO_INPUT; break; case 's': sub_cmd = GPIO_SET; break; case 'c': sub_cmd = GPIO_CLEAR; break; case 't': sub_cmd = GPIO_TOGGLE; break; default: goto show_usage; } #if defined(CONFIG_DM_GPIO) /* * TODO([email protected]): 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 */ ret = gpio_request(gpio, "cmd_gpio"); if (ret && ret != -EBUSY) { printf("gpio: requesting pin %u failed\n", gpio); return -1; } /* finally, let's do it: set direction and exec command */ if (sub_cmd == GPIO_INPUT) { gpio_direction_input(gpio); value = gpio_get_value(gpio); } else { switch (sub_cmd) { case GPIO_SET: value = 1; break; case GPIO_CLEAR: value = 0; break; case GPIO_TOGGLE: value = !gpio_get_value(gpio); break; default: goto show_usage; } gpio_direction_output(gpio, value); } printf("gpio: pin %s (gpio %i) value is %lu\n", str_gpio, gpio, value); if (ret != -EBUSY) gpio_free(gpio); return value; }