gpio.c 8.3 KB

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