1 /*
2  * Copyright (c) 2023 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public APIs for Time-aware GPIO drivers
10  */
11 #ifndef ZEPHYR_DRIVERS_MISC_TIMEAWARE_GPIO_TIMEAWARE_GPIO
12 #define ZEPHYR_DRIVERS_MISC_TIMEAWARE_GPIO_TIMEAWARE_GPIO
13 
14 /**
15  * @brief Time-aware GPIO Interface
16  * @defgroup tgpio_interface Time-aware GPIO Interface
17  * @since 3.5
18  * @version 0.1.0
19  * @ingroup io_interfaces
20  * @{
21  */
22 
23 #include <zephyr/sys/__assert.h>
24 #include <zephyr/sys/slist.h>
25 
26 #include <zephyr/types.h>
27 #include <stddef.h>
28 #include <zephyr/device.h>
29 #include <zephyr/internal/syscall_handler.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /**
36  * @brief Event polarity
37  */
38 enum tgpio_pin_polarity {
39 	TGPIO_RISING_EDGE = 0,
40 	TGPIO_FALLING_EDGE,
41 	TGPIO_TOGGLE_EDGE,
42 };
43 
44 /**
45  * @cond INTERNAL_HIDDEN
46  *
47  * TGPIO driver API definition and system call entry points
48  *
49  * (Internal use only.)
50  */
51 
52 __subsystem struct tgpio_driver_api {
53 	int (*pin_disable)(const struct device *dev, uint32_t pin);
54 	int (*get_time)(const struct device *dev, uint64_t *current_time);
55 	int (*cyc_per_sec)(const struct device *dev, uint32_t *cycles);
56 	int (*set_perout)(const struct device *dev, uint32_t pin, uint64_t start_time,
57 			  uint64_t repeat_interval, bool periodic_enable);
58 	int (*config_ext_ts)(const struct device *dev, uint32_t pin, uint32_t event_polarity);
59 	int (*read_ts_ec)(const struct device *dev, uint32_t pin, uint64_t *timestamp,
60 			  uint64_t *event_count);
61 };
62 
63 /**
64  * @endcond
65  */
66 
67 /**
68  * @brief Get time from ART timer
69  *
70  * @param dev TGPIO device
71  * @param current_time Pointer to store timer value in cycles
72  *
73  * @return 0 if successful
74  * @return negative errno code on failure.
75  */
76 __syscall int tgpio_port_get_time(const struct device *dev, uint64_t *current_time);
77 
z_impl_tgpio_port_get_time(const struct device * dev,uint64_t * current_time)78 static inline int z_impl_tgpio_port_get_time(const struct device *dev, uint64_t *current_time)
79 {
80 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
81 
82 	return api->get_time(dev, current_time);
83 }
84 
85 /**
86  * @brief Get current running rate
87  *
88  * @param dev TGPIO device
89  * @param cycles pointer to store current running frequency
90  *
91  * @return 0 if successful, negative errno code on failure.
92  */
93 __syscall int tgpio_port_get_cycles_per_second(const struct device *dev, uint32_t *cycles);
94 
z_impl_tgpio_port_get_cycles_per_second(const struct device * dev,uint32_t * cycles)95 static inline int z_impl_tgpio_port_get_cycles_per_second(const struct device *dev,
96 							   uint32_t *cycles)
97 {
98 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
99 
100 	return api->cyc_per_sec(dev, cycles);
101 }
102 
103 /**
104  * @brief Disable operation on pin
105  *
106  * @param dev TGPIO device
107  * @param pin TGPIO pin
108  *
109  * @return 0 if successful, negative errno code on failure.
110  */
111 __syscall int tgpio_pin_disable(const struct device *dev, uint32_t pin);
112 
z_impl_tgpio_pin_disable(const struct device * dev,uint32_t pin)113 static inline int z_impl_tgpio_pin_disable(const struct device *dev, uint32_t pin)
114 {
115 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
116 
117 	return api->pin_disable(dev, pin);
118 }
119 
120 /**
121  * @brief Enable/Continue operation on pin
122  *
123  * @param dev TGPIO device
124  * @param pin TGPIO pin
125  * @param event_polarity TGPIO pin event polarity
126  *
127  * @return 0 if successful, negative errno code on failure.
128  */
129 __syscall int tgpio_pin_config_ext_timestamp(const struct device *dev, uint32_t pin,
130 					      uint32_t event_polarity);
131 
z_impl_tgpio_pin_config_ext_timestamp(const struct device * dev,uint32_t pin,uint32_t event_polarity)132 static inline int z_impl_tgpio_pin_config_ext_timestamp(const struct device *dev, uint32_t pin,
133 							 uint32_t event_polarity)
134 {
135 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
136 
137 	return api->config_ext_ts(dev, pin, event_polarity);
138 }
139 
140 /**
141  * @brief Enable periodic pulse generation on a pin
142  *
143  * @param dev TGPIO device
144  * @param pin TGPIO pin
145  * @param start_time start_time of first pulse in hw cycles
146  * @param repeat_interval repeat interval between two pulses in hw cycles
147  * @param periodic_enable enables periodic mode if 'true' is passed.
148  *
149  * @return 0 if successful, negative errno code on failure.
150  */
151 __syscall int tgpio_pin_periodic_output(const struct device *dev, uint32_t pin,
152 					 uint64_t start_time, uint64_t repeat_interval,
153 					 bool periodic_enable);
154 
z_impl_tgpio_pin_periodic_output(const struct device * dev,uint32_t pin,uint64_t start_time,uint64_t repeat_interval,bool periodic_enable)155 static inline int z_impl_tgpio_pin_periodic_output(const struct device *dev, uint32_t pin,
156 						    uint64_t start_time, uint64_t repeat_interval,
157 						    bool periodic_enable)
158 {
159 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
160 
161 	return api->set_perout(dev, pin, start_time, repeat_interval, periodic_enable);
162 }
163 
164 /**
165  * @brief Read timestamp and event counter from TGPIO
166  *
167  * @param dev TGPIO device
168  * @param pin TGPIO pin
169  * @param timestamp timestamp of the last pulse received
170  * @param event_count number of pulses received since the pin is enabled
171  *
172  * @return 0 if successful, negative errno code on failure.
173  */
174 __syscall int tgpio_pin_read_ts_ec(const struct device *dev, uint32_t pin, uint64_t *timestamp,
175 				    uint64_t *event_count);
176 
z_impl_tgpio_pin_read_ts_ec(const struct device * dev,uint32_t pin,uint64_t * timestamp,uint64_t * event_count)177 static inline int z_impl_tgpio_pin_read_ts_ec(const struct device *dev, uint32_t pin,
178 					       uint64_t *timestamp, uint64_t *event_count)
179 {
180 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
181 
182 	return api->read_ts_ec(dev, pin, timestamp, event_count);
183 }
184 
185 /**
186  * @}
187  */
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #include <zephyr/syscalls/timeaware_gpio.h>
194 
195 #endif /* ZEPHYR_DRIVERS_MISC_TIMEAWARE_GPIO_TIMEAWARE_GPIO */
196