gpio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <fdtdec.h>
  8. #include <dm.h>
  9. #include <dm/ut.h>
  10. #include <dm/test.h>
  11. #include <dm/util.h>
  12. #include <asm/gpio.h>
  13. /* Test that sandbox GPIOs work correctly */
  14. static int dm_test_gpio(struct dm_test_state *dms)
  15. {
  16. unsigned int offset, gpio;
  17. struct dm_gpio_ops *ops;
  18. struct device *dev;
  19. const char *name;
  20. int offset_count;
  21. char buf[80];
  22. /*
  23. * We expect to get 3 banks. One is anonymous (just numbered) and
  24. * comes from platdata. The other two are named a (20 gpios)
  25. * and b (10 gpios) and come from the device tree. See
  26. * test/dm/test.dts.
  27. */
  28. ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
  29. ut_asserteq_str(dev->name, "extra-gpios");
  30. ut_asserteq(4, offset);
  31. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio);
  32. name = gpio_get_bank_info(dev, &offset_count);
  33. ut_asserteq_str("b", name);
  34. ut_asserteq(10, offset_count);
  35. /* Get the operations for this device */
  36. ops = gpio_get_ops(dev);
  37. ut_assert(ops->get_state);
  38. /* Cannot get a value until it is reserved */
  39. ut_asserteq(-1, ops->get_value(dev, offset));
  40. /*
  41. * Now some tests that use the 'sandbox' back door. All GPIOs
  42. * should default to input, include b4 that we are using here.
  43. */
  44. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  45. ut_asserteq_str("b4: in: 0 [ ]", buf);
  46. /* Change it to an output */
  47. sandbox_gpio_set_direction(dev, offset, 1);
  48. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  49. ut_asserteq_str("b4: out: 0 [ ]", buf);
  50. sandbox_gpio_set_value(dev, offset, 1);
  51. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  52. ut_asserteq_str("b4: out: 1 [ ]", buf);
  53. ut_assertok(ops->request(dev, offset, "testing"));
  54. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  55. ut_asserteq_str("b4: out: 1 [x] testing", buf);
  56. /* Change the value a bit */
  57. ut_asserteq(1, ops->get_value(dev, offset));
  58. ut_assertok(ops->set_value(dev, offset, 0));
  59. ut_asserteq(0, ops->get_value(dev, offset));
  60. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  61. ut_asserteq_str("b4: out: 0 [x] testing", buf);
  62. ut_assertok(ops->set_value(dev, offset, 1));
  63. ut_asserteq(1, ops->get_value(dev, offset));
  64. /* Make it an input */
  65. ut_assertok(ops->direction_input(dev, offset));
  66. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  67. ut_asserteq_str("b4: in: 1 [x] testing", buf);
  68. sandbox_gpio_set_value(dev, offset, 0);
  69. ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
  70. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  71. ut_asserteq_str("b4: in: 0 [x] testing", buf);
  72. ut_assertok(ops->free(dev, offset));
  73. ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
  74. ut_asserteq_str("b4: in: 0 [ ]", buf);
  75. /* Check the 'a' bank also */
  76. ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
  77. ut_asserteq_str(dev->name, "base-gpios");
  78. ut_asserteq(15, offset);
  79. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
  80. name = gpio_get_bank_info(dev, &offset_count);
  81. ut_asserteq_str("a", name);
  82. ut_asserteq(20, offset_count);
  83. /* And the anonymous bank */
  84. ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
  85. ut_asserteq_str(dev->name, "gpio_sandbox");
  86. ut_asserteq(14, offset);
  87. ut_asserteq(14, gpio);
  88. name = gpio_get_bank_info(dev, &offset_count);
  89. ut_asserteq_ptr(NULL, name);
  90. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
  91. return 0;
  92. }
  93. DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);