1 /*
2  * Copyright (c) 2023 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "iut.h"
8 #include "sedi_driver_gpio.h"
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/kernel.h>
11 
12 #define DT_DRV_COMPAT intel_sedi_gpio
13 
14 #define GPIO_TEST_NUM (8)
15 #define GPIO_LOOP_INPUT (1)
16 #define GPIO_LOOP_OUTPUT (4)
17 
18 #if DEBUG_GPIO
dump_gpio_regs(void)19 static void dump_gpio_regs(void)
20 {
21 	uint32_t regs = SEDI_IREG_BASE(GPIO, 0);
22 
23 	iut_print("GPLR register is 0x%x\n", read32(regs + 0x4));
24 	iut_print("GPDR register is 0x%x\n", read32(regs + 0x1C));
25 	iut_print("GRER register is 0x%x\n", read32(regs + 0x64));
26 	iut_print("GFER register is 0x%x\n", read32(regs + 0x7c));
27 	iut_print("GIMR register is 0x%x\n", read32(regs + 0xac));
28 	iut_print("GISR register is 0x%x\n", read32(regs + 0xc4));
29 	iut_print("\n");
30 }
31 #else
dump_gpio_regs(void)32 static void dump_gpio_regs(void)
33 {
34 }
35 #endif
36 
test_gpio_output(int argc,char ** argv)37 static int test_gpio_output(int argc, char **argv)
38 {
39 	const struct device *gpio_dev = DEVICE_DT_GET(DT_DRV_INST(0));
40 	int ret, i;
41 
42 	for (i = 0; i < 8; i++) {
43 		iut_case_print("starting to test gpio pin %d output ...\n", i);
44 
45 		/* Configure gpio pin to output mode, set to high */
46 		gpio_pin_configure(gpio_dev, i, GPIO_OUTPUT);
47 		dump_gpio_regs();
48 		gpio_pin_set_raw(gpio_dev, i, 1);
49 		dump_gpio_regs();
50 
51 		/* Set pin to input mode and check the level */
52 		gpio_pin_configure(gpio_dev, i, GPIO_INPUT);
53 		dump_gpio_regs();
54 		ret = gpio_pin_get_raw(gpio_dev, i);
55 		TEST_ASSERT_EQUAL(1, ret);
56 
57 		/* Set pin to output and set to low */
58 		gpio_pin_configure(gpio_dev, i, GPIO_OUTPUT);
59 		dump_gpio_regs();
60 		gpio_pin_set_raw(gpio_dev, i, 0);
61 		dump_gpio_regs();
62 
63 		/* Set pin to input mode and check the level */
64 		gpio_pin_configure(gpio_dev, i, GPIO_INPUT);
65 		ret = gpio_pin_get_raw(gpio_dev, i);
66 		dump_gpio_regs();
67 		TEST_ASSERT_EQUAL(0, ret);
68 
69 		iut_case_print("Pin %d Test done\n", i);
70 	}
71 
72 	return IUT_ERR_OK;
73 }
74 DEFINE_IUT_CASE(gpio_output, gpio, IUT_ATTRI_NONE);
75 
76 static struct gpio_callback gp_cb;
77 static uint32_t times;
78 
test_callback(const struct device * port,struct gpio_callback * cb,gpio_port_pins_t pins)79 static void test_callback(const struct device *port,
80 				struct gpio_callback *cb,
81 				gpio_port_pins_t pins)
82 {
83 	(void)port;
84 	(void)cb;
85 	times++;
86 	iut_print("trigger callback %d times, pin index is 0x%x\n", times, pins);
87 }
88 
test_gpio_loopback(int argc,char ** argv)89 static int test_gpio_loopback(int argc, char **argv)
90 {
91 	const struct device *gpio_dev = DEVICE_DT_GET(DT_DRV_INST(0));
92 	int i;
93 
94 	/* Configure gpio output pin */
95 	gpio_pin_configure(gpio_dev, GPIO_LOOP_OUTPUT, GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH);
96 	dump_gpio_regs();
97 
98 	/* Configure gpio input pin */
99 	gpio_pin_configure(gpio_dev, GPIO_LOOP_INPUT, GPIO_INPUT);
100 	dump_gpio_regs();
101 
102 	/* Enable input pin interrupts and callbacks */
103 	gpio_pin_interrupt_configure(gpio_dev, GPIO_LOOP_INPUT,
104 					GPIO_INT_MODE_EDGE | GPIO_INT_LOW_0);
105 
106 	gpio_init_callback(&gp_cb, test_callback, BIT(GPIO_LOOP_INPUT));
107 	gpio_add_callback(gpio_dev, &gp_cb);
108 
109 	for (i = 0; i < 20; i++) {
110 		gpio_pin_set(gpio_dev, GPIO_LOOP_OUTPUT, 0);
111 		k_msleep(100);
112 		gpio_pin_toggle(gpio_dev, GPIO_LOOP_OUTPUT);
113 		k_msleep(100);
114 	}
115 
116 
117 	return IUT_ERR_OK;
118 }
119 
120 DEFINE_IUT_CASE(gpio_loopback, gpio, IUT_ATTRI_NONE);
121