gpio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2013 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <fdtdec.h>
  7. #include <dm.h>
  8. #include <dm/root.h>
  9. #include <dm/test.h>
  10. #include <dm/util.h>
  11. #include <asm/gpio.h>
  12. #include <test/ut.h>
  13. /* Test that sandbox GPIOs work correctly */
  14. static int dm_test_gpio(struct unit_test_state *uts)
  15. {
  16. unsigned int offset, gpio;
  17. struct dm_gpio_ops *ops;
  18. struct udevice *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_function);
  38. /* Cannot get a value until it is reserved */
  39. ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
  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(gpio_get_status(dev, offset, buf, sizeof(buf)));
  45. ut_asserteq_str("b4: input: 0 [ ]", buf);
  46. /* Change it to an output */
  47. sandbox_gpio_set_direction(dev, offset, 1);
  48. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  49. ut_asserteq_str("b4: output: 0 [ ]", buf);
  50. sandbox_gpio_set_value(dev, offset, 1);
  51. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  52. ut_asserteq_str("b4: output: 1 [ ]", buf);
  53. ut_assertok(gpio_request(gpio, "testing"));
  54. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  55. ut_asserteq_str("b4: output: 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(gpio_get_status(dev, offset, buf, sizeof(buf)));
  61. ut_asserteq_str("b4: output: 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 open drain output, and reset it */
  65. ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
  66. ut_assertok(ops->set_open_drain(dev, offset, 1));
  67. ut_asserteq(1, sandbox_gpio_get_open_drain(dev, offset));
  68. ut_assertok(ops->set_open_drain(dev, offset, 0));
  69. ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
  70. /* Make it an input */
  71. ut_assertok(ops->direction_input(dev, offset));
  72. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  73. ut_asserteq_str("b4: input: 1 [x] testing", buf);
  74. sandbox_gpio_set_value(dev, offset, 0);
  75. ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
  76. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  77. ut_asserteq_str("b4: input: 0 [x] testing", buf);
  78. ut_assertok(gpio_free(gpio));
  79. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  80. ut_asserteq_str("b4: input: 0 [ ]", buf);
  81. /* Check the 'a' bank also */
  82. ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
  83. ut_asserteq_str(dev->name, "base-gpios");
  84. ut_asserteq(15, offset);
  85. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
  86. name = gpio_get_bank_info(dev, &offset_count);
  87. ut_asserteq_str("a", name);
  88. ut_asserteq(20, offset_count);
  89. return 0;
  90. }
  91. DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  92. /* Test that sandbox anonymous GPIOs work correctly */
  93. static int dm_test_gpio_anon(struct unit_test_state *uts)
  94. {
  95. unsigned int offset, gpio;
  96. struct udevice *dev;
  97. const char *name;
  98. int offset_count;
  99. /* And the anonymous bank */
  100. ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
  101. ut_asserteq_str(dev->name, "gpio_sandbox");
  102. ut_asserteq(14, offset);
  103. ut_asserteq(14, gpio);
  104. name = gpio_get_bank_info(dev, &offset_count);
  105. ut_asserteq_ptr(NULL, name);
  106. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
  107. return 0;
  108. }
  109. DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  110. /* Test that gpio_requestf() works as expected */
  111. static int dm_test_gpio_requestf(struct unit_test_state *uts)
  112. {
  113. unsigned int offset, gpio;
  114. struct udevice *dev;
  115. char buf[80];
  116. ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
  117. ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
  118. sandbox_gpio_set_direction(dev, offset, 1);
  119. sandbox_gpio_set_value(dev, offset, 1);
  120. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  121. ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
  122. return 0;
  123. }
  124. DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  125. /* Test that gpio_request() copies its string */
  126. static int dm_test_gpio_copy(struct unit_test_state *uts)
  127. {
  128. unsigned int offset, gpio;
  129. struct udevice *dev;
  130. char buf[80], name[10];
  131. ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
  132. strcpy(name, "odd_name");
  133. ut_assertok(gpio_request(gpio, name));
  134. sandbox_gpio_set_direction(dev, offset, 1);
  135. sandbox_gpio_set_value(dev, offset, 1);
  136. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  137. ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
  138. strcpy(name, "nothing");
  139. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  140. ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
  141. return 0;
  142. }
  143. DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  144. /* Test that we don't leak memory with GPIOs */
  145. static int dm_test_gpio_leak(struct unit_test_state *uts)
  146. {
  147. ut_assertok(dm_test_gpio(uts));
  148. ut_assertok(dm_test_gpio_anon(uts));
  149. ut_assertok(dm_test_gpio_requestf(uts));
  150. ut_assertok(dm_leak_check_end(uts));
  151. return 0;
  152. }
  153. DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  154. /* Test that we can find GPIOs using phandles */
  155. static int dm_test_gpio_phandles(struct unit_test_state *uts)
  156. {
  157. struct gpio_desc desc, desc_list[8], desc_list2[8];
  158. struct udevice *dev, *gpio_a, *gpio_b;
  159. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
  160. ut_asserteq_str("a-test", dev->name);
  161. ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
  162. ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
  163. ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
  164. ut_asserteq_str("base-gpios", gpio_a->name);
  165. ut_asserteq(true, !!device_active(gpio_a));
  166. ut_asserteq_ptr(gpio_a, desc.dev);
  167. ut_asserteq(4, desc.offset);
  168. /* GPIOF_INPUT is the sandbox GPIO driver default */
  169. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
  170. ut_assertok(dm_gpio_free(dev, &desc));
  171. ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
  172. 0));
  173. ut_asserteq_ptr(NULL, desc.dev);
  174. ut_asserteq(desc.offset, 0);
  175. ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
  176. 0));
  177. /* Last GPIO is ignord as it comes after <0> */
  178. ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
  179. ARRAY_SIZE(desc_list), 0));
  180. ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
  181. desc_list2,
  182. ARRAY_SIZE(desc_list2),
  183. 0));
  184. ut_assertok(gpio_free_list(dev, desc_list, 3));
  185. ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
  186. ARRAY_SIZE(desc_list),
  187. GPIOD_IS_OUT |
  188. GPIOD_IS_OUT_ACTIVE));
  189. ut_asserteq_ptr(gpio_a, desc_list[0].dev);
  190. ut_asserteq(1, desc_list[0].offset);
  191. ut_asserteq_ptr(gpio_a, desc_list[1].dev);
  192. ut_asserteq(4, desc_list[1].offset);
  193. ut_asserteq_ptr(gpio_b, desc_list[2].dev);
  194. ut_asserteq(5, desc_list[2].offset);
  195. ut_asserteq(1, dm_gpio_get_value(desc_list));
  196. ut_assertok(gpio_free_list(dev, desc_list, 3));
  197. ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
  198. ARRAY_SIZE(desc_list), 0));
  199. /* This was set to output previously, so still will be */
  200. ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 1, NULL));
  201. /* Active low should invert the input value */
  202. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
  203. ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
  204. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
  205. ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
  206. ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
  207. ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
  208. ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
  209. return 0;
  210. }
  211. DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);