gpio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 <log.h>
  9. #include <malloc.h>
  10. #include <acpi/acpi_device.h>
  11. #include <asm/gpio.h>
  12. #include <dm/root.h>
  13. #include <dm/test.h>
  14. #include <dm/util.h>
  15. #include <test/test.h>
  16. #include <test/ut.h>
  17. /* Test that sandbox GPIOs work correctly */
  18. static int dm_test_gpio(struct unit_test_state *uts)
  19. {
  20. unsigned int offset, gpio;
  21. struct dm_gpio_ops *ops;
  22. struct udevice *dev;
  23. struct gpio_desc *desc;
  24. const char *name;
  25. int offset_count;
  26. char buf[80];
  27. /*
  28. * We expect to get 4 banks. One is anonymous (just numbered) and
  29. * comes from platdata. The other are named a (20 gpios),
  30. * b (10 gpios) and c (10 gpios) and come from the device tree. See
  31. * test/dm/test.dts.
  32. */
  33. ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
  34. ut_asserteq_str(dev->name, "extra-gpios");
  35. ut_asserteq(4, offset);
  36. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio);
  37. name = gpio_get_bank_info(dev, &offset_count);
  38. ut_asserteq_str("b", name);
  39. ut_asserteq(10, offset_count);
  40. /* Get the operations for this device */
  41. ops = gpio_get_ops(dev);
  42. ut_assert(ops->get_function);
  43. /* Cannot get a value until it is reserved */
  44. ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
  45. /*
  46. * Now some tests that use the 'sandbox' back door. All GPIOs
  47. * should default to input, include b4 that we are using here.
  48. */
  49. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  50. ut_asserteq_str("b4: input: 0 [ ]", buf);
  51. /* Change it to an output */
  52. sandbox_gpio_set_direction(dev, offset, 1);
  53. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  54. ut_asserteq_str("b4: output: 0 [ ]", buf);
  55. sandbox_gpio_set_value(dev, offset, 1);
  56. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  57. ut_asserteq_str("b4: output: 1 [ ]", buf);
  58. ut_assertok(gpio_request(gpio, "testing"));
  59. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  60. ut_asserteq_str("b4: output: 1 [x] testing", buf);
  61. /* Change the value a bit */
  62. ut_asserteq(1, ops->get_value(dev, offset));
  63. ut_assertok(ops->set_value(dev, offset, 0));
  64. ut_asserteq(0, ops->get_value(dev, offset));
  65. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  66. ut_asserteq_str("b4: output: 0 [x] testing", buf);
  67. ut_assertok(ops->set_value(dev, offset, 1));
  68. ut_asserteq(1, ops->get_value(dev, offset));
  69. /* Make it an open drain output, and reset it */
  70. ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
  71. sandbox_gpio_get_dir_flags(dev, offset));
  72. ut_assertok(ops->set_dir_flags(dev, offset,
  73. GPIOD_IS_OUT | GPIOD_OPEN_DRAIN));
  74. ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
  75. sandbox_gpio_get_dir_flags(dev, offset));
  76. ut_assertok(ops->set_dir_flags(dev, offset,
  77. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE));
  78. ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
  79. sandbox_gpio_get_dir_flags(dev, offset));
  80. /* Make it an input */
  81. ut_assertok(ops->direction_input(dev, offset));
  82. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  83. ut_asserteq_str("b4: input: 1 [x] testing", buf);
  84. sandbox_gpio_set_value(dev, offset, 0);
  85. ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
  86. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  87. ut_asserteq_str("b4: input: 0 [x] testing", buf);
  88. ut_assertok(gpio_free(gpio));
  89. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  90. ut_asserteq_str("b4: input: 0 [ ]", buf);
  91. /* Check the 'a' bank also */
  92. ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
  93. ut_asserteq_str(dev->name, "base-gpios");
  94. ut_asserteq(15, offset);
  95. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
  96. name = gpio_get_bank_info(dev, &offset_count);
  97. ut_asserteq_str("a", name);
  98. ut_asserteq(20, offset_count);
  99. /* add gpio hog tests */
  100. ut_assertok(gpio_hog_lookup_name("hog_input_active_low", &desc));
  101. ut_asserteq(GPIOD_IS_IN | GPIOD_ACTIVE_LOW, desc->flags);
  102. ut_asserteq(10, desc->offset);
  103. ut_asserteq(1, dm_gpio_get_value(desc));
  104. ut_assertok(gpio_hog_lookup_name("hog_input_active_high", &desc));
  105. ut_asserteq(GPIOD_IS_IN, desc->flags);
  106. ut_asserteq(11, desc->offset);
  107. ut_asserteq(0, dm_gpio_get_value(desc));
  108. ut_assertok(gpio_hog_lookup_name("hog_output_low", &desc));
  109. ut_asserteq(GPIOD_IS_OUT, desc->flags);
  110. ut_asserteq(12, desc->offset);
  111. ut_asserteq(0, dm_gpio_get_value(desc));
  112. ut_assertok(dm_gpio_set_value(desc, 1));
  113. ut_asserteq(1, dm_gpio_get_value(desc));
  114. ut_assertok(gpio_hog_lookup_name("hog_output_high", &desc));
  115. ut_asserteq(GPIOD_IS_OUT, desc->flags);
  116. ut_asserteq(13, desc->offset);
  117. ut_asserteq(1, dm_gpio_get_value(desc));
  118. ut_assertok(dm_gpio_set_value(desc, 0));
  119. ut_asserteq(0, dm_gpio_get_value(desc));
  120. /* Check if lookup for labels work */
  121. ut_assertok(gpio_lookup_name("hog_input_active_low", &dev, &offset,
  122. &gpio));
  123. ut_asserteq_str(dev->name, "base-gpios");
  124. ut_asserteq(10, offset);
  125. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 10, gpio);
  126. ut_assert(gpio_lookup_name("hog_not_exist", &dev, &offset,
  127. &gpio));
  128. return 0;
  129. }
  130. DM_TEST(dm_test_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  131. /* Test that GPIO open-drain/open-source emulation works correctly */
  132. static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
  133. {
  134. struct gpio_desc desc_list[8];
  135. struct udevice *dev, *gpio_c;
  136. char buf[80];
  137. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
  138. ut_asserteq_str("a-test", dev->name);
  139. ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c));
  140. ut_asserteq_str("pinmux-gpios", gpio_c->name);
  141. ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
  142. ARRAY_SIZE(desc_list), 0))
  143. ut_asserteq(true, !!device_active(gpio_c));
  144. ut_asserteq_ptr(gpio_c, desc_list[0].dev);
  145. ut_asserteq_ptr(gpio_c, desc_list[1].dev);
  146. ut_asserteq_ptr(gpio_c, desc_list[2].dev);
  147. ut_asserteq_ptr(gpio_c, desc_list[3].dev);
  148. ut_asserteq_ptr(gpio_c, desc_list[4].dev);
  149. ut_asserteq_ptr(gpio_c, desc_list[5].dev);
  150. ut_asserteq_ptr(gpio_c, desc_list[6].dev);
  151. ut_asserteq_ptr(gpio_c, desc_list[7].dev);
  152. /* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */
  153. ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
  154. sandbox_gpio_get_dir_flags(gpio_c, 0));
  155. /* Set it as output high, should become an input */
  156. ut_assertok(dm_gpio_set_value(&desc_list[0], 1));
  157. ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf)));
  158. ut_asserteq_str("c0: input: 0 [x] a-test.test3-gpios0", buf);
  159. /* Set it as output low, should become output low */
  160. ut_assertok(dm_gpio_set_value(&desc_list[0], 0));
  161. ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf)));
  162. ut_asserteq_str("c0: output: 0 [x] a-test.test3-gpios0", buf);
  163. /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */
  164. ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
  165. sandbox_gpio_get_dir_flags(gpio_c, 1));
  166. /* Set it as output high, should become output high */
  167. ut_assertok(dm_gpio_set_value(&desc_list[1], 1));
  168. ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf)));
  169. ut_asserteq_str("c1: output: 1 [x] a-test.test3-gpios1", buf);
  170. /* Set it as output low, should become an input */
  171. ut_assertok(dm_gpio_set_value(&desc_list[1], 0));
  172. ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf)));
  173. ut_asserteq_str("c1: input: 1 [x] a-test.test3-gpios1", buf);
  174. /* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */
  175. ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
  176. sandbox_gpio_get_dir_flags(gpio_c, 6));
  177. /* Set it as output high, should become output low */
  178. ut_assertok(dm_gpio_set_value(&desc_list[6], 1));
  179. ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf)));
  180. ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf);
  181. /* Set it as output low, should become an input */
  182. ut_assertok(dm_gpio_set_value(&desc_list[6], 0));
  183. ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf)));
  184. ut_asserteq_str("c6: input: 0 [x] a-test.test3-gpios6", buf);
  185. /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */
  186. ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
  187. sandbox_gpio_get_dir_flags(gpio_c, 7));
  188. /* Set it as output high, should become an input */
  189. ut_assertok(dm_gpio_set_value(&desc_list[7], 1));
  190. ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf)));
  191. ut_asserteq_str("c7: input: 0 [x] a-test.test3-gpios7", buf);
  192. /* Set it as output low, should become output high */
  193. ut_assertok(dm_gpio_set_value(&desc_list[7], 0));
  194. ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf)));
  195. ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf);
  196. ut_assertok(gpio_free_list(dev, desc_list, 8));
  197. return 0;
  198. }
  199. DM_TEST(dm_test_gpio_opendrain_opensource,
  200. UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  201. /* Test that sandbox anonymous GPIOs work correctly */
  202. static int dm_test_gpio_anon(struct unit_test_state *uts)
  203. {
  204. unsigned int offset, gpio;
  205. struct udevice *dev;
  206. const char *name;
  207. int offset_count;
  208. /* And the anonymous bank */
  209. ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
  210. ut_asserteq_str(dev->name, "sandbox_gpio");
  211. ut_asserteq(14, offset);
  212. ut_asserteq(14, gpio);
  213. name = gpio_get_bank_info(dev, &offset_count);
  214. ut_asserteq_ptr(NULL, name);
  215. ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
  216. return 0;
  217. }
  218. DM_TEST(dm_test_gpio_anon, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  219. /* Test that gpio_requestf() works as expected */
  220. static int dm_test_gpio_requestf(struct unit_test_state *uts)
  221. {
  222. unsigned int offset, gpio;
  223. struct udevice *dev;
  224. char buf[80];
  225. ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
  226. ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
  227. sandbox_gpio_set_direction(dev, offset, 1);
  228. sandbox_gpio_set_value(dev, offset, 1);
  229. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  230. ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
  231. return 0;
  232. }
  233. DM_TEST(dm_test_gpio_requestf, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  234. /* Test that gpio_request() copies its string */
  235. static int dm_test_gpio_copy(struct unit_test_state *uts)
  236. {
  237. unsigned int offset, gpio;
  238. struct udevice *dev;
  239. char buf[80], name[10];
  240. ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
  241. strcpy(name, "odd_name");
  242. ut_assertok(gpio_request(gpio, name));
  243. sandbox_gpio_set_direction(dev, offset, 1);
  244. sandbox_gpio_set_value(dev, offset, 1);
  245. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  246. ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
  247. strcpy(name, "nothing");
  248. ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
  249. ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
  250. return 0;
  251. }
  252. DM_TEST(dm_test_gpio_copy, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  253. /* Test that we don't leak memory with GPIOs */
  254. static int dm_test_gpio_leak(struct unit_test_state *uts)
  255. {
  256. ut_assertok(dm_test_gpio(uts));
  257. ut_assertok(dm_test_gpio_anon(uts));
  258. ut_assertok(dm_test_gpio_requestf(uts));
  259. ut_assertok(dm_leak_check_end(uts));
  260. return 0;
  261. }
  262. DM_TEST(dm_test_gpio_leak, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  263. /* Test that we can find GPIOs using phandles */
  264. static int dm_test_gpio_phandles(struct unit_test_state *uts)
  265. {
  266. struct gpio_desc desc, desc_list[8], desc_list2[8];
  267. struct udevice *dev, *gpio_a, *gpio_b;
  268. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
  269. ut_asserteq_str("a-test", dev->name);
  270. ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
  271. ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
  272. ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
  273. ut_asserteq_str("base-gpios", gpio_a->name);
  274. ut_asserteq(true, !!device_active(gpio_a));
  275. ut_asserteq_ptr(gpio_a, desc.dev);
  276. ut_asserteq(4, desc.offset);
  277. /* GPIOF_INPUT is the sandbox GPIO driver default */
  278. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
  279. ut_assertok(dm_gpio_free(dev, &desc));
  280. ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
  281. 0));
  282. ut_asserteq_ptr(NULL, desc.dev);
  283. ut_asserteq(desc.offset, 0);
  284. ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
  285. 0));
  286. /* Last GPIO is ignord as it comes after <0> */
  287. ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
  288. ARRAY_SIZE(desc_list), 0));
  289. ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
  290. desc_list2,
  291. ARRAY_SIZE(desc_list2),
  292. 0));
  293. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
  294. ut_assertok(gpio_free_list(dev, desc_list, 3));
  295. ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL));
  296. ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
  297. ARRAY_SIZE(desc_list),
  298. GPIOD_IS_OUT |
  299. GPIOD_IS_OUT_ACTIVE));
  300. ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL));
  301. ut_asserteq_ptr(gpio_a, desc_list[0].dev);
  302. ut_asserteq(1, desc_list[0].offset);
  303. ut_asserteq_ptr(gpio_a, desc_list[1].dev);
  304. ut_asserteq(4, desc_list[1].offset);
  305. ut_asserteq_ptr(gpio_b, desc_list[2].dev);
  306. ut_asserteq(5, desc_list[2].offset);
  307. ut_asserteq(1, dm_gpio_get_value(desc_list));
  308. ut_assertok(gpio_free_list(dev, desc_list, 3));
  309. ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
  310. sandbox_gpio_get_dir_flags(gpio_a, 1));
  311. ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
  312. ARRAY_SIZE(desc_list), 0));
  313. /* This was set to output previously but flags resetted to 0 = INPUT */
  314. ut_asserteq(0, sandbox_gpio_get_dir_flags(gpio_a, 1));
  315. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL));
  316. /* Active low should invert the input value */
  317. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
  318. ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
  319. ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
  320. ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
  321. ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
  322. ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
  323. ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
  324. return 0;
  325. }
  326. DM_TEST(dm_test_gpio_phandles, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  327. /* Check the gpio pin configuration get from device tree information */
  328. static int dm_test_gpio_get_dir_flags(struct unit_test_state *uts)
  329. {
  330. struct gpio_desc desc_list[6];
  331. struct udevice *dev;
  332. ulong flags;
  333. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
  334. ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
  335. ARRAY_SIZE(desc_list), 0));
  336. ut_assertok(dm_gpio_get_dir_flags(&desc_list[0], &flags));
  337. ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags);
  338. ut_assertok(dm_gpio_get_dir_flags(&desc_list[1], &flags));
  339. ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags);
  340. ut_assertok(dm_gpio_get_dir_flags(&desc_list[2], &flags));
  341. ut_asserteq(GPIOD_IS_OUT, flags);
  342. ut_assertok(dm_gpio_get_dir_flags(&desc_list[3], &flags));
  343. ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags);
  344. ut_assertok(dm_gpio_get_dir_flags(&desc_list[4], &flags));
  345. ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags);
  346. ut_assertok(dm_gpio_get_dir_flags(&desc_list[5], &flags));
  347. ut_asserteq(GPIOD_IS_IN, flags);
  348. ut_assertok(gpio_free_list(dev, desc_list, 6));
  349. return 0;
  350. }
  351. DM_TEST(dm_test_gpio_get_dir_flags, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  352. /* Test of gpio_get_acpi() */
  353. static int dm_test_gpio_get_acpi(struct unit_test_state *uts)
  354. {
  355. struct acpi_gpio agpio;
  356. struct udevice *dev;
  357. struct gpio_desc desc;
  358. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
  359. ut_asserteq_str("a-test", dev->name);
  360. ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
  361. /* See sb_gpio_get_acpi() */
  362. ut_assertok(gpio_get_acpi(&desc, &agpio));
  363. ut_asserteq(1, agpio.pin_count);
  364. ut_asserteq(4, agpio.pins[0]);
  365. ut_asserteq(ACPI_GPIO_TYPE_IO, agpio.type);
  366. ut_asserteq(ACPI_GPIO_PULL_UP, agpio.pull);
  367. ut_asserteq_str("\\_SB.PINC", agpio.resource);
  368. ut_asserteq(0, agpio.interrupt_debounce_timeout);
  369. ut_asserteq(0, agpio.irq.pin);
  370. ut_asserteq(1234, agpio.output_drive_strength);
  371. ut_asserteq(true, agpio.io_shared);
  372. ut_asserteq(ACPI_GPIO_IO_RESTRICT_INPUT, agpio.io_restrict);
  373. ut_asserteq(ACPI_GPIO_ACTIVE_HIGH, agpio.polarity);
  374. return 0;
  375. }
  376. DM_TEST(dm_test_gpio_get_acpi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  377. /* Test of gpio_get_acpi() with an interrupt GPIO */
  378. static int dm_test_gpio_get_acpi_irq(struct unit_test_state *uts)
  379. {
  380. struct acpi_gpio agpio;
  381. struct udevice *dev;
  382. struct gpio_desc desc;
  383. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
  384. ut_asserteq_str("a-test", dev->name);
  385. ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0));
  386. /* See sb_gpio_get_acpi() */
  387. ut_assertok(gpio_get_acpi(&desc, &agpio));
  388. ut_asserteq(1, agpio.pin_count);
  389. ut_asserteq(6, agpio.pins[0]);
  390. ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, agpio.type);
  391. ut_asserteq(ACPI_GPIO_PULL_DOWN, agpio.pull);
  392. ut_asserteq_str("\\_SB.PINC", agpio.resource);
  393. ut_asserteq(4321, agpio.interrupt_debounce_timeout);
  394. ut_asserteq(6, agpio.irq.pin);
  395. ut_asserteq(ACPI_IRQ_ACTIVE_BOTH, agpio.irq.polarity);
  396. ut_asserteq(ACPI_IRQ_SHARED, agpio.irq.shared);
  397. ut_asserteq(true, agpio.irq.wake);
  398. ut_asserteq(0, agpio.output_drive_strength);
  399. ut_asserteq(false, agpio.io_shared);
  400. ut_asserteq(0, agpio.io_restrict);
  401. ut_asserteq(ACPI_GPIO_ACTIVE_LOW, agpio.polarity);
  402. return 0;
  403. }
  404. DM_TEST(dm_test_gpio_get_acpi_irq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);